SeptemberGame/scripts/player.gd
2025-10-20 15:31:36 -04:00

117 lines
3.6 KiB
GDScript

extends CharacterBody2D
const SPEED = 300.0
const JUMP_VELOCITY = -400.0
#add a variable for momentum...t.w.
@export var acceleration:int = 8
@export var hard_gravity:float =1.5
var direction:float = 0
@export var BUMP_POWER = 100
enum FaceDirection{LEFT, RIGHT}
var facing:FaceDirection = FaceDirection.RIGHT
@onready var right_cast = $RightCast
@onready var left_cast = $LeftCast
var pushTarget:RigidBody2D
var pushEnabled:bool = false
@onready var right_spawn = $RightSpawn
@onready var left_spawn = $LeftSpawn
@onready var jump_buffer_timer: Timer = $JumpBufferTimer
#For State Machine.............t.w.
enum State{IDLE, RUNNING, JUMP, FALLING}
var current_state:State = State.IDLE
func _physics_process(delta):
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
handle_input()
handle_movement(delta)
move_and_slide()
handle_collisions()
func handle_input()->void:
# Handle jump.
if Input.is_action_just_pressed("jump") and is_on_floor():
jump_buffer_timer.start()
direction = Input.get_axis("moveLeft", "moveRight")
if direction <0:
facing=FaceDirection.LEFT
if direction >0:
facing=FaceDirection.RIGHT
if Input.is_action_just_pressed("shove") && pushEnabled:
print("I want to shove")
var shoveDirection:int
match facing:
FaceDirection.RIGHT:
shoveDirection = 1
FaceDirection.LEFT:
shoveDirection = -1
pushTarget.apply_central_impulse(Vector2(shoveDirection,0)*700)
if Input.is_action_just_pressed("shoot"):
print("Ima shoot")
match facing:
FaceDirection.RIGHT:
print("shoot right")
%SceneManager.makeBullet(right_spawn.global_transform, 700)
FaceDirection.LEFT:
print("shoot left")
%SceneManager.makeBullet(left_spawn.global_transform, -700)
if Input.is_action_just_pressed("chuck"):
print("ima chuck a grenade")
match facing:
FaceDirection.RIGHT:
%SceneManager.makeGrenade(right_spawn.global_transform, 1)
FaceDirection.LEFT:
%SceneManager.makeGrenade(left_spawn.global_transform, -1)
func handle_movement(_delta)->void:
#change to create some momentum.....t.w.
if direction == 0:
velocity.x = move_toward(velocity.x, 0, acceleration)
else:
velocity.x = move_toward(velocity.x, SPEED * direction, acceleration)
if current_state == State.JUMP:
#apply normal velocity
velocity += get_gravity() * _delta
if velocity.y>0:
current_state = State.FALLING
else:
#apply hard gravity, character falling
velocity += get_gravity() * hard_gravity * _delta
#add a buffer timer to the jump velocity............t.w.
if is_on_floor() && jump_buffer_timer.time_left >0:
velocity.y = JUMP_VELOCITY
#change state...........t.w.
current_state = State.JUMP
jump_buffer_timer.stop()
func handle_collisions()->void:
#handle world reactions
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 right_cast.is_colliding() && facing==FaceDirection.RIGHT:
var collider = right_cast.get_collider()
#check if this is okay
if collider is Node && collider is RigidBody2D && collider.is_in_group("pushable"):
pushTarget = collider
pushEnabled = true
if left_cast.is_colliding() && facing==FaceDirection.LEFT:
var collider = left_cast.get_collider()
if collider is Node && collider is RigidBody2D && collider.is_in_group("pushable"):
pushTarget = collider
pushEnabled = true
if not right_cast.is_colliding() && not left_cast.is_colliding():
pushEnabled = false