draw player healthbar

This commit is contained in:
Rokas Puzonas 2024-02-18 20:43:26 +02:00
parent 79905b3546
commit f2545d27e9

View File

@ -77,6 +77,7 @@ const Player = struct {
input: PlayerInput = PlayerInput.init(),
health: u32 = 3,
maxHealth: u32 = 3,
dead: bool = false,
invincibility: f32 = 0,
@ -241,6 +242,7 @@ fn tickPlayer(self: *Self) void {
var player = &self.player;
var rope = &self.rope;
player.invincibility = @max(0, player.invincibility - dt);
player.acceleration = rl.Vector2.zero();
if (player.alive()) {
@ -337,12 +339,15 @@ fn tickEnemies(self: *Self) !void {
}
fn damagePlayer(self: *Self) void {
if (self.player.alive()) {
self.player.health -= 1;
const player = &self.player;
if (player.alive()) {
player.health -= 1;
player.invincibility = 0.8;
}
if (!self.player.alive()) {
self.player.input.deactivate();
if (!player.alive()) {
player.input.deactivate();
}
}
@ -544,16 +549,56 @@ fn togglePaused(self: *Self) void {
}
}
fn drawPlayer(self: *Self) void {
var player = &self.player;
const handPosition = player.getHandPosition();
const healthWidth = 5;
const healthPercent = @as(f32, @floatFromInt(player.health)) / @as(f32, @floatFromInt(player.maxHealth));
var healthColor = rl.GREEN;
if (player.invincibility > 0 and @rem(player.invincibility, 0.2) < 0.1) {
healthColor = rl.RED;
}
rl.DrawCircleSector(
player.position,
playerSize + healthWidth,
0,
360 * healthPercent,
0,
healthColor,
);
for (0..(player.health+1)) |i| {
const percent = @as(f32, @floatFromInt(i)) / @as(f32, @floatFromInt(player.maxHealth));
rl.DrawLineV(
player.position,
player.position.add((rl.Vector2{ .x = playerSize + healthWidth, .y = 0 }).rotate(percent * 2 * rl.PI)),
rl.BLACK
);
}
rl.DrawCircle(
@intFromFloat(player.position.x),
@intFromFloat(player.position.y),
playerSize,
rl.RAYWHITE
);
rl.DrawCircle(
@intFromFloat(handPosition.x),
@intFromFloat(handPosition.y),
5,
rl.RAYWHITE
);
}
pub fn tick(self: *Self) !void {
rl.ClearBackground(rl.BLACK);
rl.ClearBackground(rl.BROWN);
const screenSize = getScreenSize();
const dt = rl.GetFrameTime();
self.camera.offset.x = screenSize.x/2;
self.camera.offset.y = screenSize.y/2;
self.camera.target = self.player.position;
self.camera.target = rl.Vector2Lerp(self.camera.target, self.player.position, 10 * dt);
var player = &self.player;
var enemies = &self.enemies;
var rope = &self.rope;
@ -571,31 +616,12 @@ pub fn tick(self: *Self) !void {
}
const deathBallPosition = rope.nodePositions.get(rope.nodePositions.len-1);
const handPosition = player.getHandPosition();
rl.BeginMode2D(self.camera);
{
rl.DrawCircle(0, 0, 5, rl.GOLD);
rl.DrawCircle(
@intFromFloat(player.position.x),
@intFromFloat(player.position.y),
playerSize,
rl.RAYWHITE
);
rl.DrawCircle(
@intFromFloat(handPosition.x),
@intFromFloat(handPosition.y),
5,
rl.RAYWHITE
);
rl.DrawLine(
@intFromFloat(player.position.x),
@intFromFloat(player.position.y),
@intFromFloat(player.position.x + player.velocity.x),
@intFromFloat(player.position.y + player.velocity.y),
rl.GREEN
);
self.drawPlayer();
for (enemies.items) |*enemy| {
var color = enemy.color;