tilemap, state machine, player animations, scenemanager.
This commit is contained in:
1 parent
eac97fda20
commit
81ae9e63e5
14 files changed
+484
-21
No files matched your search
+72
-11
@@ -4,44 +4,99 @@ class_name Player extends CharacterBody2D
|
||||
@onready var right_cast: RayCast2D = $"Right Cast"
|
||||
@onready var left_spawn: Node2D = $LeftSpawn
|
||||
@onready var right_spawn: Node2D = $RightSpawn
|
||||
@onready var jump_buffer_timer = $JumpBufferTimer
|
||||
@onready var playergraphic = $playergraphic
|
||||
|
||||
|
||||
|
||||
const SPEED = 300.0
|
||||
const JUMP_VELOCITY = -400.0
|
||||
var acceleration:int = 20
|
||||
var upJump:bool = false
|
||||
|
||||
var direction
|
||||
enum FaceDirection{LEFT, RIGHT}
|
||||
var facing:FaceDirection = FaceDirection.RIGHT
|
||||
enum State{IDLE,RUN,JUMP,FALLING}
|
||||
var current_state:State = State.IDLE
|
||||
|
||||
|
||||
var pushTarget
|
||||
var pushEnabled := false
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
# Add the gravity.
|
||||
|
||||
|
||||
|
||||
handle_input()
|
||||
handle_movement(delta)
|
||||
handle_states()
|
||||
handle_animation()
|
||||
move_and_slide()
|
||||
handle_collisions()
|
||||
|
||||
func handle_movement(delta):
|
||||
if direction:
|
||||
velocity.x = direction * SPEED
|
||||
else:
|
||||
velocity.x = move_toward(velocity.x, 0, SPEED)
|
||||
func handle_states() -> void:
|
||||
match current_state:
|
||||
State.IDLE when velocity.x !=0:
|
||||
current_state = State.RUN
|
||||
State.RUN:
|
||||
if velocity.x == 0:
|
||||
current_state = State.IDLE
|
||||
# player steps off ledge
|
||||
if not is_on_floor() && velocity.y > 0:
|
||||
current_state = State.FALLING
|
||||
|
||||
State.FALLING when is_on_floor():
|
||||
if velocity.x == 0:
|
||||
current_state = State.IDLE
|
||||
else: current_state = State.RUN
|
||||
|
||||
State.JUMP when velocity.y >0:
|
||||
current_state = State.FALLING
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
func handle_animation() -> void:
|
||||
match current_state:
|
||||
State.IDLE:
|
||||
playergraphic.play("idle")
|
||||
State.RUN:
|
||||
playergraphic.play("run")
|
||||
State.FALLING:
|
||||
playergraphic.play("fall")
|
||||
State.JUMP:
|
||||
if upJump:
|
||||
playergraphic.play("jump")
|
||||
|
||||
if not is_on_floor():
|
||||
velocity += get_gravity() * delta
|
||||
|
||||
func handle_movement(delta:float) -> void:
|
||||
if direction == 0:
|
||||
#not moving
|
||||
velocity.x = move_toward(velocity.x,0, acceleration)
|
||||
else: velocity.x = move_toward(velocity.x, SPEED * direction, acceleration)
|
||||
|
||||
if not is_on_floor():
|
||||
velocity += get_gravity() * delta
|
||||
if is_on_floor() && jump_buffer_timer.time_left >0:
|
||||
velocity.y = JUMP_VELOCITY
|
||||
jump_buffer_timer.stop()
|
||||
current_state = State.JUMP
|
||||
upJump = true
|
||||
|
||||
func handle_input():
|
||||
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
|
||||
velocity.y = JUMP_VELOCITY
|
||||
#velocity.y = JUMP_VELOCITY
|
||||
jump_buffer_timer.start()
|
||||
|
||||
direction = Input.get_axis("ui_left", "ui_right")
|
||||
if direction < 0:
|
||||
facing = FaceDirection.LEFT
|
||||
playergraphic.flip_h = true
|
||||
if direction > 0:
|
||||
facing = FaceDirection.RIGHT
|
||||
playergraphic.flip_h = false
|
||||
if Input.is_action_just_pressed("shove") && pushEnabled:
|
||||
var shoveDirection:int
|
||||
match facing:
|
||||
@@ -82,3 +137,9 @@ func handle_collisions():
|
||||
if not right_cast.is_colliding() && not left_cast.is_colliding():
|
||||
pushEnabled = false
|
||||
|
||||
|
||||
|
||||
func _on_animation_finished() -> void:
|
||||
match current_state:
|
||||
State.JUMP:
|
||||
upJump = false
|
||||
@@ -1,5 +1,6 @@
|
||||
extends Node2D
|
||||
|
||||
signal playerDamage
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
@@ -13,6 +14,11 @@ func _process(delta: float) -> void:
|
||||
|
||||
func _on_trigger(effect: Variant, body: Variant) -> void:
|
||||
print("GC sees trigger "+effect)
|
||||
match effect:
|
||||
"player_hurt":
|
||||
if body is Player:
|
||||
print("hurt the player")
|
||||
playerDamage.emit()
|
||||
if not body is Player:
|
||||
body.queue_free()
|
||||
|
||||
|
||||
@@ -3,10 +3,16 @@ var bulletArray = []
|
||||
var totalAllowedBullets = 7
|
||||
var bullet = preload("res://Scenes/bullet.tscn")
|
||||
@onready var game: Node2D = $".."
|
||||
@onready var triggers = $"../triggers"
|
||||
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
pass # Replace with function body.
|
||||
if triggers:
|
||||
for obj in triggers.get_children():
|
||||
if obj is Trigger:
|
||||
obj.areaTriggerSignal.connect(game._on_trigger)
|
||||
|
||||
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
extends Area2D
|
||||
class_name Trigger extends Area2D
|
||||
|
||||
@export var effect := "destroy"
|
||||
|
||||
|
||||
Reference in new issue
Block a user