Set up player movement and collision detection.

This commit is contained in:
Tanner Van Teeffelen 2025-12-30 22:13:14 -05:00
parent a19e85e101
commit 21037cd792
14 changed files with 247 additions and 9 deletions

29
Scenes/main.tscn Normal file

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

20
Scenes/player.tscn Normal file
View file

@ -0,0 +1,20 @@
[gd_scene load_steps=4 format=3 uid="uid://syx6ov00a585"]
[ext_resource type="Script" uid="uid://ohb2t6o7wr1u" path="res://Scripts/player.gd" id="1_cvnsp"]
[ext_resource type="Texture2D" uid="uid://btqnhg54e1p66" path="res://Sprites/png/spritesheet.png" id="2_6t5aa"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_jjgbg"]
size = Vector2(16, 16)
[node name="Player" type="CharacterBody2D"]
script = ExtResource("1_cvnsp")
[node name="Sprite" type="Sprite2D" parent="."]
texture = ExtResource("2_6t5aa")
hframes = 2
vframes = 2
[node name="Camera" type="Camera2D" parent="."]
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
shape = SubResource("RectangleShape2D_jjgbg")

3
Scripts/al_globals.gd Normal file
View file

@ -0,0 +1,3 @@
extends Node
var gravity : float = 500

View file

@ -0,0 +1 @@
uid://c7qrd8m61xgmq

41
Scripts/player.gd Normal file
View file

@ -0,0 +1,41 @@
extends CharacterBody2D
@onready var tile_layer : TileMapLayer = $"../TileMap"
@export var acceleration : float = 50
@export var braking : float = 20
@export var move_speed : float = 100
var move_input : float
func _physics_process(delta: float) -> void:
#Horizontal movement.
move_input = Input.get_axis("move_left", "move_right")
if move_input != 0:
velocity.x = lerp(velocity.x, move_input * move_speed, acceleration * delta)
else:
velocity.x = lerp(velocity.x, 0.0, braking * delta)
#Vertical movement.
velocity.y += al_globals.gravity * delta
if Input.is_action_just_pressed("flip"):
al_globals.gravity *= -1
move_and_slide() #Allow physics control.
spike_detection() #Checks for tiles with the "Spike" custom data enabled.
func spike_detection() -> void:
#Adapted from Godot forum post.
#Get current tile, and then put the data from it into a variable.
var tile_coord = tile_layer.local_to_map(tile_layer.to_local(global_position))
var tile_data = tile_layer.get_cell_tile_data(tile_coord)
#If it has data, check for it. If it has the "spike" custom data, ensure the gravity is set back to normal, and then reset the level.
if tile_data:
var custom_data = tile_data.get_custom_data("spike")
if custom_data == true:
if al_globals.gravity < 0:
al_globals.gravity *= -1
get_tree().change_scene_to_file("res://Scenes/main.tscn")

1
Scripts/player.gd.uid Normal file
View file

@ -0,0 +1 @@
uid://ohb2t6o7wr1u

BIN
Sprites/af/spritesheet.af Normal file

Binary file not shown.

BIN
Sprites/png/spritesheet.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

View file

@ -2,16 +2,16 @@
importer="texture" importer="texture"
type="CompressedTexture2D" type="CompressedTexture2D"
uid="uid://dd6u124s0hsde" uid="uid://btqnhg54e1p66"
path="res://.godot/imported/icon.svg-218a8f2b3041327d8a5756f3a245f83b.ctex" path="res://.godot/imported/spritesheet.png-a2ccbc152119c25d94640044aa7d9968.ctex"
metadata={ metadata={
"vram_texture": false "vram_texture": false
} }
[deps] [deps]
source_file="res://icon.svg" source_file="res://Sprites/png/spritesheet.png"
dest_files=["res://.godot/imported/icon.svg-218a8f2b3041327d8a5756f3a245f83b.ctex"] dest_files=["res://.godot/imported/spritesheet.png-a2ccbc152119c25d94640044aa7d9968.ctex"]
[params] [params]
@ -38,6 +38,3 @@ process/hdr_as_srgb=false
process/hdr_clamp_exposure=false process/hdr_clamp_exposure=false
process/size_limit=0 process/size_limit=0
detect_3d/compress_to=1 detect_3d/compress_to=1
svg/scale=1.0
editor/scale_with_editor_scale=false
editor/convert_colors_with_editor_theme=false

View file

@ -1 +0,0 @@
<svg xmlns="http://www.w3.org/2000/svg" width="128" height="128"><rect width="124" height="124" x="2" y="2" fill="#363d52" stroke="#212532" stroke-width="4" rx="14"/><g fill="#fff" transform="translate(12.322 12.322)scale(.101)"><path d="M105 673v33q407 354 814 0v-33z"/><path fill="#478cbf" d="m105 673 152 14q12 1 15 14l4 67 132 10 8-61q2-11 15-15h162q13 4 15 15l8 61 132-10 4-67q3-13 15-14l152-14V427q30-39 56-81-35-59-83-108-43 20-82 47-40-37-88-64 7-51 8-102-59-28-123-42-26 43-46 89-49-7-98 0-20-46-46-89-64 14-123 42 1 51 8 102-48 27-88 64-39-27-82-47-48 49-83 108 26 42 56 81zm0 33v39c0 276 813 276 814 0v-39l-134 12-5 69q-2 10-14 13l-162 11q-12 0-16-11l-10-65H446l-10 65q-4 11-16 11l-162-11q-12-3-14-13l-5-69z"/><path d="M483 600c0 34 58 34 58 0v-86c0-34-58-34-58 0z"/><circle cx="725" cy="526" r="90"/><circle cx="299" cy="526" r="90"/></g><g fill="#414042" transform="translate(12.322 12.322)scale(.101)"><circle cx="307" cy="532" r="60"/><circle cx="717" cy="532" r="60"/></g></svg>

Before

Width:  |  Height:  |  Size: 995 B

View file

@ -11,9 +11,33 @@ config_version=5
[application] [application]
config/name="TheFlipSide" config/name="TheFlipSide"
run/main_scene="res://Scenes/main.tscn"
config/features=PackedStringArray("4.5", "Forward Plus") config/features=PackedStringArray("4.5", "Forward Plus")
config/icon="res://icon.svg"
[autoload]
al_globals="*res://Scripts/al_globals.gd"
[dotnet] [dotnet]
project/assembly_name="TheFlipSide" project/assembly_name="TheFlipSide"
[input]
flip={
"deadzone": 0.2,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":32,"key_label":0,"unicode":32,"location":0,"echo":false,"script":null)
]
}
move_left={
"deadzone": 0.2,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":65,"key_label":0,"unicode":97,"location":0,"echo":false,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194319,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null)
]
}
move_right={
"deadzone": 0.2,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":68,"key_label":0,"unicode":100,"location":0,"echo":false,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194321,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null)
]
}