From 8975e4b416fbdc66086bb8bd828f8b7e5f8136e2 Mon Sep 17 00:00:00 2001 From: OddlyTimbot Date: Mon, 7 Jul 2025 21:01:48 -0400 Subject: [PATCH] player damage, player death, badguy dictionairy, badguy damage, badguy death, user interface, player damage animation, player death animation --- .../player/death/Player Death 64x64.png | Bin 0 -> 1584 bytes .../death/Player Death 64x64.png.import | 34 ++++++ .../player/hurt/Player Hurt 48x48.png | Bin 0 -> 1448 bytes .../player/hurt/Player Hurt 48x48.png.import | 34 ++++++ scenes/bullet.tscn | 2 + scenes/crate.tscn | 4 +- scenes/game.tscn | 9 +- scenes/level2.tscn | 7 +- scenes/level3.tscn | 7 +- scenes/player.tscn | 114 +++++++++++++++++- scenes/slime.tscn | 36 +++++- scenes/ui.tscn | 47 ++++++++ scripts/bullet.gd | 8 ++ scripts/character_body_2d.gd | 14 ++- scripts/coin.gd | 2 +- scripts/crate.gd | 1 + scripts/crate.gd.uid | 1 + scripts/gamecontroller.gd | 61 +++++++--- scripts/scene_manager.gd | 10 +- scripts/slime.gd | 16 ++- scripts/ui.gd | 18 +++ scripts/ui.gd.uid | 1 + 22 files changed, 395 insertions(+), 31 deletions(-) create mode 100644 assets/graphics/player/death/Player Death 64x64.png create mode 100644 assets/graphics/player/death/Player Death 64x64.png.import create mode 100644 assets/graphics/player/hurt/Player Hurt 48x48.png create mode 100644 assets/graphics/player/hurt/Player Hurt 48x48.png.import create mode 100644 scenes/ui.tscn create mode 100644 scripts/crate.gd create mode 100644 scripts/crate.gd.uid create mode 100644 scripts/ui.gd create mode 100644 scripts/ui.gd.uid diff --git a/assets/graphics/player/death/Player Death 64x64.png b/assets/graphics/player/death/Player Death 64x64.png new file mode 100644 index 0000000000000000000000000000000000000000..2e6a1ae0514b237d754893f563c87ba5f95dc109 GIT binary patch literal 1584 zcmV-02G9A4P)6ha{(mSDs~=&eemLQhsu5De5)C{a8JIe3wvJ+;t7 zrHbvvg27;Ok>XiYQVE4ttTBeFjoM_}te8c|!|qIHrt@d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S-R z+eU|6CN8%LWZ>4FSbiKUbdha!@ z?QwYZo8uN4{ZRfGN}2+h void: pass # Replace with function body. @@ -11,3 +13,9 @@ func setSpeed(value): # Called every frame. 'delta' is the elapsed time since the previous frame. func _physics_process(delta: float) -> void: position += transform.x * speed * delta + + +func _on_area_entered(area: Area2D) -> void: + print("bullet is hitting area") + if area is Slime: + bulletDamageSignal.emit(area, self) diff --git a/scripts/character_body_2d.gd b/scripts/character_body_2d.gd index be5492e..1941e1f 100644 --- a/scripts/character_body_2d.gd +++ b/scripts/character_body_2d.gd @@ -12,7 +12,7 @@ const JUMP_VELOCITY = -400.0 enum FaceDirection{LEFT, RIGHT} var facing:FaceDirection = FaceDirection.RIGHT -enum State{IDLE, RUN, JUMP, FALLING} +enum State{IDLE, RUN, JUMP, FALLING, HURT, DEATH} var current_state:State = State.IDLE var pushTarget @@ -20,6 +20,8 @@ var pushEnabled = false var direction var upJump = false +signal deathAnimationCompleteSignal + func _physics_process(delta: float) -> void: #game loop handle_input() @@ -60,6 +62,10 @@ func update_animation(): player_graphic.play("jump") State.FALLING: player_graphic.play("falling") + State.HURT: + player_graphic.play("hurt") + State.DEATH: + player_graphic.play("death") func handle_movement(delta): # Add the gravity. @@ -131,9 +137,15 @@ func _on_animation_finished() -> void: match current_state: State.JUMP: upJump = false + State.HURT: + current_state = State.IDLE + State.DEATH: + deathAnimationCompleteSignal.emit() func playerTakesDamage(health): print("Player sees remaining health "+str(health)) + current_state = State.HURT func playerDies(): print("Player sees he ded.") + current_state = State.DEATH diff --git a/scripts/coin.gd b/scripts/coin.gd index 010e67a..2b975fb 100644 --- a/scripts/coin.gd +++ b/scripts/coin.gd @@ -12,5 +12,5 @@ func _process(delta: float) -> void: func _on_body_entered(body: Node2D) -> void: - print("Coin sees collision") + coinCollectedSignal.emit(body, self) diff --git a/scripts/crate.gd b/scripts/crate.gd new file mode 100644 index 0000000..7f42195 --- /dev/null +++ b/scripts/crate.gd @@ -0,0 +1 @@ +class_name Crate extends RigidBody2D diff --git a/scripts/crate.gd.uid b/scripts/crate.gd.uid new file mode 100644 index 0000000..06d8724 --- /dev/null +++ b/scripts/crate.gd.uid @@ -0,0 +1 @@ +uid://cb5w1tpwn7cn0 diff --git a/scripts/gamecontroller.gd b/scripts/gamecontroller.gd index 8eac5c8..7024431 100644 --- a/scripts/gamecontroller.gd +++ b/scripts/gamecontroller.gd @@ -3,23 +3,30 @@ extends Node2D var totalCoinsAvailable =0 var timer = Timer.new() -var timeAvailable = 5 +var timeAvailable var level="res://scenes/game.tscn" var levelTwo = "res://scenes/level2.tscn" +var startingCoins=0 var levels = ["res://scenes/game.tscn","res://scenes/level2.tscn","res://scenes/level3.tscn"] -var timers = [10,15,9] +var timers = [30,15,9] var currentLevel = 0 signal destroySignal(body) signal levelChangeSignal(level) signal playerTakeDamageSignal(health) signal playerDiesSignal +signal bulletDamageEnemySignal(body) +signal countDownSignal(timeRemaining) +signal coinCountSignal(coinsRemaining, startingCoins) var player:CharacterStats var enemy:CharacterStats var playerHealth:int + +var enemiesDict = {} + # Called when the node enters the scene tree for the first time. func _ready() -> void: player = load("res://scripts/rscs/playerStats.tres") @@ -36,13 +43,15 @@ func _ready() -> void: func secondCounter() -> void: timeAvailable -=1 + countDownSignal.emit(timeAvailable) if timeAvailable <= 0: print("YOU LOSE") #reload the current scene levelChangeSignal.emit(levels[currentLevel]) func reset() -> void: + startingCoins=0 timeAvailable = timers[currentLevel] - + playerHealth = player.starting_health # Called every frame. 'delta' is the elapsed time since the previous frame. func _process(delta: float) -> void: pass @@ -60,24 +69,46 @@ func _on_areatrigger(effect, body) -> void: print("Player health is now "+str(playerHealth)) func totalCoinCount(totalCoins): - + if startingCoins == 0: + startingCoins = totalCoins totalCoinsAvailable = totalCoins -func coinCollected(body, coin): - destroySignal.emit(coin) - totalCoinsAvailable -=1 - if totalCoinsAvailable <= 0: - print("YOU WIN") - currentLevel +=1 - if currentLevel >= levels.size(): - currentLevel = 0 - - levelChangeSignal.emit(levels[currentLevel]) +func coinCollected(body, coin): + if not body is Crate: + destroySignal.emit(coin) + totalCoinsAvailable -=1 + coinCountSignal.emit(totalCoinsAvailable, startingCoins) + if totalCoinsAvailable <= 0: + print("YOU WIN") + currentLevel +=1 + if currentLevel >= levels.size(): + currentLevel = 0 + + levelChangeSignal.emit(levels[currentLevel]) func playerAttacked(body, slime): print("GC knows player attacked") - playerHealth -= enemy.meleeDamage + playerHealth -= enemiesDict[slime].damage if playerHealth <=0: playerDiesSignal.emit() else: playerTakeDamageSignal.emit(playerHealth) + +func playerDead(): + levelChangeSignal.emit(levels[currentLevel]) + +func bulletDamage(area, bullet): + enemiesDict[area].health -= player.rangeDamage + if enemiesDict[area].health <=0: + destroySignal.emit(area) + enemiesDict.erase(area) + else: + bulletDamageEnemySignal.emit(area) +func addEnemyToLevel(slime): + var randHealth:int = randi() % 20 + var randDamage:int = randi() % 10 + var enemyStat = { + "health":enemy.starting_health + randHealth, + "damage":enemy.meleeDamage + randDamage + } + enemiesDict[slime]=enemyStat diff --git a/scripts/scene_manager.gd b/scripts/scene_manager.gd index bd0cf2f..9befafe 100644 --- a/scripts/scene_manager.gd +++ b/scripts/scene_manager.gd @@ -7,6 +7,7 @@ var totalAllowedBullets = 7 @onready var coins: Node2D = $"../Coins" @onready var enemies: Node2D = $"../Enemies" @onready var player: Player = $"../Player" +@onready var ui: Control = $"../CanvasLayer/Ui" func _ready() -> void: Gamecontroller.reset() @@ -18,14 +19,20 @@ func _ready() -> void: for obj in enemies.get_children(): if obj is Slime: obj.playerDamageSignal.connect(Gamecontroller.playerAttacked) + Gamecontroller.addEnemyToLevel(obj) + Gamecontroller.bulletDamageEnemySignal.connect(obj.takeDamage) updateCoins() Gamecontroller.destroySignal.connect(destroyItem) Gamecontroller.levelChangeSignal.connect(changeScene) Gamecontroller.playerTakeDamageSignal.connect(player.playerTakesDamage) + if ui: + Gamecontroller.playerTakeDamageSignal.connect(ui.healthUpdate) + Gamecontroller.countDownSignal.connect(ui.timerUpdate) + Gamecontroller.coinCountSignal.connect(ui.coinsUpdate) Gamecontroller.playerDiesSignal.connect(player.playerDies) - + player.deathAnimationCompleteSignal.connect(Gamecontroller.playerDead) #call this any time the coins change func updateCoins() -> void: var coinCount = 0 @@ -46,6 +53,7 @@ func bulletFactory(): if bulletArray.size() < totalAllowedBullets: #make a new bullet mybullet = bullet.instantiate() + mybullet.bulletDamageSignal.connect(Gamecontroller.bulletDamage) owner.add_child(mybullet) else: #recycle bullet diff --git a/scripts/slime.gd b/scripts/slime.gd index a08b8d8..9079f51 100644 --- a/scripts/slime.gd +++ b/scripts/slime.gd @@ -5,6 +5,7 @@ class_name Slime extends Area2D @onready var left_down_cast: RayCast2D = $LeftDownCast @onready var right_down_cast: RayCast2D = $RightDownCast + var speed:int = 100 var direction = 1 @@ -16,6 +17,7 @@ func _ready() -> void: # Called every frame. 'delta' is the elapsed time since the previous frame. func _process(delta: float) -> void: + if not right_down_cast.is_colliding(): direction = -1 slime_graphic.flip_h = true @@ -25,12 +27,14 @@ func _process(delta: float) -> void: slime_graphic.flip_h = false if right_side_cast.is_colliding(): - direction = -1 - slime_graphic.flip_h = true + if not right_side_cast.get_collider() is Player && not right_side_cast.get_collider() is Slime: + direction = -1 + slime_graphic.flip_h = true if left_side_cast.is_colliding(): - direction = 1 - slime_graphic.flip_h = false + if not left_side_cast.get_collider() is Player && not left_side_cast.get_collider() is Slime: + direction = 1 + slime_graphic.flip_h = false position.x += direction * speed * delta @@ -39,3 +43,7 @@ func _on_body_entered(body: Node2D) -> void: if body is Player: print("Slime attack!") playerDamageSignal.emit(body, self) + +func takeDamage(body): + if body == self: + slime_graphic.play("hurt") diff --git a/scripts/ui.gd b/scripts/ui.gd new file mode 100644 index 0000000..7718c7d --- /dev/null +++ b/scripts/ui.gd @@ -0,0 +1,18 @@ +extends Control +@onready var health: Label = $VBoxContainer/HBoxContainer/MarginContainer/Health +@onready var timer: Label = $VBoxContainer/HBoxContainer/MarginContainer2/Timer +@onready var coins: Label = $VBoxContainer/HBoxContainer/MarginContainer3/Coins + + +# Called when the node enters the scene tree for the first time. +func _ready() -> void: + pass + +func healthUpdate(healthRemaining): + health.text = "Health: "+str(healthRemaining) + +func timerUpdate(timeRemaining): + timer.text = str(timeRemaining) + +func coinsUpdate(coinsRemaining, startingCoins): + coins.text="Coins: "+str(coinsRemaining)+"/"+str(startingCoins) diff --git a/scripts/ui.gd.uid b/scripts/ui.gd.uid new file mode 100644 index 0000000..45b2cb5 --- /dev/null +++ b/scripts/ui.gd.uid @@ -0,0 +1 @@ +uid://cguclh40lh0w1