-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial explosion particle effect
- Loading branch information
1 parent
272926c
commit 0fba4e5
Showing
4 changed files
with
187 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
const std = @import("std"); | ||
const raylib = @import("raylib"); | ||
const Shared = @import("../Shared.zig").Shared; | ||
const Color = @import("../Colors.zig").Color; | ||
const Particle = @import("Particle.zig").Particle; | ||
|
||
pub const Explosion = struct { | ||
active: bool, | ||
position: raylib.Vector2, | ||
lifeSpawn: f32, | ||
particle: [PARTICLE_COUNT]Particle, | ||
blastRadius: f32, | ||
|
||
const ANIMATION_SPEED_MOD = 15; | ||
const PARTICLE_COUNT = 50; | ||
|
||
pub fn init(position: raylib.Vector2, color: Color, blastRadius: f32) Explosion { | ||
var particles: [PARTICLE_COUNT]Particle = undefined; | ||
for (0..PARTICLE_COUNT) |i| { | ||
particles[i] = Particle.init(position, GetRandomColor(color), Shared.Random.Get().float(f32) * 4); | ||
} | ||
|
||
return Explosion{ | ||
.active = true, | ||
.position = position, | ||
.lifeSpawn = 0, | ||
.particle = particles, | ||
.blastRadius = blastRadius, | ||
}; | ||
} | ||
|
||
pub inline fn GetRandomColor(color: Color) raylib.Color { | ||
switch (Shared.Random.Get().intRangeAtMost(u8, 0, 2)) { | ||
0 => { | ||
return color.Base; | ||
}, | ||
1 => { | ||
return color.Light; | ||
}, | ||
else => { | ||
return color.Dark; | ||
}, | ||
} | ||
} | ||
|
||
pub inline fn Update(self: *@This(), screenSize: raylib.Vector2) void { | ||
if (self.active) { | ||
self.lifeSpawn += raylib.getFrameTime() * ANIMATION_SPEED_MOD; | ||
|
||
var nonActiveCount: u32 = 0; | ||
for (0..PARTICLE_COUNT) |i| { | ||
var particle = self.particle[i]; | ||
particle.Update(screenSize, self.blastRadius * 5); | ||
if (self.lifeSpawn < 5) { | ||
if (!particle.active) { | ||
particle.Reset(self.position); | ||
} | ||
} else { | ||
if (!particle.active) nonActiveCount += 1; | ||
} | ||
|
||
self.particle[i] = particle; | ||
} | ||
self.active = nonActiveCount < PARTICLE_COUNT; | ||
} | ||
} | ||
|
||
pub inline fn Draw(self: @This()) void { | ||
if (self.active) { | ||
for (0..PARTICLE_COUNT) |i| { | ||
self.particle[i].Draw(); | ||
} | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
const std = @import("std"); | ||
const raylib = @import("raylib"); | ||
const Shared = @import("../Shared.zig").Shared; | ||
|
||
pub const Particle = struct { | ||
position: raylib.Vector2, | ||
speed: raylib.Vector2, | ||
radius: f32, | ||
lifeSpawn: f32, | ||
active: bool, | ||
color: raylib.Color, | ||
|
||
const ANIMATION_SPEED_MOD = 100; | ||
|
||
const PARTICLE_MAX_SPEED: f32 = 100; | ||
const PARTICLE_MIN_SPEED: f32 = 20; | ||
|
||
const LifeSpanLength = 50; | ||
|
||
pub inline fn init(position: raylib.Vector2, color: raylib.Color, radius: f32) Particle { | ||
const rotation = Shared.Random.Get().float(f32) * 360; | ||
return Particle{ | ||
.position = position, | ||
.speed = raylib.Vector2.init( | ||
@cos((std.math.degreesToRadians(f32, rotation) * (PARTICLE_MAX_SPEED - PARTICLE_MIN_SPEED)) + PARTICLE_MIN_SPEED), | ||
@sin((std.math.degreesToRadians(f32, rotation) * (PARTICLE_MAX_SPEED - PARTICLE_MIN_SPEED)) + PARTICLE_MIN_SPEED), | ||
), | ||
.radius = radius, | ||
.active = true, | ||
.lifeSpawn = Shared.Random.Get().float(f32) * LifeSpanLength, | ||
.color = color.alpha((Shared.Random.Get().float(f32) * 0.2) + 0.55), | ||
}; | ||
} | ||
|
||
pub inline fn Reset(self: *@This(), position: raylib.Vector2) void { | ||
self.position = position; | ||
const rotation = Shared.Random.Get().float(f32) * 360; | ||
self.speed = raylib.Vector2.init( | ||
@cos((std.math.degreesToRadians(f32, rotation) * (PARTICLE_MAX_SPEED - PARTICLE_MIN_SPEED)) + PARTICLE_MIN_SPEED), | ||
@sin((std.math.degreesToRadians(f32, rotation) * (PARTICLE_MAX_SPEED - PARTICLE_MIN_SPEED)) + PARTICLE_MIN_SPEED), | ||
); | ||
self.active = true; | ||
self.lifeSpawn = Shared.Random.Get().float(f32) * (LifeSpanLength / 2); | ||
} | ||
|
||
pub inline fn Update(self: *@This(), screenSize: raylib.Vector2, endLifeSpan: f32) void { | ||
if (self.active) { | ||
self.lifeSpawn += raylib.getFrameTime() * ANIMATION_SPEED_MOD; | ||
|
||
// Movement | ||
self.position.x += self.speed.x; | ||
self.position.y -= self.speed.y; | ||
|
||
// Collision logic: particle vs walls | ||
if (self.position.x > screenSize.x + self.radius) { | ||
self.active = false; | ||
self.lifeSpawn = 0; | ||
} else if (self.position.x < 0 - self.radius) { | ||
self.active = false; | ||
self.lifeSpawn = 0; | ||
} | ||
if (self.position.y > screenSize.y + self.radius) { | ||
self.active = false; | ||
self.lifeSpawn = 0; | ||
} else if (self.position.y < 0 - self.radius) { | ||
self.active = false; | ||
self.lifeSpawn = 0; | ||
} | ||
|
||
// Life of particle | ||
if (self.lifeSpawn >= endLifeSpan) { | ||
self.position.x = 0; | ||
self.position.y = 0; | ||
self.speed.x = 0; | ||
self.speed.y = 0; | ||
self.lifeSpawn = 0; | ||
self.active = false; | ||
} | ||
} | ||
} | ||
|
||
pub inline fn Draw(self: @This()) void { | ||
if (self.active) { | ||
const color = if (self.lifeSpawn > 15) self.color else self.color.alpha((30 - self.lifeSpawn) / 30); | ||
raylib.drawCircleV( | ||
self.position, | ||
self.radius, | ||
color, | ||
); | ||
} | ||
} | ||
}; |