diff --git a/project.godot b/project.godot index ea8ea15..aa14200 100644 --- a/project.godot +++ b/project.godot @@ -15,6 +15,14 @@ run/main_scene="uid://cueixogtk70go" config/features=PackedStringArray("4.5", "Forward Plus") 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] folder_colors={ diff --git a/scenes/game.tscn b/scenes/game.tscn index 5582db5..fc52b0d 100644 --- a/scenes/game.tscn +++ b/scenes/game.tscn @@ -20,8 +20,10 @@ script = ExtResource("2_iywne") [node name="Player" parent="." instance=ExtResource("3_lnu2h")] position = Vector2(84, 58) -BUMP_POWER = 50 +BUMP_POWER = null SHOVE_POWER = 400 +ACCELERATION = 10 +HARD_GRAVITY = 2 [node name="Block" type="StaticBody2D" parent="."] position = Vector2(136, 339) diff --git a/scenes/player.tscn b/scenes/player.tscn index bdca377..e4132c4 100644 --- a/scenes/player.tscn +++ b/scenes/player.tscn @@ -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="Texture2D" uid="uid://dr3rp5hv7rexv" path="res://graphics/player_idle/Player Idle 48x48.png" id="2_g2els"] @@ -9,6 +9,10 @@ radius = 6.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"] atlas = ExtResource("2_g2els") region = Rect2(0, 0, 48, 48) @@ -53,14 +57,6 @@ region = Rect2(432, 0, 48, 48) atlas = ExtResource("3_dqkch") 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"] atlas = ExtResource("4_qlg0r") region = Rect2(0, 0, 48, 48) @@ -97,6 +93,14 @@ region = Rect2(336, 0, 48, 48) animations = [{ "frames": [{ "duration": 1.0, +"texture": SubResource("AtlasTexture_oprun") +}], +"loop": true, +"name": &"fall", +"speed": 15.0 +}, { +"frames": [{ +"duration": 1.0, "texture": SubResource("AtlasTexture_qhqgy") }, { "duration": 1.0, @@ -133,14 +137,8 @@ animations = [{ "frames": [{ "duration": 1.0, "texture": SubResource("AtlasTexture_jej6c") -}, { -"duration": 1.0, -"texture": SubResource("AtlasTexture_f1ej7") -}, { -"duration": 1.0, -"texture": SubResource("AtlasTexture_oprun") }], -"loop": false, +"loop": true, "name": &"jump", "speed": 15.0 }, { @@ -195,8 +193,10 @@ position = Vector2(10, -4) [node name="LeftSpawn" type="Marker2D" parent="."] position = Vector2(-10, -4) -[node name="AnimatedSprite2D" type="AnimatedSprite2D" parent="."] +[node name="PlayerSprite" type="AnimatedSprite2D" parent="."] sprite_frames = SubResource("SpriteFrames_3vyb7") -animation = &"run" +animation = &"jump" autoplay = "idle" -frame_progress = 0.6462772 + +[node name="Camera2D" type="Camera2D" parent="."] +position_smoothing_enabled = true diff --git a/scripts/player.gd b/scripts/player.gd index 721f105..effd5d7 100644 --- a/scripts/player.gd +++ b/scripts/player.gd @@ -4,10 +4,14 @@ const SPEED = 300.0 const JUMP_VELOCITY = -600.0 enum FaceDirection{LEFT, RIGHT} +enum State{IDLE, WALK, JUMP, FALLING} -@export var BUMP_POWER = 50 -@export var SHOVE_POWER = 200 +@export var BUMP_POWER := 50 +@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 direction : float = 0.0 var jump_buffer_timer : Timer @@ -18,6 +22,7 @@ var push_enabled : bool = false @onready var left_cast: RayCast2D = $LeftCast @onready var right_spawn: Marker2D = $RightSpawn @onready var left_spawn: Marker2D = $LeftSpawn +@onready var player_sprite: AnimatedSprite2D = $PlayerSprite func _ready() -> void: jump_buffer_timer = Timer.new() @@ -28,15 +33,17 @@ func _physics_process(delta: float) -> void: handle_movement(delta) move_and_slide() handle_collisions() + update_state() + update_animation() func handle_input() -> void: # Handle jumping. if Input.is_action_just_pressed("jump"): jump_buffer_timer.start(0.1) - if is_on_floor() and jump_buffer_timer.time_left > 0: velocity.y = JUMP_VELOCITY + current_state = State.JUMP jump_buffer_timer.stop() # Set direction. @@ -55,8 +62,8 @@ func handle_input() -> void: FaceDirection.LEFT: shove_direction = -1 push_target.apply_central_impulse(Vector2(shove_direction, 0) * SHOVE_POWER) - - # Handle shooting.eee + + # Handle shooting if Input.is_action_just_pressed("shoot"): print("pew-pew") match facing: @@ -66,7 +73,8 @@ func handle_input() -> void: FaceDirection.LEFT: %SceneManager.make_bullet(left_spawn.global_transform, -700) print("shoot left") - + + # Handle throwing grenades if Input.is_action_just_pressed("throw"): print("grenade!") match facing: @@ -78,15 +86,23 @@ func handle_input() -> void: print("throwing left") func handle_movement(delta) -> void: - # Left-right movement. + # Left-right movement. Use acceleration for smoothing if direction: - velocity.x = direction * SPEED + velocity.x = move_toward(velocity.x, SPEED * direction, ACCELERATION) else: - velocity.x = move_toward(velocity.x, 0, SPEED) - - # Add gravity. + velocity.x = move_toward(velocity.x, 0, ACCELERATION) + 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: @@ -110,3 +126,45 @@ func handle_collisions() -> void: if not right_cast.is_colliding() and not left_cast.is_colliding(): 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")