MarchTTLGame/scripts/player.gd

107 lines
3.0 KiB
GDScript3
Raw Normal View History

class_name Player extends CharacterBody2D
2026-03-09 21:11:08 +00:00
@onready var right_gun: Marker2D = $"Right Gun"
@onready var left_gun: Marker2D = $"Left Gun"
@onready var player_graphic: AnimatedSprite2D = $PlayerGraphic
2026-03-09 21:11:08 +00:00
const SPEED = 300
const JUMP_VELOCITY = -400.0
const BUMP_POWER = 1000
enum FaceDirection{LEFT, RIGHT}
var facing:FaceDirection = FaceDirection.RIGHT
var direction
enum PlayerState{IDLE,RUNNING,JUMPING,FALLING}
var current_player_state:PlayerState = PlayerState.IDLE
var jumpUp:bool = false
2026-03-09 21:11:08 +00:00
func _physics_process(delta: float) -> void:
#Handle Shoot
handle_input()
handle_movement(delta)
handle_state()
handle_animation()
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
move_and_slide()
handle_collisions()
func handle_input()->void:
# Handle jump.
direction = Input.get_axis("ui_left", "ui_right")
if direction <0:
player_graphic.flip_h = true
if direction >0:
player_graphic.flip_h = false
if Input.is_action_just_pressed("ui_accept") :
velocity.y = JUMP_VELOCITY
current_player_state = PlayerState.JUMPING
jumpUp = true
if Input.is_action_just_pressed("Shoot"):
print("player wants to shoot")
match facing:
FaceDirection.RIGHT:
print("shoot to the right")
%SceneManager.makeBullet(right_gun.global_transform, 700)
FaceDirection.LEFT:
print("shoot to the left")
%SceneManager.makeBullet(left_gun.global_transform, -700)
func handle_state()->void:
match current_player_state:
PlayerState.IDLE when velocity.x !=0:
current_player_state = PlayerState.RUNNING
PlayerState.RUNNING when velocity.x ==0:
current_player_state = PlayerState.IDLE
PlayerState.JUMPING when velocity.y > 0:
current_player_state = PlayerState.FALLING
PlayerState.FALLING when is_on_floor():
if velocity.x ==0:
current_player_state = PlayerState.IDLE
if velocity.x !=0:
current_player_state = PlayerState.RUNNING
func handle_animation()->void:
match current_player_state:
PlayerState.IDLE:
player_graphic.play("idle")
PlayerState.RUNNING:
player_graphic.play("running")
PlayerState.JUMPING:
if jumpUp:
player_graphic.play("jumping")
PlayerState.FALLING:
player_graphic.play("falling")
func handle_movement(delta)->void:
# Add the gravity.
if not is_on_floor():
velocity += get_gravity() * delta
2026-03-09 21:11:08 +00:00
if direction:
velocity.x = direction * SPEED
if direction >0:
facing=FaceDirection.RIGHT
if direction <0:
facing=FaceDirection.LEFT
2026-03-09 21:11:08 +00:00
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
func handle_collisions()->void:
2026-03-09 21:11:08 +00:00
for i in get_slide_collision_count():
var c = get_slide_collision(i)
if c.get_collider() is RigidBody2D:
c.get_collider().apply_central_impulse(-c.get_normal()*BUMP_POWER)
if c.get_collider() is NPCCharector:
var myCharector:NPCCharector = c.get_collider()
var direction = -c.get_normal()*BUMP_POWER
myCharector.knockBack(direction)
func _on_animation_finished() -> void:
match current_player_state:
PlayerState.JUMPING:
jumpUp = false