added shooting animation; timer is a little broken
This commit is contained in:
1 parent
14146a476d
commit
ffd98d163e
9 files changed
+302
-15
No files matched your search
+39
-11
@@ -1,10 +1,12 @@
|
||||
extends CharacterBody2D
|
||||
class_name Player extends CharacterBody2D
|
||||
## Character controller.
|
||||
## TODO: fix bullet cooldown timer
|
||||
|
||||
const SPEED = 300.0
|
||||
const JUMP_VELOCITY = -600.0
|
||||
|
||||
enum FaceDirection{LEFT, RIGHT}
|
||||
enum State{IDLE, WALK, JUMP, FALLING}
|
||||
enum State{IDLE, WALK, JUMP, FALLING, SHOOT_STILL, SHOOT_RUN}
|
||||
|
||||
@export var BUMP_POWER := 50
|
||||
@export var SHOVE_POWER := 200
|
||||
@@ -14,9 +16,10 @@ enum State{IDLE, WALK, JUMP, FALLING}
|
||||
var current_state: State = State.IDLE
|
||||
var facing : FaceDirection = FaceDirection.RIGHT
|
||||
var direction : float = 0.0
|
||||
var jump_buffer_timer : Timer
|
||||
var push_target : RigidBody2D
|
||||
var push_enabled : bool = false
|
||||
var jump_buffer_timer : Timer
|
||||
var shoot_cooldown_timer : Timer
|
||||
|
||||
@onready var right_cast: RayCast2D = $RightCast
|
||||
@onready var left_cast: RayCast2D = $LeftCast
|
||||
@@ -27,6 +30,9 @@ var push_enabled : bool = false
|
||||
func _ready() -> void:
|
||||
jump_buffer_timer = Timer.new()
|
||||
add_child(jump_buffer_timer)
|
||||
shoot_cooldown_timer = Timer.new()
|
||||
shoot_cooldown_timer.one_shot = true
|
||||
add_child(shoot_cooldown_timer)
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
handle_input()
|
||||
@@ -65,14 +71,22 @@ func handle_input() -> void:
|
||||
|
||||
# Handle shooting
|
||||
if Input.is_action_just_pressed("shoot"):
|
||||
print("pew-pew")
|
||||
match facing:
|
||||
FaceDirection.RIGHT:
|
||||
%SceneManager.make_bullet(right_spawn.global_transform, 700)
|
||||
print("shoot right")
|
||||
FaceDirection.LEFT:
|
||||
%SceneManager.make_bullet(left_spawn.global_transform, -700)
|
||||
print("shoot left")
|
||||
if shoot_cooldown_timer.time_left == 0:
|
||||
if velocity.x:
|
||||
current_state = State.SHOOT_RUN
|
||||
else:
|
||||
current_state = State.SHOOT_STILL
|
||||
print("pew-pew")
|
||||
match facing:
|
||||
FaceDirection.RIGHT:
|
||||
%SceneManager.make_bullet(right_spawn.global_transform, 700)
|
||||
print("shoot right")
|
||||
FaceDirection.LEFT:
|
||||
%SceneManager.make_bullet(left_spawn.global_transform, -700)
|
||||
print("shoot left")
|
||||
shoot_cooldown_timer.start(0.2)
|
||||
else:
|
||||
print("can't shoot right now because there are %s seconds left in the cooldown timer" % shoot_cooldown_timer.time_left)
|
||||
|
||||
# Handle throwing grenades
|
||||
if Input.is_action_just_pressed("throw"):
|
||||
@@ -152,6 +166,16 @@ func update_state() -> void:
|
||||
current_state = State.IDLE
|
||||
else:
|
||||
current_state = State.WALK
|
||||
|
||||
# Player shooting
|
||||
State.SHOOT_STILL:
|
||||
await player_sprite.animation_finished
|
||||
current_state = State.IDLE
|
||||
|
||||
# Player shooting while moving
|
||||
State.SHOOT_RUN:
|
||||
await player_sprite.animation_finished
|
||||
current_state = State.WALK
|
||||
|
||||
func update_animation() -> void:
|
||||
match facing:
|
||||
@@ -168,3 +192,7 @@ func update_animation() -> void:
|
||||
player_sprite.play("jump")
|
||||
State.FALLING:
|
||||
player_sprite.play("fall")
|
||||
State.SHOOT_STILL:
|
||||
player_sprite.play("shoot_still")
|
||||
State.SHOOT_RUN:
|
||||
player_sprite.play("shoot_run")
|
||||
Reference in new issue
Block a user