multiple level, timer, health, coins
This commit is contained in:
1 parent
8966ac2165
commit
a580dca65b
11 files changed
+713
-29
No files matched your search
@@ -9,6 +9,7 @@ const PUSH_FORCE = 750
|
||||
@onready var right_ray: RayCast2D = $RightRay
|
||||
@onready var left_ray: RayCast2D = $LeftRay
|
||||
@onready var playerSprite: AnimatedSprite2D = $AnimatedSprite2D
|
||||
@onready var gun: Sprite2D = $Gun
|
||||
|
||||
|
||||
var faceLeft = false
|
||||
@@ -26,18 +27,27 @@ signal playerDead
|
||||
@onready var right_target: Node2D = $RightTarget
|
||||
@onready var left_target: Node2D = $LeftTarget
|
||||
|
||||
func _ready() -> void:
|
||||
gunDrop()
|
||||
|
||||
func hurtPlayer(amt):
|
||||
print("Character knows it should hurt")
|
||||
|
||||
animPlaying = "hurt"
|
||||
playerSprite.play("hurt")
|
||||
|
||||
func killPlayer():
|
||||
print("Characters know it is the end")
|
||||
if living:
|
||||
living = false
|
||||
animPlaying = "death"
|
||||
playerSprite.play(animPlaying)
|
||||
|
||||
func gunCollected():
|
||||
print("player knows to show gun")
|
||||
gun.visible = true
|
||||
|
||||
func gunDrop():
|
||||
gun.visible = false
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
if living and not animPlaying == "hurt":
|
||||
# Add the gravity.
|
||||
@@ -52,7 +62,7 @@ func _physics_process(delta: float) -> void:
|
||||
# As good practice, you should replace UI actions with custom gameplay actions.
|
||||
var direction := Input.get_axis("ui_left", "ui_right")
|
||||
if Input.is_action_just_pressed("shove") && pushRightEnabled && faceLeft == false:
|
||||
print("shove a box")
|
||||
|
||||
pushTarget.apply_central_impulse(Vector2(1,0) * PUSH_FORCE * 2)
|
||||
pushRightEnabled = false
|
||||
|
||||
@@ -61,7 +71,6 @@ func _physics_process(delta: float) -> void:
|
||||
pushLeftEnabled = false
|
||||
|
||||
if Input.is_action_just_pressed("shoot"):
|
||||
print("shoot a bullet")
|
||||
var mybullet = %SceneManager.bulletFactory()
|
||||
if not faceLeft:
|
||||
mybullet.transform = right_target.global_transform
|
||||
@@ -99,24 +108,20 @@ func _physics_process(delta: float) -> void:
|
||||
|
||||
move_and_slide()
|
||||
if right_ray.is_colliding():
|
||||
print("Something is on my right")
|
||||
if not faceLeft:
|
||||
var collider = right_ray.get_collider()
|
||||
if collider is Node:
|
||||
if collider.is_in_group("box"):
|
||||
print("This is a pushable box")
|
||||
pushTarget = collider
|
||||
pushRightEnabled = true
|
||||
else:
|
||||
pushRightEnabled = false
|
||||
|
||||
if left_ray.is_colliding():
|
||||
print("Something is on my left")
|
||||
if faceLeft:
|
||||
var collider = left_ray.get_collider()
|
||||
if collider is Node:
|
||||
if collider.is_in_group("box"):
|
||||
print("This is a pushable box")
|
||||
pushTarget = collider
|
||||
pushLeftEnabled = true
|
||||
else:
|
||||
|
||||
@@ -2,22 +2,52 @@ extends Node
|
||||
|
||||
var coinsCollected:int = 0
|
||||
var player:Resource
|
||||
var totalCoinsLevel = 0
|
||||
var levels = ["res://scenes/game.tscn", "res://scenes/game_level2.tscn"]
|
||||
var timers = [25, 19]
|
||||
var currentLevel = 0
|
||||
|
||||
var gameSeconds = 0
|
||||
|
||||
var gotGun = false
|
||||
|
||||
signal playerHurt(amt)
|
||||
signal playerDeath()
|
||||
signal coinsUpdate(amt, total)
|
||||
signal levelComplete(level)
|
||||
signal tick(seconds)
|
||||
signal playerOptions(gun)
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
player = load("res://scripts/res/playerstats.tres")
|
||||
|
||||
|
||||
func setTotalCoins(amt):
|
||||
totalCoinsLevel = amt
|
||||
print(str(coinsCollected)+ " "+str(totalCoinsLevel))
|
||||
coinsUpdate.emit(coinsCollected, totalCoinsLevel)
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
func _process(delta: float) -> void:
|
||||
pass
|
||||
|
||||
func gunCollected():
|
||||
print("GameController knows player has gun")
|
||||
gotGun = true
|
||||
|
||||
func coinCollected():
|
||||
coinsCollected += 1
|
||||
print("Total coins collecte " + str(coinsCollected))
|
||||
coinsUpdate.emit(coinsCollected, totalCoinsLevel)
|
||||
# did you complete the level
|
||||
if coinsCollected >= totalCoinsLevel:
|
||||
# level complete
|
||||
currentLevel +=1
|
||||
if currentLevel < levels.size():
|
||||
levelComplete.emit(levels[currentLevel])
|
||||
else:
|
||||
currentLevel = 0
|
||||
levelComplete.emit(levels[currentLevel])
|
||||
|
||||
func playerDamage():
|
||||
if player.health > 0:
|
||||
@@ -27,8 +57,19 @@ func playerDamage():
|
||||
playerDeath.emit()
|
||||
else:
|
||||
## damage him
|
||||
playerHurt.emit(20)
|
||||
playerHurt.emit(player.health)
|
||||
|
||||
func reset():
|
||||
gameSeconds = 0
|
||||
player.health = player.max_health
|
||||
coinsCollected = 0
|
||||
|
||||
|
||||
func timeOut():
|
||||
gameSeconds += 1
|
||||
if gameSeconds >= timers[currentLevel]:
|
||||
playerDeath.emit()
|
||||
tick.emit(timers[currentLevel] - gameSeconds)
|
||||
|
||||
func sendOptions():
|
||||
playerOptions.emit(gotGun)
|
||||
@@ -3,30 +3,59 @@ extends Node
|
||||
var bulletsFiredTotal := 0
|
||||
var bulletsMadeTotal := 0
|
||||
var bulletArray:Array = []
|
||||
var timer:= Timer.new()
|
||||
|
||||
@onready var coins: Node = $"../Coins"
|
||||
@onready var enemies: Node = $"../enemies"
|
||||
@onready var gun_trigger: Area2D = $"../GunTrigger"
|
||||
|
||||
@onready var player: CharacterBody2D = $"../CharacterBody2D"
|
||||
@onready var ui: Control = $"../CanvasLayer/Control"
|
||||
|
||||
|
||||
var bullet = preload("res://scenes/bullet.tscn")
|
||||
|
||||
func _ready() -> void:
|
||||
add_child(timer)
|
||||
timer.wait_time = 1
|
||||
timer.one_shot = false
|
||||
timer.connect("timeout", GameController.timeOut)
|
||||
timer.start()
|
||||
|
||||
var totalCoins = 0
|
||||
var totalEnemies = 0
|
||||
## Coins loop
|
||||
for n in coins.get_children():
|
||||
print("coin found")
|
||||
totalCoins += 1
|
||||
n.coinCollected.connect(GameController.coinCollected)
|
||||
## enemy loop
|
||||
for n in enemies.get_children():
|
||||
print("enemy found")
|
||||
totalEnemies += 1
|
||||
n.playerDamage.connect(GameController.playerDamage)
|
||||
|
||||
## wire up game controller signals to Player
|
||||
|
||||
|
||||
## wire up GameController signals to Player
|
||||
GameController.playerHurt.connect(hurtPlayer)
|
||||
GameController.playerHurt.connect(ui.healthUpdate)
|
||||
GameController.playerDeath.connect(killPlayer)
|
||||
GameController.levelComplete.connect(changeLevel)
|
||||
GameController.tick.connect(ui.timerUpdate)
|
||||
GameController.playerOptions.connect(showGun)
|
||||
|
||||
gun_trigger.gunCollect.connect(GameController.gunCollected)
|
||||
gun_trigger.gunCollect.connect(player.gunCollected)
|
||||
|
||||
player.playerDead.connect(deadPlayer)
|
||||
|
||||
|
||||
|
||||
GameController.coinsUpdate.connect(ui.coinsUpdate)
|
||||
|
||||
GameController.setTotalCoins(totalCoins)
|
||||
|
||||
GameController.sendOptions()
|
||||
|
||||
func bulletFactory():
|
||||
print("Make a bullet at the factory")
|
||||
var mybullet
|
||||
@@ -60,3 +89,14 @@ func killPlayer():
|
||||
func deadPlayer():
|
||||
GameController.reset()
|
||||
get_tree().reload_current_scene()
|
||||
|
||||
func showGun(gun):
|
||||
if gun == true:
|
||||
player.gunCollected()
|
||||
else:
|
||||
player.gunDrop()
|
||||
|
||||
func changeLevel(level):
|
||||
print("Scene Manager should change level")
|
||||
GameController.reset()
|
||||
get_tree().change_scene_to_file(level)
|
||||
@@ -1,6 +1,6 @@
|
||||
extends Area2D
|
||||
var BoxCount = 0
|
||||
var MaxBoxes = 3
|
||||
|
||||
signal gunCollect
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
@@ -14,8 +14,6 @@ func _process(delta: float) -> void:
|
||||
|
||||
func _on_body_entered(body: Node2D) -> void:
|
||||
print("Trigger saw a thing")
|
||||
if body.is_in_group("box"):
|
||||
print("box hit the trigger")
|
||||
BoxCount +=1
|
||||
if BoxCount >= MaxBoxes:
|
||||
print("YU WUN")
|
||||
if body.is_in_group("player"):
|
||||
print("player entered area")
|
||||
gunCollect.emit()
|
||||
@@ -0,0 +1,23 @@
|
||||
extends Control
|
||||
@onready var health: Label = $VBoxContainer/HBoxContainer/MarginContainer/health
|
||||
@onready var coins: Label = $VBoxContainer/HBoxContainer/MarginContainer2/coins
|
||||
@onready var timer: Label = $VBoxContainer/HBoxContainer/MarginContainer3/timer
|
||||
|
||||
var tempCoinCount = 0
|
||||
var tempCoinTotal = 0
|
||||
|
||||
func timerUpdate(amt):
|
||||
timer.text = "Seconds : " + str(amt)
|
||||
|
||||
func healthUpdate(amt):
|
||||
health.text = ("Heath : " + str(amt))
|
||||
|
||||
func coinsUpdate(amt, total):
|
||||
print("Update the UI " +str(amt))
|
||||
tempCoinCount = amt
|
||||
tempCoinTotal = total
|
||||
if coins:
|
||||
coins.text = ("Coins: " + str(amt) + " of " + str(total))
|
||||
|
||||
func _ready() -> void:
|
||||
coins.text = "Coins: " + str(tempCoinCount) + " of " + str(tempCoinTotal)
|
||||
Reference in new issue
Block a user