week3
This commit is contained in:
1 parent
3607e63548
commit
c1486f3e55
16 files changed
+488
-22
No files matched your search
+71
-8
@@ -4,12 +4,20 @@ class_name Player extends CharacterBody2D
|
||||
@onready var left_cast: RayCast2D = $LeftCast
|
||||
@onready var right_spawn: Marker2D = $RightSpawn
|
||||
@onready var left_spawn: Marker2D = $LeftSpawn
|
||||
@onready var player_graphic: AnimatedSprite2D = $PlayerGraphic
|
||||
|
||||
@export var shove_power:= 800
|
||||
@export var bump_power:= 50
|
||||
|
||||
var direction
|
||||
signal bulletSignal(pos, speed)
|
||||
|
||||
enum FaceDirection {LEFT, RIGHT}
|
||||
enum PlayerStates {IDLE, RUNNING, JUMPING, FALLING}
|
||||
|
||||
var facing: FaceDirection = FaceDirection.RIGHT
|
||||
var current_player_state: PlayerStates = PlayerStates.IDLE
|
||||
var upJump:bool = false
|
||||
|
||||
var pushRightEnabled = false
|
||||
var pushLeftEnabled = false
|
||||
@@ -23,6 +31,26 @@ func _physics_process(delta: float) -> void:
|
||||
if not is_on_floor():
|
||||
velocity += get_gravity() * delta
|
||||
|
||||
handle_input()
|
||||
handle_movement()
|
||||
handle_states()
|
||||
handle_animations()
|
||||
move_and_slide()
|
||||
handle_collisions()
|
||||
|
||||
func handle_input()-> void:
|
||||
#Handle shoot
|
||||
if Input.is_action_just_pressed("shoot"):
|
||||
print ("player wants to shoot")
|
||||
#send a bullet signal
|
||||
if facing==FaceDirection.RIGHT:
|
||||
bulletSignal.emit(right_spawn.global_transform, 800)
|
||||
if facing==FaceDirection.LEFT:
|
||||
bulletSignal.emit(left_spawn.global_transform, -800)
|
||||
player_graphic.flip_h = true
|
||||
|
||||
|
||||
|
||||
# Handle shove.
|
||||
if Input.is_action_just_pressed("shove"):
|
||||
print("Player shove!")
|
||||
@@ -33,25 +61,30 @@ func _physics_process(delta: float) -> void:
|
||||
# shove it left
|
||||
pushTarget.apply_central_impulse(Vector2(-1,0) * shove_power)
|
||||
|
||||
|
||||
# Handle jump.
|
||||
# Handle jump.
|
||||
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
|
||||
velocity.y = JUMP_VELOCITY
|
||||
current_player_state=PlayerStates.JUMPING
|
||||
upJump = true
|
||||
|
||||
# Get the input direction and handle the movement/deceleration.
|
||||
# As good practice, you should replace UI actions with custom gameplay actions.
|
||||
direction = Input.get_axis("ui_left", "ui_right")
|
||||
|
||||
# Get the input direction and handle the movement/deceleration.
|
||||
# As good practice, you should replace UI actions with custom gameplay actions.
|
||||
var direction := Input.get_axis("ui_left", "ui_right")
|
||||
func handle_movement()-> void:
|
||||
if direction:
|
||||
velocity.x = direction * SPEED
|
||||
# set player direction
|
||||
if direction < 0:
|
||||
facing = FaceDirection.LEFT
|
||||
player_graphic.flip_h = true
|
||||
if direction > 0:
|
||||
facing = FaceDirection.RIGHT
|
||||
player_graphic.flip_h = false
|
||||
else:
|
||||
velocity.x = move_toward(velocity.x, 0, SPEED)
|
||||
|
||||
move_and_slide()
|
||||
|
||||
func handle_collisions()-> void:
|
||||
# find all things my player collided with
|
||||
for i in get_slide_collision_count():
|
||||
var c = get_slide_collision(i)
|
||||
@@ -77,5 +110,35 @@ func _physics_process(delta: float) -> void:
|
||||
pushTarget = collider
|
||||
else:
|
||||
pushLeftEnabled = false
|
||||
|
||||
func handle_states()-> void:
|
||||
match current_player_state:
|
||||
PlayerStates.IDLE when velocity.x !=0:
|
||||
current_player_state = PlayerStates.RUNNING
|
||||
PlayerStates.RUNNING when velocity.x==0:
|
||||
current_player_state = PlayerStates.IDLE
|
||||
PlayerStates.JUMPING when velocity.y>0:
|
||||
#you falling
|
||||
current_player_state = PlayerStates.FALLING
|
||||
PlayerStates.FALLING when is_on_floor():
|
||||
if velocity.x ==0:
|
||||
current_player_state = PlayerStates.IDLE
|
||||
else:
|
||||
current_player_state = PlayerStates.RUNNING
|
||||
|
||||
func handle_animations()-> void:
|
||||
match current_player_state:
|
||||
PlayerStates.RUNNING:
|
||||
player_graphic.play("run")
|
||||
PlayerStates.IDLE:
|
||||
player_graphic.play("idle")
|
||||
PlayerStates.JUMPING:
|
||||
if upJump:
|
||||
player_graphic.play("jump")
|
||||
PlayerStates.FALLING:
|
||||
player_graphic.play("fall")
|
||||
|
||||
|
||||
func _on_animation_finished() -> void:
|
||||
match current_player_state:
|
||||
PlayerStates.JUMPING:
|
||||
upJump = false
|
||||
Reference in new issue
Block a user