greandes, dyanmic instantiation, statemachine

This commit is contained in:
torontocaper committed 2026-05-04 21:01:53 -04:00
1 parent 4dada35f76
commit ed9a99e192
17 files changed
+448 -25

No files matched your search

+11
View File
@@ -0,0 +1,11 @@
class_name Crate 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
View File
@@ -0,0 +1 @@
uid://vwu0qtoxeqcn
+9 -3
View File
@@ -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,10 @@ 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("destructible") and intentMessage == "destroy":
destroySignal.emit(body)
func totalCrates(numberOfCrates:int)->void:
print("GC knows total crates: "+str(numberOfCrates))
if numberOfCrates <=0:
print("You WON!!!!")
+24
View File
@@ -0,0 +1,24 @@
class_name Grenade extends RigidBody2D
var timer = Timer.new()
func _ready() -> void:
add_child(timer)
#how long
timer.wait_time = 2
#one time onlu
timer.one_shot = true
timer.connect("timeout",explode)
timer.start()
func explode()->void:
print('ima spolde')
self.queue_free()
func _on_body_entered(body: Node) -> void:
print("collision w Grenade!!")
if body.is_in_group("destructible"):
body.queue_free()
explode()
+1
View File
@@ -0,0 +1 @@
uid://86lxae5ujqvq
+49 -4
View File
@@ -3,20 +3,33 @@ class_name Player extends CharacterBody2D
const SPEED = 300.0
const JUMP_VELOCITY = -400.0
@onready var right_spawn: Marker2D = $RightSpawn
@onready var left_spawn: Marker2D = $LeftSpawn
@onready var player_graphic: AnimatedSprite2D = $PlayerGraphic
var direction
enum FaceDirection{LEFT,RIGHT}
var facing:FaceDirection = FaceDirection.RIGHT
enum PlayerState{IDLE,RUNNING}
var current_player_state:PlayerState = PlayerState.IDLE
func _physics_process(delta: float) -> void:
handle_input()
handle_state()
handle_animation()
# 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:
@@ -28,3 +41,35 @@ func _physics_process(delta: float) -> void:
if c.get_collider() is RigidBody2D:
#deliver the impact
c.get_collider().apply_central_impulse(-c.get_normal() * 100)
func handle_input()->void:
if Input.is_action_just_pressed("Chuck"):
print("chuck a grenade")
%SceneManager.makeGrenade(right_spawn.global_transform, 1)
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
player_graphic.flip_h = true
if direction>0:
facing = FaceDirection.RIGHT
player_graphic.flip_h = false
func handle_state()->void:
match current_player_state:
PlayerState.IDLE when velocity.x !=0:
#change to running
current_player_state = PlayerState.RUNNING
PlayerState.RUNNING when velocity.x ==0:
current_player_state = PlayerState.IDLE
func handle_animation()->void:
match current_player_state:
PlayerState.IDLE:
player_graphic.play("idle")
PlayerState.RUNNING:
player_graphic.play("run")
+34
View File
@@ -0,0 +1,34 @@
class_name SceneManager extends Node2D
@onready var game: Node2D = $".."
@onready var crates: Node2D = $"../Crates"
var grenade = preload("res://Scenes/grenade.tscn")
func _ready() -> void:
print("Scene manager is ready!")
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)
func makeGrenade(_greandePosition, _grenadeDirection)->void:
print("SM wants to make a grenade")
var myGrenade:Grenade = grenade.instantiate()
owner.add_child(myGrenade)
myGrenade.transform = _greandePosition
myGrenade.apply_central_impulse(Vector2(_grenadeDirection, -1)*200)
+1
View File
@@ -0,0 +1 @@
uid://qhll446powxt