added shoving
This commit is contained in:
1 parent
808a961af3
commit
416fc6100b
12 files changed
+226
-71
No files matched your search
@@ -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
|
||||
@@ -0,0 +1 @@
|
||||
uid://dn8v5sbqg05v3
|
||||
@@ -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)
|
||||
@@ -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!")
|
||||
@@ -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)
|
||||
File renamed without changes.
@@ -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()
|
||||
@@ -0,0 +1 @@
|
||||
uid://sj5x7sv6o6yb
|
||||
Reference in new issue
Block a user