added state handling, animation, camera; changed resolution

This commit is contained in:
Adam Burns 2025-10-15 22:42:47 -04:00
parent c5b67bc07c
commit 14146a476d
4 changed files with 100 additions and 32 deletions

View File

@ -15,6 +15,14 @@ run/main_scene="uid://cueixogtk70go"
config/features=PackedStringArray("4.5", "Forward Plus") config/features=PackedStringArray("4.5", "Forward Plus")
config/icon="res://icon.svg" config/icon="res://icon.svg"
[display]
window/size/viewport_width=640
window/size/viewport_height=360
window/size/window_width_override=1280
window/size/window_height_override=720
window/stretch/mode="viewport"
[file_customization] [file_customization]
folder_colors={ folder_colors={

View File

@ -20,8 +20,10 @@ script = ExtResource("2_iywne")
[node name="Player" parent="." instance=ExtResource("3_lnu2h")] [node name="Player" parent="." instance=ExtResource("3_lnu2h")]
position = Vector2(84, 58) position = Vector2(84, 58)
BUMP_POWER = 50 BUMP_POWER = null
SHOVE_POWER = 400 SHOVE_POWER = 400
ACCELERATION = 10
HARD_GRAVITY = 2
[node name="Block" type="StaticBody2D" parent="."] [node name="Block" type="StaticBody2D" parent="."]
position = Vector2(136, 339) position = Vector2(136, 339)

View File

@ -1,4 +1,4 @@
[gd_scene load_steps=28 format=3 uid="uid://d3y1iqmpknpyo"] [gd_scene load_steps=27 format=3 uid="uid://d3y1iqmpknpyo"]
[ext_resource type="Script" uid="uid://d3hp5rjoph7hg" path="res://scripts/player.gd" id="1_3vyb7"] [ext_resource type="Script" uid="uid://d3hp5rjoph7hg" path="res://scripts/player.gd" id="1_3vyb7"]
[ext_resource type="Texture2D" uid="uid://dr3rp5hv7rexv" path="res://graphics/player_idle/Player Idle 48x48.png" id="2_g2els"] [ext_resource type="Texture2D" uid="uid://dr3rp5hv7rexv" path="res://graphics/player_idle/Player Idle 48x48.png" id="2_g2els"]
@ -9,6 +9,10 @@
radius = 6.0 radius = 6.0
height = 32.0 height = 32.0
[sub_resource type="AtlasTexture" id="AtlasTexture_oprun"]
atlas = ExtResource("3_dqkch")
region = Rect2(96, 0, 48, 48)
[sub_resource type="AtlasTexture" id="AtlasTexture_qhqgy"] [sub_resource type="AtlasTexture" id="AtlasTexture_qhqgy"]
atlas = ExtResource("2_g2els") atlas = ExtResource("2_g2els")
region = Rect2(0, 0, 48, 48) region = Rect2(0, 0, 48, 48)
@ -53,14 +57,6 @@ region = Rect2(432, 0, 48, 48)
atlas = ExtResource("3_dqkch") atlas = ExtResource("3_dqkch")
region = Rect2(0, 0, 48, 48) region = Rect2(0, 0, 48, 48)
[sub_resource type="AtlasTexture" id="AtlasTexture_f1ej7"]
atlas = ExtResource("3_dqkch")
region = Rect2(48, 0, 48, 48)
[sub_resource type="AtlasTexture" id="AtlasTexture_oprun"]
atlas = ExtResource("3_dqkch")
region = Rect2(96, 0, 48, 48)
[sub_resource type="AtlasTexture" id="AtlasTexture_a8ls1"] [sub_resource type="AtlasTexture" id="AtlasTexture_a8ls1"]
atlas = ExtResource("4_qlg0r") atlas = ExtResource("4_qlg0r")
region = Rect2(0, 0, 48, 48) region = Rect2(0, 0, 48, 48)
@ -97,6 +93,14 @@ region = Rect2(336, 0, 48, 48)
animations = [{ animations = [{
"frames": [{ "frames": [{
"duration": 1.0, "duration": 1.0,
"texture": SubResource("AtlasTexture_oprun")
}],
"loop": true,
"name": &"fall",
"speed": 15.0
}, {
"frames": [{
"duration": 1.0,
"texture": SubResource("AtlasTexture_qhqgy") "texture": SubResource("AtlasTexture_qhqgy")
}, { }, {
"duration": 1.0, "duration": 1.0,
@ -133,14 +137,8 @@ animations = [{
"frames": [{ "frames": [{
"duration": 1.0, "duration": 1.0,
"texture": SubResource("AtlasTexture_jej6c") "texture": SubResource("AtlasTexture_jej6c")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_f1ej7")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_oprun")
}], }],
"loop": false, "loop": true,
"name": &"jump", "name": &"jump",
"speed": 15.0 "speed": 15.0
}, { }, {
@ -195,8 +193,10 @@ position = Vector2(10, -4)
[node name="LeftSpawn" type="Marker2D" parent="."] [node name="LeftSpawn" type="Marker2D" parent="."]
position = Vector2(-10, -4) position = Vector2(-10, -4)
[node name="AnimatedSprite2D" type="AnimatedSprite2D" parent="."] [node name="PlayerSprite" type="AnimatedSprite2D" parent="."]
sprite_frames = SubResource("SpriteFrames_3vyb7") sprite_frames = SubResource("SpriteFrames_3vyb7")
animation = &"run" animation = &"jump"
autoplay = "idle" autoplay = "idle"
frame_progress = 0.6462772
[node name="Camera2D" type="Camera2D" parent="."]
position_smoothing_enabled = true

View File

@ -4,10 +4,14 @@ const SPEED = 300.0
const JUMP_VELOCITY = -600.0 const JUMP_VELOCITY = -600.0
enum FaceDirection{LEFT, RIGHT} enum FaceDirection{LEFT, RIGHT}
enum State{IDLE, WALK, JUMP, FALLING}
@export var BUMP_POWER = 50 @export var BUMP_POWER := 50
@export var SHOVE_POWER = 200 @export var SHOVE_POWER := 200
@export var ACCELERATION := 5
@export var HARD_GRAVITY := 100
var current_state: State = State.IDLE
var facing : FaceDirection = FaceDirection.RIGHT var facing : FaceDirection = FaceDirection.RIGHT
var direction : float = 0.0 var direction : float = 0.0
var jump_buffer_timer : Timer var jump_buffer_timer : Timer
@ -18,6 +22,7 @@ var push_enabled : bool = false
@onready var left_cast: RayCast2D = $LeftCast @onready var left_cast: RayCast2D = $LeftCast
@onready var right_spawn: Marker2D = $RightSpawn @onready var right_spawn: Marker2D = $RightSpawn
@onready var left_spawn: Marker2D = $LeftSpawn @onready var left_spawn: Marker2D = $LeftSpawn
@onready var player_sprite: AnimatedSprite2D = $PlayerSprite
func _ready() -> void: func _ready() -> void:
jump_buffer_timer = Timer.new() jump_buffer_timer = Timer.new()
@ -28,15 +33,17 @@ func _physics_process(delta: float) -> void:
handle_movement(delta) handle_movement(delta)
move_and_slide() move_and_slide()
handle_collisions() handle_collisions()
update_state()
update_animation()
func handle_input() -> void: func handle_input() -> void:
# Handle jumping. # Handle jumping.
if Input.is_action_just_pressed("jump"): if Input.is_action_just_pressed("jump"):
jump_buffer_timer.start(0.1) jump_buffer_timer.start(0.1)
if is_on_floor() and jump_buffer_timer.time_left > 0: if is_on_floor() and jump_buffer_timer.time_left > 0:
velocity.y = JUMP_VELOCITY velocity.y = JUMP_VELOCITY
current_state = State.JUMP
jump_buffer_timer.stop() jump_buffer_timer.stop()
# Set direction. # Set direction.
@ -56,7 +63,7 @@ func handle_input() -> void:
shove_direction = -1 shove_direction = -1
push_target.apply_central_impulse(Vector2(shove_direction, 0) * SHOVE_POWER) push_target.apply_central_impulse(Vector2(shove_direction, 0) * SHOVE_POWER)
# Handle shooting.eee # Handle shooting
if Input.is_action_just_pressed("shoot"): if Input.is_action_just_pressed("shoot"):
print("pew-pew") print("pew-pew")
match facing: match facing:
@ -67,6 +74,7 @@ func handle_input() -> void:
%SceneManager.make_bullet(left_spawn.global_transform, -700) %SceneManager.make_bullet(left_spawn.global_transform, -700)
print("shoot left") print("shoot left")
# Handle throwing grenades
if Input.is_action_just_pressed("throw"): if Input.is_action_just_pressed("throw"):
print("grenade!") print("grenade!")
match facing: match facing:
@ -78,15 +86,23 @@ func handle_input() -> void:
print("throwing left") print("throwing left")
func handle_movement(delta) -> void: func handle_movement(delta) -> void:
# Left-right movement. # Left-right movement. Use acceleration for smoothing
if direction: if direction:
velocity.x = direction * SPEED velocity.x = move_toward(velocity.x, SPEED * direction, ACCELERATION)
else: else:
velocity.x = move_toward(velocity.x, 0, SPEED) velocity.x = move_toward(velocity.x, 0, ACCELERATION)
# Add gravity.
if not is_on_floor(): if not is_on_floor():
velocity += get_gravity() * delta # Add gravity.
if current_state == State.JUMP:
# Character is jumping; apply normal gravity
velocity += get_gravity() * delta
if velocity.y > 0:
current_state = State.FALLING
else:
# Character falling; apply hard gravity
current_state = State.FALLING
velocity += get_gravity() * HARD_GRAVITY * delta
func handle_collisions() -> void: func handle_collisions() -> void:
@ -110,3 +126,45 @@ func handle_collisions() -> void:
if not right_cast.is_colliding() and not left_cast.is_colliding(): if not right_cast.is_colliding() and not left_cast.is_colliding():
push_enabled = false push_enabled = false
func update_state() -> void:
match current_state:
# If player is moving left or right
State.IDLE when velocity.x !=0:
current_state = State.WALK
# If player stops walking, or starts falling
State.WALK:
# If not moving left or right
if velocity.x == 0:
current_state = State.IDLE
# If falling
if not is_on_floor() and velocity.y > 0:
current_state = State.FALLING
# When jump peaks, we start to fall
State.JUMP when velocity.y > 0:
current_state = State.FALLING
# Player lands, either still or moving
State.FALLING when is_on_floor():
if velocity.x == 0:
current_state = State.IDLE
else:
current_state = State.WALK
func update_animation() -> void:
match facing:
FaceDirection.LEFT:
player_sprite.flip_h = true
FaceDirection.RIGHT:
player_sprite.flip_h = false
match current_state:
State.IDLE:
player_sprite.play("idle")
State.WALK:
player_sprite.play("run")
State.JUMP:
player_sprite.play("jump")
State.FALLING:
player_sprite.play("fall")