180 lines
5.6 KiB
GDScript
180 lines
5.6 KiB
GDScript
extends KinematicBody2D
|
|
|
|
var velocity = Vector2(0,0)
|
|
var direction = Vector2(0,0)
|
|
|
|
export var player_number: int = 1
|
|
|
|
onready var movement_animations: AnimatedSprite = $AnimatedSprite
|
|
|
|
onready var movement_state_machine: Node = $Controllers/state_machine
|
|
|
|
onready var player_move_component = $Controllers/move_component
|
|
|
|
onready var player_hurtbox = $Hurtbox/CollisionShape2D
|
|
|
|
onready var gun = $Gun
|
|
|
|
func _ready() -> void:
|
|
movement_state_machine.init(self, movement_animations, player_move_component)
|
|
player_move_component.player_number = player_number
|
|
|
|
func _unhandled_input(event: InputEvent) -> void:
|
|
movement_state_machine.process_input(event)
|
|
|
|
func _physics_process(delta: float) -> void:
|
|
movement_state_machine.process_physics(delta)
|
|
# #TODO: So much hack
|
|
# if direction.x != 0:
|
|
# gun.set_scale( Vector2(direction.x,1))
|
|
if direction.x < 0:
|
|
var t = Transform2D()
|
|
# Translation
|
|
t.origin = Vector2(gun.offset_position.x * -1, gun.offset_position.y)
|
|
gun.transform = t
|
|
elif direction.x > 0:
|
|
var t = Transform2D()
|
|
# Translation
|
|
t.origin = Vector2(gun.offset_position.x, gun.offset_position.y)
|
|
gun.transform = t
|
|
|
|
func _process(delta: float) -> void:
|
|
movement_state_machine.process_frame(delta)
|
|
PlayerInfo.player_position = global_position
|
|
|
|
func _on_AnimatedSprite_animation_finished():
|
|
if movement_animations.frames.get_animation_loop(movement_animations.animation) == false:
|
|
movement_state_machine.current_state.animation_index += 1
|
|
|
|
func _on_Hurtbox_area_entered(area):
|
|
print("Ouch.",area)
|
|
movement_state_machine.change_state($Controllers/state_machine/hurt)
|
|
|
|
func set_hurtbox(enabled: bool):
|
|
#player_hurtbox.disabled = enabled #nope
|
|
player_hurtbox.set_deferred("disabled", !enabled) #WTF Apparently this is how to do it
|
|
#$Hurtbox.monitoring = enabled #nope
|
|
#$Hurtbox.set_deferred("monitoring", enabled) #works
|
|
#$Hurtbox.monitoring = false #nope
|
|
#$Hurtbox.set_monitoring(enabled) # Nope
|
|
|
|
func shoot_projectile():
|
|
var is_shooting = false
|
|
is_shooting = gun.shoot(direction.x)
|
|
|
|
#func _ready():
|
|
# # Static types are necessary here to avoid warnings.
|
|
# var camera: Camera2D = $Camera2D
|
|
# camera.custom_viewport = $"../.."
|
|
# yield(get_tree(), "idle_frame")
|
|
# camera.make_current()
|
|
|
|
#
|
|
#func _physics_process(delta):
|
|
# # Horizontal movement
|
|
# velocity.x = (Input.get_axis("ui_left", "ui_right") * WALK_MAX_SPEED)
|
|
#
|
|
# # Vertical movement code. Apply gravity.
|
|
# velocity.y += gravity * delta
|
|
#
|
|
# # Move based on the velocity and snap to the ground.
|
|
# #velocity = move_and_slide_with_snap(velocity, Vector2.DOWN, Vector2.UP)
|
|
# velocity = move_and_slide(velocity, Vector2(0, -1))
|
|
#
|
|
# # Check for jumping. is_on_floor() must be called after movement code.
|
|
# # Apply Jump velocity
|
|
# if is_on_floor() and Input.is_action_just_pressed("ui_accept"):
|
|
# velocity.y = -JUMP_SPEED
|
|
#
|
|
# # Animate based on last state and velocity
|
|
# if velocity.x > 0 and _animated_sprite.flip_h != true:
|
|
# _animated_sprite.flip_h = true
|
|
# if velocity.x < 0 and _animated_sprite.flip_h != false:
|
|
# _animated_sprite.flip_h = false
|
|
#
|
|
# # forced state changes like falling an gitting hurt
|
|
# if velocity.y > 0 and player_state != STATE.JUMP and player_state != STATE.FALL:
|
|
# print("STATE_TRANSITION: Fall")
|
|
# player_state = STATE.FALL
|
|
#
|
|
#
|
|
# # Each state should trigger its own animation
|
|
# match player_state:
|
|
# STATE.IDLE:
|
|
# # Transitions
|
|
# if velocity.y == -JUMP_SPEED:
|
|
# print("STATE_TRANSITION: Jump")
|
|
# player_last_state = STATE.IDLE
|
|
# player_state = STATE.JUMP
|
|
# elif velocity.x != 0:
|
|
# print("STATE_TRANSITION: Run")
|
|
# player_last_state = STATE.IDLE
|
|
# player_state = STATE.RUN
|
|
# # Animation
|
|
# if player_last_state != STATE.IDLE:
|
|
# $IdleTimeout.start()
|
|
# _animated_sprite.play("idle")
|
|
# player_last_state = STATE.IDLE
|
|
# if $IdleTimeout.time_left == 0 and _animated_sprite.animation != 'blink':
|
|
# _animated_sprite.play("blink")
|
|
# animation_play_finished = false
|
|
# if $IdleTimeout.time_left == 0 and animation_play_finished == true:
|
|
# $IdleTimeout.start()
|
|
# _animated_sprite.play("idle")
|
|
# print("blink over")
|
|
#
|
|
# STATE.JUMP:
|
|
# # Transitions
|
|
# if is_on_floor():
|
|
# print("STATE_TRANSITION: Idle")
|
|
# _animated_sprite.play("idle")
|
|
# player_last_state = STATE.JUMP
|
|
# player_state = STATE.IDLE
|
|
# # Animation
|
|
# if player_last_state != STATE.JUMP:
|
|
# _animated_sprite.play("jump")
|
|
# player_last_state = STATE.JUMP
|
|
#
|
|
# STATE.FALL:
|
|
# # Transitions
|
|
# if is_on_floor():
|
|
# print("STATE_TRANSITION: Idle")
|
|
# _animated_sprite.play("idle")
|
|
# player_last_state = STATE.FALL
|
|
# player_state = STATE.IDLE
|
|
# # Animation
|
|
# if player_last_state != STATE.FALL:
|
|
# _animated_sprite.animation = "jump"
|
|
# _animated_sprite.frame = 8
|
|
# _animated_sprite.stop()
|
|
# player_last_state = STATE.FALL
|
|
#
|
|
# STATE.RUN:
|
|
# # print (_animated_sprite.animation ) # apparentl playing doesn't stop when loop is disabled.
|
|
# # Transitions
|
|
# if velocity.y == -JUMP_SPEED:
|
|
# print("STATE_TRANSITION: Jump")
|
|
# _animated_sprite.play("jump")
|
|
# player_last_state = STATE.RUN
|
|
# player_state = STATE.JUMP
|
|
# elif velocity.x == 0:
|
|
# print("STATE_TRANSITION: Idle")
|
|
# _animated_sprite.play("idle")
|
|
# player_last_state = STATE.RUN
|
|
# player_state = STATE.IDLE
|
|
# # Animation
|
|
# if player_last_state == STATE.IDLE:
|
|
# _animated_sprite.play("step")
|
|
# animation_play_finished = false
|
|
# player_last_state = STATE.RUN
|
|
# elif animation_play_finished == true:
|
|
# _animated_sprite.play("run")
|
|
# animation_play_finished = false
|
|
#
|
|
#
|
|
#func _on_AnimatedSprite_animation_finished():
|
|
# animation_play_finished = true
|
|
# print($AnimatedSprite.animation," anim over ", $IdleTimeout.time_left, animation_play_finished)
|
|
|
|
|