week 2 preparation, scene manager
This commit is contained in:
1 parent
76b6721c42
commit
c9e8aaf6ef
8 files changed
+159
-41
No files matched your search
@@ -1 +1,14 @@
|
||||
class_name Crate extends RigidBody2D
|
||||
|
||||
var teleport_target: Vector2 = Vector2.ZERO
|
||||
var should_teleport: bool = false
|
||||
|
||||
func teleport_to(position: Vector2):
|
||||
teleport_target = position
|
||||
should_teleport = true
|
||||
|
||||
# called during physics processing, allowing you to safely read and modify the simulation state of a RigidBody2D
|
||||
func _integrate_forces(state: PhysicsDirectBodyState2D):
|
||||
if should_teleport:
|
||||
state.transform = Transform2D.IDENTITY.translated(teleport_target)
|
||||
should_teleport = false
|
||||
@@ -1,23 +1,50 @@
|
||||
extends Node2D
|
||||
class_name GameController extends Node2D
|
||||
|
||||
var crateTotal = 3
|
||||
var crateTotal:int = 0
|
||||
signal destroySignal(body)
|
||||
signal teleportSignal(body)
|
||||
signal levelChangeSignal(level)
|
||||
|
||||
var timer := Timer.new()
|
||||
var timeAvailable := 10
|
||||
var currentScene:String = "res://scenes/game.tscn"
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
pass # Replace with function body.
|
||||
add_child(timer)
|
||||
timer.wait_time = 1
|
||||
timer.one_shot = false
|
||||
timer.connect("timeout", secondCounter)
|
||||
timer.start()
|
||||
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
func _process(delta: float) -> void:
|
||||
pass
|
||||
|
||||
|
||||
func _on_trigger(body: Variant, effect) -> void:
|
||||
func secondCounter()->void:
|
||||
timeAvailable -=1
|
||||
if timeAvailable<=0:
|
||||
print("You Lose Baby!!")
|
||||
levelChangeSignal.emit(currentScene)
|
||||
|
||||
func _on_trigger(body: Variant, effect:String, trigger:Trigger) -> void:
|
||||
print("GC knows trigger...."+effect)
|
||||
if body is Crate:
|
||||
match effect:
|
||||
"destroy":
|
||||
crateTotal -=1
|
||||
if crateTotal <=0:
|
||||
print("You WON!!!")
|
||||
body.queue_free()
|
||||
destroySignal.emit(body)
|
||||
"teleport":
|
||||
teleportSignal.emit(body)
|
||||
if body is Player:
|
||||
match effect:
|
||||
"timeExtend":
|
||||
timeAvailable +=5
|
||||
destroySignal.emit(trigger)
|
||||
|
||||
func updateCrates(total:int)->void:
|
||||
crateTotal = total
|
||||
print("Crate total: "+str(crateTotal))
|
||||
if crateTotal <=0:
|
||||
print("You WON!!!")
|
||||
levelChangeSignal.emit(currentScene)
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
extends CharacterBody2D
|
||||
class_name Player extends CharacterBody2D
|
||||
|
||||
|
||||
const SPEED = 300.0
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
class_name SceneManager extends Node2D
|
||||
@onready var level: Node2D = $"../Level"
|
||||
@onready var crates: Node2D = $"../Crates"
|
||||
@onready var triggers: Node2D = $"../Triggers"
|
||||
@onready var game:GameController = $".."
|
||||
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
buildLevel()
|
||||
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
func _process(delta: float) -> void:
|
||||
pass
|
||||
|
||||
func buildLevel()->void:
|
||||
#Prepare the game controller with all
|
||||
#needed information
|
||||
if triggers:
|
||||
updateTriggers()
|
||||
if crates:
|
||||
updateCrates()
|
||||
|
||||
#Listen to signals coming from game controller
|
||||
#that have to do with updating the stage
|
||||
game.destroySignal.connect(destroy)
|
||||
game.teleportSignal.connect(teleport)
|
||||
game.levelChangeSignal.connect(loadLevel)
|
||||
|
||||
#Finds all Triggers and wires signals to game controller
|
||||
func updateTriggers()->void:
|
||||
for obj in triggers.get_children():
|
||||
if obj is Trigger:
|
||||
if not obj.AreaTrigger.is_connected(game._on_trigger):
|
||||
obj.AreaTrigger.connect(game._on_trigger)
|
||||
|
||||
#Finds all crates and wires number count to game controller
|
||||
func updateCrates()->void:
|
||||
var totalCrates =0
|
||||
for obj in crates.get_children():
|
||||
if obj is Crate:
|
||||
if not obj.tree_exited.is_connected(updateCrates):
|
||||
obj.tree_exited.connect(updateCrates)
|
||||
totalCrates +=1
|
||||
game.updateCrates(totalCrates)
|
||||
|
||||
func loadLevel(level:String)->void:
|
||||
get_tree().call_deferred("change_scene_to_file", level)
|
||||
|
||||
func destroy(body:Variant)->void:
|
||||
body.queue_free()
|
||||
|
||||
func teleport(body:Variant)->void:
|
||||
if body is Crate:
|
||||
#send it
|
||||
var viewport_size = get_viewport().get_visible_rect().size
|
||||
var random_x = randf_range(0, viewport_size.x)
|
||||
var random_y = randf_range(0, viewport_size.y)
|
||||
var newPosition = Vector2(random_x, random_y)
|
||||
body.teleport_to(newPosition)
|
||||
@@ -0,0 +1 @@
|
||||
uid://b8iyniv2mu5mi
|
||||
+3
-3
@@ -1,6 +1,6 @@
|
||||
extends Area2D
|
||||
class_name Trigger extends Area2D
|
||||
|
||||
signal AreaTrigger(body, effect)
|
||||
signal AreaTrigger(body, effect, trigger)
|
||||
@export var effect="destroy"
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
@@ -15,4 +15,4 @@ func _process(delta: float) -> void:
|
||||
|
||||
func _on_body_entered(body: Node2D) -> void:
|
||||
print("Trigger activated")
|
||||
AreaTrigger.emit(body, effect)
|
||||
AreaTrigger.emit(body, effect, self)
|
||||
Reference in new issue
Block a user