Skip to content

Commit

Permalink
Enemy death effect
Browse files Browse the repository at this point in the history
  • Loading branch information
fabioarnold committed Dec 8, 2023
1 parent 29bf140 commit 20d0472
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
36 changes: 23 additions & 13 deletions src/Enemy.zig
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ box: Box = undefined,
state: u8 = 0,
health: u8 = 0,
invincibility_frames: u8 = 0,
death_frames: u8 = 0,
frame: u8 = 0,
counter: i32 = 0,
counter: u32 = 0,
face_left: bool = false,

pub fn load() void {
Expand All @@ -44,19 +45,26 @@ pub fn activate(self: *Self, @"type": Type, box: Box) void {
}

pub fn tick(self: *Self, r: std.rand.Random, game: *GameData, attribs: []const Attrib) void {
if (self.health == 0) {
self.death_frames += 1;
if (self.death_frames == 5) {
self.active = false;
return;
}
}
self.invincibility_frames -|= 1;
if (self.invincibility_frames > 0) return;

switch (self.type) {
.gopher => tickGopher(self, r, game, attribs),
}
}

pub fn hurt(self: *Self, damage: u8) void {
if (self.invincibility_frames > 0) return;
if (self.invincibility_frames > 0 or self.health == 0) return;

self.health -|= damage;
self.invincibility_frames = 30;
if (self.health == 0) {
self.active = false;
}
}

pub fn draw(self: Self) void {
Expand All @@ -73,14 +81,12 @@ const GopherState = enum(u8) {
fn tickGopher(self: *Self, r: std.rand.Random, game: *GameData, attribs: []const Attrib) void {
const room = game.getCurrentRoom();
const state: *GopherState = @ptrCast(&self.state);
self.invincibility_frames -|= 1;
if (self.invincibility_frames > 0) return;
switch (state.*) {
.idle => {
self.frame = 0;
self.face_left = self.counter & 16 != 0;
if (self.counter <= 0) {
self.counter = r.intRangeLessThan(i32, 100, 500);
if (self.counter == 0) {
self.counter = r.intRangeLessThan(u32, 100, 500);
state.* = .walk;
}
},
Expand All @@ -93,20 +99,24 @@ fn tickGopher(self: *Self, r: std.rand.Random, game: *GameData, attribs: []const
} else {
self.box.x += amount;
}
if (self.counter <= 0) {
self.counter = r.intRangeLessThan(i32, 100, 200);
if (self.counter == 0) {
self.counter = r.intRangeLessThan(u32, 100, 200);
state.* = .idle;
}
},
}
self.counter -= 1;
self.counter -|= 1;

if (self.box.overlaps(game.player.box)) {
if (self.health > 0 and self.box.overlaps(game.player.box)) {
game.player.hurt(4);
}
}

fn drawGopher(self: Self) void {
if (self.health == 0) {
effects.drawDeathEffectSmall(self.box.x + @divTrunc(self.box.w, 2), self.box.y + @divTrunc(self.box.h, 2), self.death_frames);
return;
}
if (self.invincibility_frames % 6 >= 3) {
Renderer.Sprite.draw(effects.hurt_fx, self.box.x - 4, self.box.y);
return;
Expand Down
7 changes: 7 additions & 0 deletions src/effects.zig
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ pub fn drawDeathEffect(x: i32, y: i32, counter: u32) void {
}
}

pub fn drawDeathEffectSmall(x: i32, y: i32, counter: u8) void {
const frame = counter;
if (frame > 4) return;
const src_rect = Rect.init(frame * 24, 0, 24, 24);
Sprite.drawFrame(effects_tex, src_rect, x - 12, y - 12);
}

pub fn drawTeleportEffect(player_x: i32, player_y: i32, counter: u8) void {
if (counter < 32) return;
const frame: i32 = counter - 32;
Expand Down

0 comments on commit 20d0472

Please sign in to comment.