player death, player hurt, ui

This commit is contained in:
OddlyTimbot committed 2025-02-10 21:12:59 -05:00
1 parent 8bb10b79a8
commit 4389b9804c
14 files changed
+432 -81

No files matched your search

+8
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:
@@ -28,3 +29,10 @@ func _on_body_entered(body: Node2D) -> void:
print("Bullet hit sumtin")
if not body.is_in_group("player"):
hit.emit(self, body)
func _on_area_entered(area: Area2D) -> void:
print("Bullet hitting area")
if area.is_in_group("enemies"):
badguyHit.emit(area)
+99 -72
View File
@@ -21,77 +21,104 @@ var pushLeftEnabled = false
var isJumping = false
func _physics_process(delta: float) -> void:
# Add the gravity.
if not is_on_floor():
velocity += get_gravity() * delta
else:
isJumping = false
# Handle jump.
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 == true:
pushTarget.apply_central_impulse(Vector2(-1,0) * PUSH_FORCE * 2 )
# Handle Shooting
if Input.is_action_just_pressed("shoot"):
if faceLeft == true:
var myBullet = %SceneManager.makeBullet(left_target.global_transform, -700)
#myBullet.transform = left_target.global_transform
#myBullet.setSpeed(-700)
if faceLeft == false:
var myBullet = %SceneManager.makeBullet(right_target.global_transform, 700)
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
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("idle")
velocity.x = move_toward(velocity.x, 0, SPEED)
var living = true
var animPlaying = "idle"
move_and_slide()
if right_ray.is_colliding():
if not faceLeft:
var collider = right_ray.get_collider()
if collider is RigidBody2D:
pushTarget = collider
pushRightEnabled = true
else:
pushRightEnabled = false
signal deathAnimationComplete
func hurtPlayer(_amt):
if _amt < 100:
animPlaying = "hurt"
animation.play(animPlaying)
if left_ray.is_colliding():
if faceLeft:
var collider = left_ray.get_collider()
if collider is RigidBody2D:
pushTarget = collider
pushLeftEnabled = true
else:
pushLeftEnabled = false
for i in get_slide_collision_count():
var c = get_slide_collision(i)
if c.get_collider() is RigidBody2D:
c.get_collider().apply_central_impulse(-c.get_normal() * BUMP_FORCE)
func killPlayer():
print("PC will kill player")
if living:
living = false
animPlaying = "death"
animation.play(animPlaying)
func _physics_process(delta: float) -> void:
if living and not animPlaying == "hurt":
# Add the gravity.
if not is_on_floor():
velocity += get_gravity() * delta
else:
isJumping = false
# Handle jump.
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 == true:
pushTarget.apply_central_impulse(Vector2(-1,0) * PUSH_FORCE * 2 )
# Handle Shooting
if Input.is_action_just_pressed("shoot"):
if faceLeft == true:
var myBullet = %SceneManager.makeBullet(left_target.global_transform, -700)
#myBullet.transform = left_target.global_transform
#myBullet.setSpeed(-700)
if faceLeft == false:
var myBullet = %SceneManager.makeBullet(right_target.global_transform, 700)
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
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("idle")
velocity.x = move_toward(velocity.x, 0, SPEED)
move_and_slide()
if right_ray.is_colliding():
if not faceLeft:
var collider = right_ray.get_collider()
if collider is RigidBody2D:
pushTarget = collider
pushRightEnabled = true
else:
pushRightEnabled = false
if left_ray.is_colliding():
if faceLeft:
var collider = left_ray.get_collider()
if collider is RigidBody2D:
pushTarget = collider
pushLeftEnabled = true
else:
pushLeftEnabled = false
for i in get_slide_collision_count():
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":
# emit death signal
deathAnimationComplete.emit()
+28 -1
View File
@@ -13,12 +13,18 @@ var player:Resource
#level information
var levels = ["res://scenes/game.tscn","res://scenes/level2.tscn"]
var timers = [10, 15]
var timers = [20, 25]
var currentLevel = 0
#Signals from GC
signal destroyBox(body)
signal destroyBadguy(area)
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() -> void:
@@ -34,21 +40,38 @@ func _ready() -> void:
timer.start()
func reset():
totalCoinsCollected = 0
player.health = player.starting_health
countdown = timers[currentLevel]
tick.emit.call_deferred(countdown)
coinsUpdate.emit.call_deferred(totalCoinsCollected, totalCoins)
playerHurt.emit.call_deferred(player.health)
func secondCounter():
countdown -=1
tick.emit(countdown)
if countdown <=0:
print("YOU LOSE")
levelComplete.emit(levels[currentLevel])
func coinCollected():
totalCoinsCollected += 1
coinsUpdate.emit(totalCoinsCollected, totalCoins)
print("GC knows coin collected")
func playerDamaged():
print("GC knows player taking damage")
player.health -= 20
print("Health remaining: "+str(player.health))
if player.health > 0:
playerHurt.emit(player.health)
else:
playerDeath.emit()
func bulletHitBadguy(area):
#decide what to do
#how many bad guys remaining
#send a signal
destroyBadguy.emit(area)
func bulletHit(body):
print("Game controller knows bullet hit something")
@@ -72,3 +95,7 @@ func countCoins(value):
totalCoins = value
func countBadGuys(value):
totalBadGuys = value
func playerDead():
print("GC knows player death")
levelComplete.emit(levels[currentLevel])
+17
View File
@@ -3,6 +3,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")
var bulletArray:Array = []
@@ -29,13 +31,24 @@ func _ready() -> void:
if obj.is_in_group("enemies"):
totalEnemies +=1
obj.playerDamage.connect(Gamecontroller.playerDamaged)
Gamecontroller.countBadGuys(totalEnemies)
Gamecontroller.countCoins(totalCoins)
Gamecontroller.countCrates(totalCrates)
# Player interactions
Gamecontroller.playerHurt.connect(player.hurtPlayer)
Gamecontroller.playerDeath.connect(player.killPlayer)
#Subscribe to vital events from GC
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()
@@ -52,6 +65,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,6 +79,9 @@ func makeBullet(position, speed):
myBullet.transform = position
myBullet.setSpeed(speed)
return myBullet
func killBadguy(area):
area.queue_free()
func onBulletHit(bullet, body):
bullet.position = Vector2(-100,-100)
+18
View File
@@ -0,0 +1,18 @@
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)