badguys, autoloads, custom resources, collectibles
This commit is contained in:
1 parent
b3575f2f06
commit
8bb10b79a8
16 files changed
+873
-37
No files matched your search
@@ -0,0 +1,27 @@
|
||||
class_name BadGuy extends Area2D
|
||||
@onready var sprite: AnimatedSprite2D = $AnimatedSprite2D
|
||||
@onready var right_ray: RayCast2D = $RightRay
|
||||
@onready var left_ray: RayCast2D = $LeftRay
|
||||
@onready var right_floor_ray: RayCast2D = $RightFloorRay
|
||||
@onready var left_floor_ray: RayCast2D = $LeftFloorRay
|
||||
const speed = 60
|
||||
var direction = 1
|
||||
|
||||
#signals
|
||||
signal playerDamage
|
||||
|
||||
func _process(delta: float) -> void:
|
||||
if not right_floor_ray.is_colliding():
|
||||
#about to fall on the right....
|
||||
direction = -1
|
||||
sprite.flip_h = true
|
||||
if not left_floor_ray.is_colliding():
|
||||
direction = 1
|
||||
sprite.flip_h = false
|
||||
|
||||
position.x += direction * speed * delta
|
||||
|
||||
func _on_body_entered(body: Node2D) -> void:
|
||||
if body.is_in_group("player"):
|
||||
print("Bad Guy Collided")
|
||||
playerDamage.emit()
|
||||
@@ -0,0 +1,8 @@
|
||||
class_name Coin extends Area2D
|
||||
|
||||
signal coinCollected
|
||||
|
||||
func _on_body_entered(body: Node2D) -> void:
|
||||
if body.is_in_group("player"):
|
||||
coinCollected.emit()
|
||||
queue_free()
|
||||
@@ -1,45 +1,74 @@
|
||||
extends Node2D
|
||||
|
||||
var boxTotal = 4
|
||||
var boxTotal := 4
|
||||
var timer:= Timer.new()
|
||||
var countdown = 10
|
||||
var totalCrates = 0
|
||||
var countdown := 10
|
||||
var totalCrates := 0
|
||||
var cratesDestroyed := 0
|
||||
var totalCoins := 0
|
||||
var totalCoinsCollected := 0
|
||||
var totalBadGuys := 0
|
||||
|
||||
var player:Resource
|
||||
|
||||
#level information
|
||||
var levels = ["res://scenes/game.tscn","res://scenes/level2.tscn"]
|
||||
var timers = [10, 15]
|
||||
var currentLevel = 0
|
||||
|
||||
#Signals from GC
|
||||
signal destroyBox(body)
|
||||
signal levelComplete(levelToLoad)
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
player = load("res://scripts/res/playerstats.tres")
|
||||
print("Player health "+str(player.health))
|
||||
|
||||
print("Number of levels: "+str(levels.size() ) )
|
||||
reset()
|
||||
add_child(timer)
|
||||
timer.wait_time = 1
|
||||
timer.one_shot = false
|
||||
timer.connect("timeout", secondCounter)
|
||||
# timer.start()
|
||||
timer.start()
|
||||
|
||||
func reset():
|
||||
countdown = timers[currentLevel]
|
||||
|
||||
func secondCounter():
|
||||
|
||||
countdown -=1
|
||||
if countdown <=0:
|
||||
print("YOU LOSE")
|
||||
get_tree().reload_current_scene()
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
func _process(delta: float) -> void:
|
||||
pass
|
||||
|
||||
|
||||
func _on_trigger(effect: Variant, body) -> void:
|
||||
print("Game Controller sees an alert")
|
||||
if body is RigidBody2D:
|
||||
print("I see a crate")
|
||||
body.queue_free()
|
||||
boxTotal -=1
|
||||
if boxTotal <=0:
|
||||
print("YOU WON!!")
|
||||
get_tree().reload_current_scene()
|
||||
|
||||
levelComplete.emit(levels[currentLevel])
|
||||
func coinCollected():
|
||||
totalCoinsCollected += 1
|
||||
print("GC knows coin collected")
|
||||
func playerDamaged():
|
||||
print("GC knows player taking damage")
|
||||
player.health -= 20
|
||||
print("Health remaining: "+str(player.health))
|
||||
|
||||
func bulletHit(body):
|
||||
print("Game controller knows bullet hit something")
|
||||
if body.is_in_group("destructable"):
|
||||
destroyBox.emit(body)
|
||||
|
||||
totalCrates -=1
|
||||
cratesDestroyed +=1
|
||||
if totalCrates <=0:
|
||||
print("YOU WON!!")
|
||||
#refactor this - make it a signal
|
||||
currentLevel +=1
|
||||
if currentLevel >= levels.size():
|
||||
currentLevel = 0
|
||||
levelComplete.emit(levels[currentLevel])
|
||||
else:
|
||||
print("Crates Remaining "+str(totalCrates) )
|
||||
# Coming from SceneManager
|
||||
func countCrates(value):
|
||||
totalCrates = value
|
||||
func countCoins(value):
|
||||
totalCoins = value
|
||||
func countBadGuys(value):
|
||||
totalBadGuys = value
|
||||
@@ -0,0 +1,5 @@
|
||||
class_name CharacterStats extends Resource
|
||||
|
||||
@export var max_health:int = 100
|
||||
@export var starting_health:int = 100
|
||||
@export var health:int = 100
|
||||
@@ -0,0 +1,9 @@
|
||||
[gd_resource type="Resource" script_class="CharacterStats" load_steps=2 format=3 uid="uid://kty8mv3cl6dc"]
|
||||
|
||||
[ext_resource type="Script" path="res://scripts/res/characterstats.gd" id="1_tqdla"]
|
||||
|
||||
[resource]
|
||||
script = ExtResource("1_tqdla")
|
||||
max_health = 150
|
||||
starting_health = 100
|
||||
health = 100
|
||||
@@ -0,0 +1,9 @@
|
||||
[gd_resource type="Resource" script_class="CharacterStats" load_steps=2 format=3 uid="uid://buy7hpseunp3u"]
|
||||
|
||||
[ext_resource type="Script" path="res://scripts/res/characterstats.gd" id="1_a2a1n"]
|
||||
|
||||
[resource]
|
||||
script = ExtResource("1_a2a1n")
|
||||
max_health = 60
|
||||
starting_health = 50
|
||||
health = 50
|
||||
@@ -1,24 +1,46 @@
|
||||
extends Node
|
||||
@onready var game: Node2D = $".."
|
||||
|
||||
@onready var crates: Node2D = $"../crates"
|
||||
@onready var coins: Node2D = $"../coins"
|
||||
@onready var enemies: Node2D = $"../enemies"
|
||||
|
||||
var bullet = preload("res://scenes/bullet.tscn")
|
||||
var bulletArray:Array = []
|
||||
var bulletsMadeTotal :=0
|
||||
|
||||
|
||||
var totalCrates := 0
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
game.destroyBox.connect(boxDestroy)
|
||||
#Give GC number of crates
|
||||
Gamecontroller.destroyBox.connect(boxDestroy)
|
||||
for obj in crates.get_children():
|
||||
if obj.is_in_group("destructable"):
|
||||
#increase the box counter
|
||||
totalCrates +=1
|
||||
|
||||
var totalCoins = 0
|
||||
for obj in coins.get_children():
|
||||
if obj.is_in_group("collectibles"):
|
||||
totalCoins +=1
|
||||
obj.coinCollected.connect(Gamecontroller.coinCollected)
|
||||
var totalEnemies = 0
|
||||
for obj in enemies.get_children():
|
||||
if obj.is_in_group("enemies"):
|
||||
totalEnemies +=1
|
||||
obj.playerDamage.connect(Gamecontroller.playerDamaged)
|
||||
Gamecontroller.countBadGuys(totalEnemies)
|
||||
Gamecontroller.countCoins(totalCoins)
|
||||
Gamecontroller.countCrates(totalCrates)
|
||||
|
||||
#Subscribe to vital events from GC
|
||||
Gamecontroller.levelComplete.connect(changeLevel)
|
||||
|
||||
Gamecontroller.reset()
|
||||
|
||||
|
||||
|
||||
func changeLevel(level):
|
||||
get_tree().change_scene_to_file(level)
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
func _process(delta: float) -> void:
|
||||
@@ -49,7 +71,7 @@ func onBulletHit(bullet, body):
|
||||
bullet.setSpeed(0)
|
||||
|
||||
print("Scene manager knows bullet hit")
|
||||
game.bulletHit(body)
|
||||
Gamecontroller.bulletHit(body)
|
||||
|
||||
func boxDestroy(body):
|
||||
body.queue_free()
|
||||
|
||||
Reference in new issue
Block a user