week 2 preparation, scene manager
This commit is contained in:
parent
76b6721c42
commit
c9e8aaf6ef
@ -5,7 +5,6 @@
|
|||||||
[sub_resource type="RectangleShape2D" id="RectangleShape2D_uwrxv"]
|
[sub_resource type="RectangleShape2D" id="RectangleShape2D_uwrxv"]
|
||||||
|
|
||||||
[node name="Crate" type="RigidBody2D" unique_id=1307809047]
|
[node name="Crate" type="RigidBody2D" unique_id=1307809047]
|
||||||
rotation = 0.65698373
|
|
||||||
script = ExtResource("1_b66cd")
|
script = ExtResource("1_b66cd")
|
||||||
metadata/_edit_group_ = true
|
metadata/_edit_group_ = true
|
||||||
|
|
||||||
|
|||||||
@ -1,36 +1,27 @@
|
|||||||
[gd_scene format=3 uid="uid://cqk2rgy3m1chs"]
|
[gd_scene format=3 uid="uid://cqk2rgy3m1chs"]
|
||||||
|
|
||||||
[ext_resource type="Script" uid="uid://dxubdt7nh5s1j" path="res://scripts/gameController.gd" id="1_lnu2h"]
|
[ext_resource type="Script" uid="uid://dxubdt7nh5s1j" path="res://scripts/gameController.gd" id="1_lnu2h"]
|
||||||
|
[ext_resource type="Script" uid="uid://b8iyniv2mu5mi" path="res://scripts/sceneManager.gd" id="2_iywne"]
|
||||||
[ext_resource type="PackedScene" uid="uid://b6lw2go5mwk3h" path="res://scenes/crate.tscn" id="2_lnu2h"]
|
[ext_resource type="PackedScene" uid="uid://b6lw2go5mwk3h" path="res://scenes/crate.tscn" id="2_lnu2h"]
|
||||||
[ext_resource type="Script" uid="uid://c17de7vvtri1e" path="res://scripts/player.gd" id="2_yqjtg"]
|
[ext_resource type="Script" uid="uid://c17de7vvtri1e" path="res://scripts/player.gd" id="2_yqjtg"]
|
||||||
[ext_resource type="PackedScene" uid="uid://bf50mxdma8wq4" path="res://scenes/trigger.tscn" id="4_lbhrr"]
|
[ext_resource type="PackedScene" uid="uid://bf50mxdma8wq4" path="res://scenes/trigger.tscn" id="4_lbhrr"]
|
||||||
|
|
||||||
|
[sub_resource type="CircleShape2D" id="CircleShape2D_uwrxv"]
|
||||||
|
|
||||||
[sub_resource type="RectangleShape2D" id="RectangleShape2D_8cj0n"]
|
[sub_resource type="RectangleShape2D" id="RectangleShape2D_8cj0n"]
|
||||||
size = Vector2(74, 20)
|
size = Vector2(74, 20)
|
||||||
|
|
||||||
[sub_resource type="CircleShape2D" id="CircleShape2D_uwrxv"]
|
|
||||||
|
|
||||||
[sub_resource type="WorldBoundaryShape2D" id="WorldBoundaryShape2D_lbhrr"]
|
[sub_resource type="WorldBoundaryShape2D" id="WorldBoundaryShape2D_lbhrr"]
|
||||||
|
|
||||||
[node name="Game" type="Node2D" unique_id=239599901]
|
[node name="Game" type="Node2D" unique_id=239599901]
|
||||||
|
position = Vector2(6, 0)
|
||||||
script = ExtResource("1_lnu2h")
|
script = ExtResource("1_lnu2h")
|
||||||
|
|
||||||
[node name="StaticBody2D" type="StaticBody2D" parent="." unique_id=2062716976]
|
[node name="SceneManager" type="Node2D" parent="." unique_id=1299641124]
|
||||||
position = Vector2(591, 442)
|
script = ExtResource("2_iywne")
|
||||||
scale = Vector2(2, 1)
|
|
||||||
metadata/_edit_group_ = true
|
|
||||||
|
|
||||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="StaticBody2D" unique_id=1655047088]
|
|
||||||
shape = SubResource("RectangleShape2D_8cj0n")
|
|
||||||
|
|
||||||
[node name="Crate" parent="." unique_id=1307809047 instance=ExtResource("2_lnu2h")]
|
|
||||||
position = Vector2(644, 404)
|
|
||||||
|
|
||||||
[node name="Crate2" parent="." unique_id=1988562966 instance=ExtResource("2_lnu2h")]
|
|
||||||
position = Vector2(609, 403)
|
|
||||||
|
|
||||||
[node name="Player" type="CharacterBody2D" parent="." unique_id=1540403255]
|
[node name="Player" type="CharacterBody2D" parent="." unique_id=1540403255]
|
||||||
position = Vector2(535, 417)
|
position = Vector2(577, 414)
|
||||||
script = ExtResource("2_yqjtg")
|
script = ExtResource("2_yqjtg")
|
||||||
metadata/_edit_group_ = true
|
metadata/_edit_group_ = true
|
||||||
|
|
||||||
@ -38,22 +29,48 @@ metadata/_edit_group_ = true
|
|||||||
shape = SubResource("CircleShape2D_uwrxv")
|
shape = SubResource("CircleShape2D_uwrxv")
|
||||||
debug_color = Color(0.28743955, 0.62438726, 0.25921708, 0.41960785)
|
debug_color = Color(0.28743955, 0.62438726, 0.25921708, 0.41960785)
|
||||||
|
|
||||||
[node name="Trigger" parent="." unique_id=1908774248 instance=ExtResource("4_lbhrr")]
|
[node name="Level" type="Node2D" parent="." unique_id=1047144827]
|
||||||
position = Vector2(692, 509)
|
|
||||||
effect = "destroy"
|
|
||||||
|
|
||||||
[node name="Trigger2" parent="." unique_id=1627494044 instance=ExtResource("4_lbhrr")]
|
[node name="StaticBody2D" type="StaticBody2D" parent="Level" unique_id=2062716976]
|
||||||
position = Vector2(508, 507)
|
position = Vector2(591, 442)
|
||||||
|
scale = Vector2(2, 1)
|
||||||
|
metadata/_edit_group_ = true
|
||||||
|
|
||||||
[node name="StaticBody2D2" type="StaticBody2D" parent="." unique_id=426786591]
|
[node name="CollisionShape2D" type="CollisionShape2D" parent="Level/StaticBody2D" unique_id=1655047088]
|
||||||
|
shape = SubResource("RectangleShape2D_8cj0n")
|
||||||
|
|
||||||
|
[node name="StaticBody2D2" type="StaticBody2D" parent="Level" unique_id=426786591]
|
||||||
position = Vector2(601, 622)
|
position = Vector2(601, 622)
|
||||||
metadata/_edit_group_ = true
|
metadata/_edit_group_ = true
|
||||||
|
|
||||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="StaticBody2D2" unique_id=895098891]
|
[node name="CollisionShape2D" type="CollisionShape2D" parent="Level/StaticBody2D2" unique_id=895098891]
|
||||||
shape = SubResource("WorldBoundaryShape2D_lbhrr")
|
shape = SubResource("WorldBoundaryShape2D_lbhrr")
|
||||||
|
|
||||||
[node name="Crate3" parent="." unique_id=1771442258 instance=ExtResource("2_lnu2h")]
|
[node name="Crates" type="Node2D" parent="." unique_id=131687815]
|
||||||
position = Vector2(570, 397)
|
|
||||||
|
|
||||||
[connection signal="AreaTrigger" from="Trigger" to="." method="_on_trigger"]
|
[node name="Crate" parent="Crates" unique_id=1307809047 instance=ExtResource("2_lnu2h")]
|
||||||
[connection signal="AreaTrigger" from="Trigger2" to="." method="_on_trigger"]
|
position = Vector2(644, 404)
|
||||||
|
|
||||||
|
[node name="Crate2" parent="Crates" unique_id=1988562966 instance=ExtResource("2_lnu2h")]
|
||||||
|
position = Vector2(609, 403)
|
||||||
|
|
||||||
|
[node name="Crate3" parent="Crates" unique_id=1771442258 instance=ExtResource("2_lnu2h")]
|
||||||
|
position = Vector2(539, 411)
|
||||||
|
|
||||||
|
[node name="Triggers" type="Node2D" parent="." unique_id=1373437339]
|
||||||
|
|
||||||
|
[node name="Trigger" parent="Triggers" unique_id=1908774248 instance=ExtResource("4_lbhrr")]
|
||||||
|
position = Vector2(692, 509)
|
||||||
|
effect = "destroy"
|
||||||
|
|
||||||
|
[node name="Trigger4" parent="Triggers" unique_id=1450661815 instance=ExtResource("4_lbhrr")]
|
||||||
|
position = Vector2(578, 326)
|
||||||
|
effect = "timeExtend"
|
||||||
|
|
||||||
|
[node name="Trigger3" parent="Triggers" unique_id=1623839308 instance=ExtResource("4_lbhrr")]
|
||||||
|
position = Vector2(353, 593)
|
||||||
|
effect = "destroy"
|
||||||
|
|
||||||
|
[node name="Trigger2" parent="Triggers" unique_id=1627494044 instance=ExtResource("4_lbhrr")]
|
||||||
|
position = Vector2(508, 507)
|
||||||
|
effect = "teleport"
|
||||||
|
|||||||
@ -1 +1,14 @@
|
|||||||
class_name Crate extends RigidBody2D
|
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.
|
# Called when the node enters the scene tree for the first time.
|
||||||
func _ready() -> void:
|
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.
|
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||||
func _process(delta: float) -> void:
|
func _process(delta: float) -> void:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
func secondCounter()->void:
|
||||||
func _on_trigger(body: Variant, effect) -> 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)
|
print("GC knows trigger...."+effect)
|
||||||
if body is Crate:
|
if body is Crate:
|
||||||
match effect:
|
match effect:
|
||||||
"destroy":
|
"destroy":
|
||||||
crateTotal -=1
|
destroySignal.emit(body)
|
||||||
if crateTotal <=0:
|
"teleport":
|
||||||
print("You WON!!!")
|
teleportSignal.emit(body)
|
||||||
body.queue_free()
|
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,4 +1,4 @@
|
|||||||
extends CharacterBody2D
|
class_name Player extends CharacterBody2D
|
||||||
|
|
||||||
|
|
||||||
const SPEED = 300.0
|
const SPEED = 300.0
|
||||||
|
|||||||
61
scripts/sceneManager.gd
Normal file
61
scripts/sceneManager.gd
Normal file
@ -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)
|
||||||
1
scripts/sceneManager.gd.uid
Normal file
1
scripts/sceneManager.gd.uid
Normal file
@ -0,0 +1 @@
|
|||||||
|
uid://b8iyniv2mu5mi
|
||||||
@ -1,6 +1,6 @@
|
|||||||
extends Area2D
|
class_name Trigger extends Area2D
|
||||||
|
|
||||||
signal AreaTrigger(body, effect)
|
signal AreaTrigger(body, effect, trigger)
|
||||||
@export var effect="destroy"
|
@export var effect="destroy"
|
||||||
|
|
||||||
# Called when the node enters the scene tree for the first time.
|
# 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:
|
func _on_body_entered(body: Node2D) -> void:
|
||||||
print("Trigger activated")
|
print("Trigger activated")
|
||||||
AreaTrigger.emit(body, effect)
|
AreaTrigger.emit(body, effect, self)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user