a game about throwing hammers made for the github game off
player health ui
bendn 2022-11-26
parent 7894403 · commit 45bc1dd
-rw-r--r--Main.tscn4
-rw-r--r--player/player.gd61
-rw-r--r--ui/hud.tscn3
-rw-r--r--ui/hud/health_meter.gd11
-rw-r--r--ui/hud/hud.tscn30
5 files changed, 82 insertions, 27 deletions
diff --git a/Main.tscn b/Main.tscn
index 4028bc7..0049636 100644
--- a/Main.tscn
+++ b/Main.tscn
@@ -1,4 +1,4 @@
-[gd_scene load_steps=24 format=3 uid="uid://dcaws2iac7w3y"]
+[gd_scene load_steps=24 format=3 uid="uid://06slp4crd6oj"]
[ext_resource type="Script" path="res://Main.gd" id="1_l4fqe"]
[ext_resource type="PackedScene" uid="uid://umj2kojm6mlo" path="res://player/player.tscn" id="1_ug1uc"]
@@ -22,7 +22,7 @@
[ext_resource type="PackedScene" uid="uid://dcbnac3k5c2bh" path="res://levels/rand/16.tscn" id="19_ko67h"]
[ext_resource type="PackedScene" uid="uid://pk206siyyege" path="res://levels/rand/17.tscn" id="20_f85eu"]
[ext_resource type="PackedScene" uid="uid://dbcrx23xsfrlp" path="res://levels/rand/18.tscn" id="21_rvdjm"]
-[ext_resource type="PackedScene" uid="uid://bnsgjilr4mn10" path="res://ui/hud.tscn" id="23_5dqpk"]
+[ext_resource type="PackedScene" uid="uid://bnsgjilr4mn10" path="res://ui/hud/hud.tscn" id="23_5dqpk"]
[node name="Main" type="Node2D"]
texture_filter = 1
diff --git a/player/player.gd b/player/player.gd
index 000d711..f6a1131 100644
--- a/player/player.gd
+++ b/player/player.gd
@@ -6,26 +6,31 @@ const JumpEffect := preload("res://fx/jump.tscn")
const DoubleJumpEffect := preload("res://fx/double_jump.tscn")
const WallJumpEffect := preload("res://fx/wall_dust.tscn")
+signal hp_changed(health: int)
+
## Accel
-@export var ACCELERATION := 512
+@export var accel := 512
## Topspeed
-@export var MAX_SPEED := 64
+@export var top_speed := 64
## Jump force
-@export var JUMP_FORCE := 150
+@export var jump_force := 150
## The topspeed at which we slide down the wall
-@export var MAX_WALL_SLIDE_SPEED := 110
+@export var max_wall_slide_fall_speed := 110
## The standard speed at which we slide down the wall
-@export var WALL_SLIDE_SPEED := 42
+@export var wall_slide_fall_speed := 42
## Friction
-@export var FRICTION := 0.25
+@export var frict := 0.25
## How much less movement control to have in the air
-@export var AIR_MOVEMENT_MODIFIER := 0.95
+@export var air_movement_modifier := 0.95
+
+## Max hp
+@export var max_health := 3
@onready var sprite := $Sprite as Sprite2D
@onready var anims := $Player as AnimationPlayer
@@ -55,6 +60,14 @@ var just_jumped := false
var has_hammer := false
+var health := max_health:
+ set(hp):
+ health = hp
+ hp_changed.emit(hp)
+ if hp == 0:
+ queue_free()
+
+
func _physics_process(delta: float) -> void:
just_jumped = false
match state:
@@ -94,20 +107,20 @@ func dust() -> void:
## Applys gravity.
func apply_gravity(delta: float) -> void:
velocity.y += GRAVITY * delta
- velocity.y = minf(velocity.y, JUMP_FORCE)
+ velocity.y = minf(velocity.y, jump_force)
## Applys force with the [param input] of the player.
func apply_force(input: float, delta: float) -> void:
if input != 0:
- velocity.x += input * ACCELERATION * delta
- velocity.x = clampf(velocity.x, -MAX_SPEED, MAX_SPEED)
+ velocity.x += input * accel * delta
+ velocity.x = clampf(velocity.x, -top_speed, top_speed)
if not is_on_floor():
- velocity.x *= AIR_MOVEMENT_MODIFIER
+ velocity.x *= air_movement_modifier
## Applys friction to the player.
func apply_friction(input: float) -> void:
if input == 0 and not is_zero_approx(velocity.x) and is_on_floor():
- velocity.x = lerpf(velocity.x, 0, FRICTION)
+ velocity.x = lerpf(velocity.x, 0, frict)
## Plays animations for the move state.
func animate() -> void:
@@ -134,15 +147,15 @@ func play(anim: StringName, speed: float = 1.0) -> void:
func jump_check() -> void:
var want2jump := Input.is_action_just_pressed(&"jump")
if want2jump and (is_on_floor() or coyote.time_left > 0):
- jump(JUMP_FORCE)
+ jump(jump_force)
just_jumped = true
else:
- if want2jump and velocity.y < -JUMP_FORCE / 2:
- velocity.y = -JUMP_FORCE / 2
+ if want2jump and velocity.y < -jump_force / 2:
+ velocity.y = -jump_force / 2
if want2jump and double_jump == true:
+ jump(jump_force * .75)
double_jump = false
- jump(JUMP_FORCE * .75)
## Jumps with [param force] force.
func jump(force: float) -> void:
@@ -189,8 +202,8 @@ func get_wall_axis() -> int:
## Checks if we should jump off the [param wall_axis].
func wall_slide_jump_check(wall_axis: int) -> void:
if Input.is_action_just_pressed("jump"):
- velocity.x = wall_axis * MAX_SPEED
- velocity.y = -JUMP_FORCE / 1.25
+ velocity.x = wall_axis * top_speed
+ velocity.y = -jump_force / 1.25
state = State.MOVE
wall_dust(wall_axis)
# SoundFx.play("Jump", -20)
@@ -204,20 +217,20 @@ func wall_dust(wall_axis: int) -> void:
## Slides down the wall.
func wall_slide_drop(delta: float) -> void:
- var max_slide_speed := WALL_SLIDE_SPEED
+ var max_slide_speed := wall_slide_fall_speed
if Input.is_action_pressed("down"):
- max_slide_speed = MAX_WALL_SLIDE_SPEED
+ max_slide_speed = max_wall_slide_fall_speed
velocity.y = min(velocity.y + GRAVITY * delta, max_slide_speed)
## Checks if we should detatch from the wall.
func wall_detatch(wall_axis: int, delta: float) -> void:
var detached := false
if Input.is_action_just_pressed("right"):
- velocity.x = ACCELERATION * delta
+ velocity.x = accel * delta
detached = true
if Input.is_action_just_pressed("left"):
- velocity.x = -ACCELERATION * delta
+ velocity.x = -accel * delta
detached = true
if detached:
@@ -226,3 +239,7 @@ func wall_detatch(wall_axis: int, delta: float) -> void:
if wall_axis == 0 or is_on_floor():
state = State.MOVE
+
+
+func hit(damage: int) -> void:
+ health -= damage
diff --git a/ui/hud.tscn b/ui/hud.tscn
deleted file mode 100644
index e2e7ee4..0000000
--- a/ui/hud.tscn
+++ /dev/null
@@ -1,3 +0,0 @@
-[gd_scene format=3 uid="uid://bnsgjilr4mn10"]
-
-[node name="HUD" type="CanvasLayer"]
diff --git a/ui/hud/health_meter.gd b/ui/hud/health_meter.gd
new file mode 100644
index 0000000..5905e66
--- /dev/null
+++ b/ui/hud/health_meter.gd
@@ -0,0 +1,11 @@
+extends Control
+
+@onready var full := $Full as TextureRect
+
+
+func _ready():
+ Globals.player.hp_changed.connect(_hp_changed)
+
+
+func _hp_changed(hp: int):
+ full.size.x = hp * 5 + 1
diff --git a/ui/hud/hud.tscn b/ui/hud/hud.tscn
new file mode 100644
index 0000000..00eba1d
--- /dev/null
+++ b/ui/hud/hud.tscn
@@ -0,0 +1,30 @@
+[gd_scene load_steps=4 format=3 uid="uid://bnsgjilr4mn10"]
+
+[ext_resource type="Texture2D" uid="uid://3dgefswp61jy" path="res://assets/ui/health.png" id="1_8qpc0"]
+[ext_resource type="Script" path="res://ui/hud/health_meter.gd" id="1_yn38u"]
+[ext_resource type="Texture2D" uid="uid://tpy1gjydytic" path="res://assets/ui/health_empty.png" id="2_pqvic"]
+
+[node name="HUD" type="CanvasLayer"]
+
+[node name="HealthMeter" type="Control" parent="."]
+layout_mode = 3
+anchors_preset = 0
+offset_left = 11.0
+offset_top = 8.0
+offset_right = 27.0
+offset_bottom = 22.0
+script = ExtResource("1_yn38u")
+
+[node name="Empty" type="TextureRect" parent="HealthMeter"]
+layout_mode = 0
+offset_right = 16.0
+offset_bottom = 12.0
+texture = ExtResource("2_pqvic")
+stretch_mode = 1
+
+[node name="Full" type="TextureRect" parent="HealthMeter"]
+layout_mode = 0
+offset_right = 16.0
+offset_bottom = 12.0
+texture = ExtResource("1_8qpc0")
+stretch_mode = 1