player death, player hurt, ui

This commit is contained in:
GabriellePipo committed 2025-02-10 21:13:02 -05:00
1 parent ce71477b76
commit 321259532e
14 files changed
+376 -42

No files matched your search

+6 -1
View File
@@ -3,6 +3,7 @@ class_name Bullet extends Area2D
var speed = 700
signal hit
signal badguyhit(area)
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
@@ -25,6 +26,10 @@ func setSpeed(value):
func _on_body_entered(body: Node2D) -> void:
print("hit")
if not body.is_in_group("player"):
hit.emit(self, body)
func _on_area_entered(area: Area2D) -> void:
if area.is_in_group("enemies"):
badguyhit.emit(area)
+29 -3
View File
@@ -9,13 +9,21 @@ var totalCoinsCollected := 0
var totalEnemies := 0
#level information
var levels = ["res://scenes/game.tscn","res://scenes/level2.tscn"]
var timers = [10,15]
var timers = [20,15]
var currentLevel = 0
var player:Resource
# signals from game controller
signal destroyBadguy(area)
signal destroyBox(body)
signal levelComplete(levelToLoad)
signal playerHurt(amt)
signal playerDeath
signal tick(timeRemaining)
signal coinsUpdate(coinsCollected, coinsAvailable)
# Called when the node enters the scene tree for the first time.
func _ready():
player = load("res://scripts/res/playerstats.tres")
@@ -30,9 +38,15 @@ func _ready():
func reset():
countdown = timers[currentLevel]
player.health = player.starting_health
totalCoinsCollected = 0
tick.emit.call_deferred(countdown)
coinsUpdate.emit.call_deferred(totalCoinsCollected,totalCoins)
playerHurt.emit.call_deferred(player.health)
func secondCounter():
countdown -=1
print("minus one")
tick.emit(countdown)
if countdown <=0:
print("you lose")
levelComplete.emit(levels[currentLevel])
@@ -44,12 +58,20 @@ func _process(delta):
pass
func coinCollected():
print("gane cotroller knows coin collected")
totalCoinsCollected+=1
coinsUpdate.emit(totalCoinsCollected, totalCoins)
print(totalCoinsCollected)
func playerDamaged():
player.health -= 20
print("game controller knows player damaged, health is now "+str(player.health))
if player.health > 0:
playerHurt.emit(player.health)
else:
playerDeath.emit()
func bullethitBadguy(area):
destroyBadguy.emit(area)
func bulletHit(body):
print("game controller knows bullet hit something")
@@ -72,3 +94,7 @@ func countCrates(value):
totalCrates = value
func countCoins(value):
totalCoins = value
func playerDead():
print("gc knows player is dead")
levelComplete.emit(levels[currentLevel])
+57 -32
View File
@@ -25,40 +25,58 @@ var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
var isJumping = false
func _physics_process(delta):
if not is_on_floor():
velocity.y += gravity * delta
else:
isJumping = false
var living = true
var animPlaying = "idle"
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = JUMP_VELOCITY
animation.play("jump")
isJumping = true
# shove attack
if Input.is_action_just_pressed("shove"):
if pushRightEnabled && faceleft == false:
pushTarget.apply_central_impulse(Vector2(1,0) * PUSH_FORCE * 2)
if pushLeftEnabled && faceleft:
pushTarget.apply_central_impulse(Vector2(-1,0) * PUSH_FORCE * 2)
var direction = Input.get_axis("left", "right")
if direction:
velocity.x = direction * SPEED
if direction<0:
signal deathAnimationComplete
func hurtPlayer(_amt):
if _amt < 100:
animPlaying = "hurt"
animation.play(animPlaying)
func killPlayer():
print("player character is killed")
if living:
living = false
animPlaying = "death"
animation.play(animPlaying)
func _physics_process(delta):
if living and not animPlaying == "hurt":
if not is_on_floor():
velocity.y += gravity * delta
else:
isJumping = false
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = JUMP_VELOCITY
animation.play("jump")
isJumping = true
# shove attack
if Input.is_action_just_pressed("shove"):
if pushRightEnabled && faceleft == false:
pushTarget.apply_central_impulse(Vector2(1,0) * PUSH_FORCE * 2)
if pushLeftEnabled && faceleft:
pushTarget.apply_central_impulse(Vector2(-1,0) * PUSH_FORCE * 2)
var direction = Input.get_axis("left", "right")
if direction:
velocity.x = direction * SPEED
if direction<0:
if not isJumping:
animation.play("run")
faceleft = true
animation.flip_h = true
if direction>0:
if not isJumping:
animation.play("run")
faceleft = false
animation.flip_h = false
else:
if not isJumping:
animation.play("run")
faceleft = true
animation.flip_h = true
if direction>0:
if not isJumping:
animation.play("run")
faceleft = false
animation.flip_h = false
else:
if not isJumping:
animation.play("idle")
velocity.x = move_toward(velocity.x, 0, SPEED)
animation.play("idle")
velocity.x = move_toward(velocity.x, 0, SPEED)
# shoot
@@ -96,3 +114,10 @@ func _physics_process(delta):
var c = get_slide_collision(i)
if c.get_collider() is RigidBody2D:
c.get_collider().apply_central_impulse(-c.get_normal() * BUMP_FORCE)
func _on_animation_finished() -> void:
if animPlaying == "hurt":
animPlaying = "idle"
if animPlaying == "death":
deathAnimationComplete.emit()
+20
View File
@@ -2,6 +2,8 @@ extends Node
@onready var crates: Node2D = $"../crates"
@onready var coins: Node2D = $"../coins"
@onready var enemies: Node2D = $"../enemies"
@onready var player: CharacterBody2D = $"../CharacterBody2D"
@onready var ui: Control = $"../CanvasLayer/ui"
var bullet = preload("res://scenes/bullet.tscn")
@@ -28,9 +30,23 @@ func _ready() -> void:
obj.playerDamage.connect(Gamecontroller.playerDamaged)
Gamecontroller.countCrates(totalCrates)
Gamecontroller.countCoins(totalCoins)
#Player interactions
Gamecontroller.playerHurt.connect(player.hurtPlayer)
Gamecontroller.playerDeath.connect(player.killPlayer)
#subscribe to vital events from the game controller
Gamecontroller.levelComplete.connect(changeLevel)
player.deathAnimationComplete.connect(Gamecontroller.playerDead)
#ui events
Gamecontroller.tick.connect(ui.timerUpdate)
Gamecontroller.playerHurt.connect(ui.healthUpdate)
Gamecontroller.playerDeath.connect(ui.healthGone)
Gamecontroller.coinsUpdate.connect(ui.coinsUpdate)
Gamecontroller.destroyBadguy.connect(killBadguy)
Gamecontroller.reset()
func changeLevel(level):
@@ -45,6 +61,7 @@ func bulletFactory():
if bulletArray.size() < 4:
myBullet = bullet.instantiate()
myBullet.connect("hit", onBulletHit)
myBullet.connect("badguyhit", Gamecontroller.bullethitBadguy)
owner.add_child(myBullet)
else:
myBullet = bulletArray.pop_back()
@@ -65,5 +82,8 @@ func onBulletHit(bullet, body):
print("Scene manager knows bullet hit")
Gamecontroller.bulletHit(body)
func killBadguy (area):
area.queue_free()
func boxDestroy(body):
body.queue_free()
+21
View File
@@ -0,0 +1,21 @@
extends Control
@onready var timer: Label = $VBoxContainer/HBoxContainer/MarginContainer3/timer
@onready var health: Label = $VBoxContainer/HBoxContainer/MarginContainer/health
@onready var coins: Label = $VBoxContainer/HBoxContainer/MarginContainer2/coins
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
pass # Replace with function body.
func timerUpdate(timeRemaining):
timer.text=str(timeRemaining)
func healthUpdate(_healthRemaining):
health.text="HEALTH "+str(_healthRemaining)
func healthGone():
health.text="HEALTH 0"
func coinsUpdate(coinsCollectedTotal, coinsAvailableTotal):
coins.text=str(coinsCollectedTotal)+" of "+str(coinsAvailableTotal)