added shoving

This commit is contained in:
edgul 2026-04-27 21:06:17 -04:00
parent 808a961af3
commit 416fc6100b
12 changed files with 226 additions and 71 deletions

View File

@ -21,6 +21,32 @@ folder_colors={
"res://scripts/": "red"
}
[input]
shove={
"deadzone": 0.2,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":70,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null)
]
}
jump={
"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)
, 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":87,"key_label":0,"unicode":119,"location":0,"echo":false,"script":null)
]
}
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)
]
}
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":4194321,"key_label":0,"unicode":0,"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":68,"key_label":0,"unicode":100,"location":0,"echo":false,"script":null)
]
}
[physics]
3d/physics_engine="Jolt Physics"

13
scenes/brown_box.tscn Normal file
View File

@ -0,0 +1,13 @@
[gd_scene format=3 uid="uid://wt8o0uyqblsv"]
[ext_resource type="Script" uid="uid://dn8v5sbqg05v3" path="res://scripts/brown_box.gd" id="1_g34fy"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_o3qty"]
[node name="brownBox" type="RigidBody2D" unique_id=143097810 groups=["destructable"]]
script = ExtResource("1_g34fy")
metadata/_edit_group_ = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="." unique_id=274978092]
shape = SubResource("RectangleShape2D_o3qty")
debug_color = Color(0.8449452, 0.37733862, 0.08704034, 0.41960785)

View File

@ -1,49 +1,26 @@
[gd_scene format=3 uid="uid://d7n4nyeox11q"]
[ext_resource type="Script" uid="uid://c6cnqclwk4lcb" path="res://scripts/game_controller.gd" id="1_bj22s"]
[ext_resource type="Script" uid="uid://dlmf1p0vfitcx" path="res://scripts/character.gd" id="1_o3qty"]
[ext_resource type="PackedScene" uid="uid://wt8o0uyqblsv" path="res://scenes/brown_box.tscn" id="2_ca7g5"]
[ext_resource type="Script" uid="uid://sj5x7sv6o6yb" path="res://scripts/scene_manager.gd" id="2_k3dxm"]
[ext_resource type="PackedScene" uid="uid://bhsvqyhsemekq" path="res://scenes/player.tscn" id="3_k3dxm"]
[ext_resource type="Script" uid="uid://gkpbb5sf4gu7" path="res://scripts/area_2d.gd" id="3_v0i7m"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_elsnr"]
size = Vector2(244, 20)
[sub_resource type="RectangleShape2D" id="RectangleShape2D_o3qty"]
[sub_resource type="WorldBoundaryShape2D" id="WorldBoundaryShape2D_elsnr"]
[sub_resource type="CapsuleShape2D" id="CapsuleShape2D_elsnr"]
[sub_resource type="CircleShape2D" id="CircleShape2D_bj22s"]
radius = 20.880613
[sub_resource type="RectangleShape2D" id="RectangleShape2D_elsnr"]
size = Vector2(244, 20)
[node name="GameNode" type="Node2D" unique_id=2127085168]
position = Vector2(0, -1)
script = ExtResource("1_bj22s")
[node name="platform" type="StaticBody2D" parent="." unique_id=1848808292]
position = Vector2(483, 318)
constant_linear_velocity = Vector2(15, 0)
metadata/_edit_group_ = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="platform" unique_id=79089169]
shape = SubResource("RectangleShape2D_elsnr")
[node name="brownBox" type="RigidBody2D" parent="." unique_id=473138492]
position = Vector2(442, 281)
metadata/_edit_group_ = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="brownBox" unique_id=1978306212]
shape = SubResource("RectangleShape2D_o3qty")
debug_color = Color(0.8449452, 0.37733862, 0.08704034, 0.41960785)
[node name="yellowBox" type="RigidBody2D" parent="." unique_id=413950760]
position = Vector2(498, 276)
rotation = 1.0476488
metadata/_edit_group_ = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="yellowBox" unique_id=1759041685]
shape = SubResource("RectangleShape2D_o3qty")
debug_color = Color(0.70038986, 0.49176556, 0.052423634, 0.41960785)
[node name="SceneManager" type="Node2D" parent="." unique_id=1765894074]
unique_name_in_owner = true
script = ExtResource("2_k3dxm")
[node name="worldFloor" type="StaticBody2D" parent="." unique_id=376228153]
position = Vector2(446, 437)
@ -52,14 +29,8 @@ metadata/_edit_group_ = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="worldFloor" unique_id=1759461926]
shape = SubResource("WorldBoundaryShape2D_elsnr")
[node name="character" type="CharacterBody2D" parent="." unique_id=1476507768]
[node name="player" parent="." unique_id=395957349 instance=ExtResource("3_k3dxm")]
position = Vector2(555, 272)
script = ExtResource("1_o3qty")
metadata/_edit_group_ = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="character" unique_id=130782569]
shape = SubResource("CapsuleShape2D_elsnr")
debug_color = Color(0.33339813, 0.6108008, 0.35667416, 0.41960785)
[node name="area" type="Area2D" parent="." unique_id=2033836703]
position = Vector2(638, 351)
@ -70,5 +41,56 @@ metadata/_edit_group_ = true
shape = SubResource("CircleShape2D_bj22s")
debug_color = Color(0.37550476, 0.5048963, 0.9171919, 0.41960785)
[node name="crates" type="Node2D" parent="." unique_id=1870478446]
[node name="brownBox" parent="crates" unique_id=143097810 instance=ExtResource("2_ca7g5")]
position = Vector2(409, 222)
[node name="brownBox2" parent="crates" unique_id=1038663129 instance=ExtResource("2_ca7g5")]
position = Vector2(436, 166)
[node name="brownBox4" parent="crates" unique_id=1193856484 instance=ExtResource("2_ca7g5")]
position = Vector2(472, 211)
[node name="brownBox5" parent="crates" unique_id=2032058715 instance=ExtResource("2_ca7g5")]
position = Vector2(460, 104)
[node name="brownBox3" parent="crates" unique_id=170891935 instance=ExtResource("2_ca7g5")]
position = Vector2(498, 155)
[node name="platforms" type="Node2D" parent="." unique_id=637172238]
[node name="platform" type="StaticBody2D" parent="platforms" unique_id=1848808292]
position = Vector2(483, 318)
constant_linear_velocity = Vector2(15, 0)
metadata/_edit_group_ = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="platforms/platform" unique_id=79089169]
shape = SubResource("RectangleShape2D_elsnr")
[node name="platform2" type="StaticBody2D" parent="platforms" unique_id=4214019]
position = Vector2(759, 245)
constant_linear_velocity = Vector2(15, 0)
metadata/_edit_group_ = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="platforms/platform2" unique_id=200788977]
shape = SubResource("RectangleShape2D_elsnr")
[node name="platform3" type="StaticBody2D" parent="platforms" unique_id=786694827]
position = Vector2(1007, 178)
constant_linear_velocity = Vector2(15, 0)
metadata/_edit_group_ = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="platforms/platform3" unique_id=1451698215]
shape = SubResource("RectangleShape2D_elsnr")
[node name="platform4" type="StaticBody2D" parent="platforms" unique_id=298215616]
position = Vector2(219, 383)
constant_linear_velocity = Vector2(15, 0)
metadata/_edit_group_ = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="platforms/platform4" unique_id=1906028968]
shape = SubResource("RectangleShape2D_elsnr")
[connection signal="body_entered" from="area" to="area" method="_on_body_entered"]
[connection signal="triggerActiveSignal" from="area" to="." method="_on_area_2d_trigger_active_signal"]

19
scenes/player.tscn Normal file
View File

@ -0,0 +1,19 @@
[gd_scene format=3 uid="uid://bhsvqyhsemekq"]
[ext_resource type="Script" uid="uid://dlmf1p0vfitcx" path="res://scripts/player.gd" id="1_3vyb7"]
[sub_resource type="CapsuleShape2D" id="CapsuleShape2D_elsnr"]
[node name="player" type="CharacterBody2D" unique_id=395957349]
script = ExtResource("1_3vyb7")
metadata/_edit_group_ = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="." unique_id=2113735481]
shape = SubResource("CapsuleShape2D_elsnr")
debug_color = Color(0.33339813, 0.6108008, 0.35667416, 0.41960785)
[node name="raycastRight" type="RayCast2D" parent="." unique_id=1050681815]
target_position = Vector2(20, 0)
[node name="raycastLeft" type="RayCast2D" parent="." unique_id=1511802462]
target_position = Vector2(-20, 0)

11
scripts/brown_box.gd Normal file
View File

@ -0,0 +1,11 @@
class_name BrownBox extends RigidBody2D
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
pass

1
scripts/brown_box.gd.uid Normal file
View File

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

View File

@ -1,27 +0,0 @@
class_name Player extends CharacterBody2D
const SPEED = 300.0
const JUMP_VELOCITY = -400.0
func _physics_process(delta):
# Add the gravity.
if not is_on_floor():
velocity += get_gravity() * delta
# Handle jump.
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
velocity.y = JUMP_VELOCITY
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var direction = Input.get_axis("ui_left", "ui_right")
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
move_and_slide() # pre-calc next position from velocity
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() * 100)

View File

@ -1,14 +1,21 @@
extends Node2D
class_name GameController extends Node2D
signal destroy(body)
# Called when the node enters the scene tree for the first time.
func _ready():
get_window().grab_focus() # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
pass
func _on_area_2d_trigger_active_signal(body, intentMessage):
print("game controller received trigger")
if intentMessage == "destroy" and not body is Player:
body.queue_free()
if intentMessage == "destroy" and body.is_in_group("destructable"):
destroy.emit(body)
var crate_num : int = 0
func set_crate_num(count: int):
crate_num = count
print("number of crates: " + str(crate_num))
if crate_num <= 0:
print("You Win!")

53
scripts/player.gd Normal file
View File

@ -0,0 +1,53 @@
class_name Player extends CharacterBody2D
@onready var raycast_left: RayCast2D = $raycastLeft
@onready var raycast_right: RayCast2D = $raycastRight
const SPEED = 300.0
const JUMP_VELOCITY = -400.0
var direction : float = 0
enum FaceDirection { LEFT, RIGHT }
var facing: FaceDirection = FaceDirection.LEFT
var shove_target: RigidBody2D
func _physics_process(delta: float):
handle_input()
handle_movement(delta)
move_and_slide() # pre-calc next position from velocity
handle_collisions()
func handle_input():
if Input.is_action_just_pressed("shove"):
if shove_target:
var shove_normal = 1 if facing == FaceDirection.RIGHT else -1
shove_target.apply_central_impulse(Vector2(shove_normal,0)*700)
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = JUMP_VELOCITY
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
direction = Input.get_axis("left", "right")
if direction < 0:
facing = FaceDirection.LEFT
elif direction > 0:
facing = FaceDirection.RIGHT
func handle_movement(delta: float):
# Add the gravity.
if not is_on_floor():
velocity += get_gravity() * delta
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
func handle_collisions():
if raycast_right.is_colliding() and facing == FaceDirection.RIGHT:
shove_target = raycast_right.get_collider()
elif raycast_left.is_colliding() and facing == FaceDirection.LEFT:
shove_target = raycast_left.get_collider()
else:
shove_target = null
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() * 100)

29
scripts/scene_manager.gd Normal file
View File

@ -0,0 +1,29 @@
class_name SceneManager extends Node2D
@onready var game_node: Node2D = $".."
@onready var crates: Node2D = $"../crates"
func _ready() -> void:
print("scene manager is ready")
game_node.destroy.connect(destroy)
build_level()
func _process(delta: float) -> void:
pass
func build_level() -> void:
update_crates()
func update_crates() -> void:
var crate_total:int = 0
if crates:
for obj in crates.get_children():
if obj is BrownBox:
if not obj.tree_exited.is_connected(update_crates):
obj.tree_exited.connect(update_crates)
crate_total += 1
game_node.set_crate_num(crate_total)
func destroy(body):
if body is BrownBox:
body.queue_free()

View File

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