added force push, scenemanager, raycasts
This commit is contained in:
1 parent
190dacd5bb
commit
f36db7cd2a
112 files changed
+1033
-47
No files matched your search
@@ -0,0 +1,27 @@
|
||||
class_name SceneManager extends Node2D
|
||||
@onready var game = $".."
|
||||
@onready var crates = $"../Crates"
|
||||
|
||||
func _ready() -> void:
|
||||
print("Hello")
|
||||
game.destroySignal.connect(destroy)
|
||||
buildLevel()
|
||||
|
||||
func destroy(body)->void:
|
||||
if body is Crate:
|
||||
body.queue_free()
|
||||
|
||||
func buildLevel()->void:
|
||||
updateCrates()
|
||||
|
||||
func updateCrates()->void:
|
||||
#is there a crate holder?
|
||||
var _crateTotal:int = 0
|
||||
if crates:
|
||||
for obj in crates.get_children():
|
||||
if obj is Crate:
|
||||
if not obj.tree_exited.is_connected(updateCrates):
|
||||
obj.tree_exited.connect(updateCrates)
|
||||
_crateTotal +=1
|
||||
print("Number of crates"+str(_crateTotal))
|
||||
game.totalCrates(_crateTotal)
|
||||
@@ -0,0 +1 @@
|
||||
uid://b2otvelhtq7en
|
||||
@@ -0,0 +1 @@
|
||||
class_name Crate extends RigidBody2D
|
||||
@@ -0,0 +1 @@
|
||||
uid://eb5ucqehuof
|
||||
@@ -1,5 +1,6 @@
|
||||
extends Node2D
|
||||
class_name GameController extends Node2D
|
||||
|
||||
signal destroySignal(body)
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
@@ -13,5 +14,9 @@ func _process(delta: float) -> void:
|
||||
|
||||
func _on_triggerSignal(body: Variant, intentMessage: Variant) -> void:
|
||||
print("GC knows about a trigger!")
|
||||
if not body is Player and intentMessage == "destroy":
|
||||
body.queue_free()
|
||||
if body.is_in_group("Destructable") and intentMessage == "destroy":
|
||||
destroySignal.emit(body)
|
||||
func totalCrates(numberOfCrates:int)->void:
|
||||
print("GC of crates"+str(numberOfCrates))
|
||||
if numberOfCrates <=0:
|
||||
print("You won. yay I am so happy for you. you")
|
||||
+54
-7
@@ -1,27 +1,74 @@
|
||||
class_name Player extends CharacterBody2D
|
||||
@onready var right_cast = $RightCast
|
||||
@onready var left_cast = $LeftCast
|
||||
|
||||
|
||||
const SPEED = 300.0
|
||||
const JUMP_VELOCITY = -400.0
|
||||
var direction
|
||||
enum FaceDirection{LEFT, RIGHT}
|
||||
var facing:FaceDirection = FaceDirection.RIGHT
|
||||
|
||||
var pushTarget:RigidBody2D
|
||||
var pushEnabled:bool = false
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
# Add the gravity.
|
||||
if not is_on_floor():
|
||||
velocity += get_gravity() * delta
|
||||
# Handle jump.
|
||||
handle_input()
|
||||
handle_movement(delta)
|
||||
move_and_slide()
|
||||
handle_collisions()
|
||||
|
||||
func handle_input()->void:
|
||||
if Input.is_action_just_pressed("Force Push") && pushEnabled:
|
||||
if not pushTarget is RigidBody2D:
|
||||
return
|
||||
#what direction to shove
|
||||
var shoveDirection:int
|
||||
match facing:
|
||||
FaceDirection.RIGHT:
|
||||
shoveDirection =1
|
||||
FaceDirection.LEFT:
|
||||
shoveDirection =-1
|
||||
pushTarget.apply_central_impulse(Vector2(shoveDirection,-1)* 1000)
|
||||
|
||||
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")
|
||||
direction = Input.get_axis("ui_left", "ui_right")
|
||||
if direction<0:
|
||||
facing = FaceDirection.LEFT
|
||||
if direction>0:
|
||||
facing = FaceDirection.RIGHT
|
||||
|
||||
func handle_movement(delta)->void:
|
||||
# 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)
|
||||
|
||||
move_and_slide()
|
||||
|
||||
func handle_collisions()->void:
|
||||
if right_cast.is_colliding() and facing==FaceDirection.RIGHT:
|
||||
print("Right is working")
|
||||
pushEnabled = true
|
||||
var collider = right_cast.get_collider()
|
||||
if collider is RigidBody2D:
|
||||
pushTarget = collider
|
||||
|
||||
if left_cast.is_colliding() and facing==FaceDirection.LEFT:
|
||||
print("Left is working")
|
||||
pushEnabled = true
|
||||
var collider = left_cast.get_collider()
|
||||
if collider is RigidBody2D:
|
||||
pushTarget = collider
|
||||
|
||||
if not right_cast.is_colliding() && not left_cast.is_colliding():
|
||||
pushEnabled = false
|
||||
for i in get_slide_collision_count():
|
||||
var c = get_slide_collision(i)
|
||||
if c.get_collider() is RigidBody2D:
|
||||
|
||||
Reference in new issue
Block a user