This commit is contained in:
Jonathan Wyss 2024-11-27 13:57:56 +01:00
parent 3149c01bc1
commit fe6cd670e8
2 changed files with 92 additions and 120 deletions

View File

@ -25,7 +25,7 @@
.dependencies = .{ .dependencies = .{
.@"raylib-zig" = .{ .@"raylib-zig" = .{
.url = "https://github.com/Not-Nik/raylib-zig/archive/devel.tar.gz", .url = "https://github.com/Not-Nik/raylib-zig/archive/devel.tar.gz",
.hash = "12202f8c415153088be8df39a51e0a4c9d402afd403422a0dcc9afdd417e437a6fdb", .hash = "1220c61380facb4480c01109fda97cf1a37f45308c522a84fdea142ca625593ddc2a",
}, },
}, },
.paths = .{ .paths = .{

View File

@ -6,7 +6,6 @@ const allocator = std.heap.page_allocator;
var prng = std.rand.DefaultPrng.init(124346556); var prng = std.rand.DefaultPrng.init(124346556);
const rand = prng.random(); const rand = prng.random();
fn randMinMaxFloat(min: f32, max: f32) f32 { fn randMinMaxFloat(min: f32, max: f32) f32 {
@ -45,25 +44,21 @@ const Particle = struct {
pub fn update(self: *Self) void { pub fn update(self: *Self) void {
self.velocity.add(self.acceleration); self.velocity.add(self.acceleration);
self.position.add(self.velocity); self.position.add(self.velocity);
if (self.position.a.y > screenHeight) if (self.position.a.y > screenHeight) {
{
self.velocity.a.y *= -0.9; self.velocity.a.y *= -0.9;
// self.acceleration.a.y *= -0.5; // self.acceleration.a.y *= -0.5;
} }
if (self.lifespan > 0) { if (self.lifespan > 0) {
self.lifespan -= 1; self.lifespan -= 1;
} } else {
else
{
self.show = 0; self.show = 0;
} }
self.acceleration.a.x = 0; self.acceleration.a.x = 0;
self.acceleration.a.y = 0; self.acceleration.a.y = 0;
self.acceleration.a.z = 0; self.acceleration.a.z = 0;
} }
pub fn spawn(self:*Self,xaccel : f32,yaccel : f32) void pub fn spawn(self: *Self, xaccel: f32, yaccel: f32) void {
{
const xrnr = randMinMaxFloat(-xaccel, xaccel); const xrnr = randMinMaxFloat(-xaccel, xaccel);
const yrnr = randMinMaxFloat(-yaccel, yaccel); const yrnr = randMinMaxFloat(-yaccel, yaccel);
@ -89,14 +84,11 @@ const Particle = struct {
} }
}; };
const Emitter = struct const Emitter = struct {
{
particleCount: u32 = 0, particleCount: u32 = 0,
particles: []Particle = undefined, particles: []Particle = undefined,
pub fn init(self:*Emitter) !void pub fn init(self: *Emitter) !void {
{
var xrnr = randMinMaxFloat(-1, 1); var xrnr = randMinMaxFloat(-1, 1);
var yrnr = randMinMaxFloat(-0.5, 0.5); var yrnr = randMinMaxFloat(-0.5, 0.5);
self.particles = try allocator.alloc(Particle, 500000); self.particles = try allocator.alloc(Particle, 500000);
@ -106,24 +98,19 @@ const Emitter = struct
p.* = Particle{ .velocity = .{ .a = .{ .x = 1, .y = -3 } }, .acceleration = .{ .a = .{ .x = xrnr, .y = yrnr } } }; p.* = Particle{ .velocity = .{ .a = .{ .x = 1, .y = -3 } }, .acceleration = .{ .a = .{ .x = xrnr, .y = yrnr } } };
} }
} }
pub fn emit(self:*Emitter,count : u32,xaccel : f32,yaccel : f32) void pub fn emit(self: *Emitter, count: u32, xaccel: f32, yaccel: f32) void {
{
var i: u32 = 0; var i: u32 = 0;
for(self.particles) |*p| for (self.particles) |*p| {
{ if (p.show == 0) {
if(p.show == 0)
{
i += 1; i += 1;
p.spawn(xaccel, yaccel); p.spawn(xaccel, yaccel);
} }
if(i >= count) if (i >= count) {
{
break; break;
} }
} }
} }
pub fn update(self:*Emitter,texture : rl.Texture2D) void pub fn update(self: *Emitter, texture: rl.Texture2D) void {
{
var posx: i32 = 0; var posx: i32 = 0;
var posy: i32 = 0; var posy: i32 = 0;
const att: Attractor = .{ .vec = .{ .a = .{ .x = 10, .y = 10, .z = 10 } } }; const att: Attractor = .{ .vec = .{ .a = .{ .x = 10, .y = 10, .z = 10 } } };
@ -132,9 +119,7 @@ const Emitter = struct
att.draw(); att.draw();
att2.draw(); att2.draw();
for (self.particles) |*p| { for (self.particles) |*p| {
if(p.show == 1) if (p.show == 1) {
{
p.applyGravity(2); p.applyGravity(2);
p.applyForce(att.attract(p.*)); p.applyForce(att.attract(p.*));
p.applyForce(att2.attract(p.*)); p.applyForce(att2.attract(p.*));
@ -148,38 +133,30 @@ const Emitter = struct
} }
} }
}; };
const Attractor = struct const Attractor = struct {
{
vec: pr.Vec, vec: pr.Vec,
pub fn attract(self:Attractor,particle : Particle) pr.Vec pub fn attract(self: Attractor, particle: Particle) pr.Vec {
{
var vec = self.vec.sub(particle.position); var vec = self.vec.sub(particle.position);
if( vec.a.x != 0 and vec.a.y != 0) if (vec.a.x != 0 and vec.a.y != 0) {
{
vec.a.x = vec.a.x / 200; vec.a.x = vec.a.x / 200;
vec.a.y = vec.a.y / 200; vec.a.y = vec.a.y / 200;
} }
return vec; return vec;
} }
pub fn draw(self:Attractor) void pub fn draw(self: Attractor) void {
{
const posx: i32 = @intFromFloat(screenWidth / 2 + self.vec.a.x); const posx: i32 = @intFromFloat(screenWidth / 2 + self.vec.a.x);
const posy: i32 = @intFromFloat(screenHeight / 2 + self.vec.a.y); const posy: i32 = @intFromFloat(screenHeight / 2 + self.vec.a.y);
rl.drawRectangle(posx, posy, 10, 10, rl.Color{ .r = 10, .g = 10, .b = 120, .a = 255 }); rl.drawRectangle(posx, posy, 10, 10, rl.Color{ .r = 10, .g = 10, .b = 120, .a = 255 });
} }
}; };
fn ItoF(x:i32) f32 fn ItoF(x: i32) f32 {
{
return @as(f32, @floatFromInt(x)); return @as(f32, @floatFromInt(x));
} }
fn rectangle(x : u16,y : u16,width : u16,height : u16) rl.Rectangle fn rectangle(x: u16, y: u16, width: u16, height: u16) rl.Rectangle {
{
return rl.Rectangle{ .x = @as(f32, @floatFromInt(x)), .y = @as(f32, @floatFromInt(y)), .width = @as(f32, @floatFromInt(width)), .height = @as(f32, @floatFromInt(height)) }; return rl.Rectangle{ .x = @as(f32, @floatFromInt(x)), .y = @as(f32, @floatFromInt(y)), .width = @as(f32, @floatFromInt(width)), .height = @as(f32, @floatFromInt(height)) };
} }
pub fn main() anyerror!void { pub fn main() anyerror!void {
const rect3 = rectangle(10, 150, 600, 10); const rect3 = rectangle(10, 150, 600, 10);
const rect4 = rectangle(10, 170, 600, 10); const rect4 = rectangle(10, 170, 600, 10);
const rect5 = rectangle(10, 190, 600, 10); const rect5 = rectangle(10, 190, 600, 10);
@ -202,28 +179,24 @@ pub fn main() anyerror!void {
var texture: rl.Texture2D = undefined; var texture: rl.Texture2D = undefined;
const image: rl.Image = rl.loadImage("resources/blut.png"); const image: rl.Image = rl.loadImage("resources/blut.png");
if(image.width > 0) if (image.width > 0) {
{
texture = rl.loadTextureFromImage(image); texture = rl.loadTextureFromImage(image);
} } else {
else
{
std.debug.print("error opening resource", .{}); std.debug.print("error opening resource", .{});
std.posix.exit(1); std.posix.exit(1);
} }
rl.unloadImage(image); rl.unloadImage(image);
rl.setTargetFPS(60); // Set our game to run at 60 frames-per-second rl.setTargetFPS(60); // Set our game to run at 60 frames-per-second
while (!rl.windowShouldClose()) { // Detect window close button or ESC key while (!rl.windowShouldClose()) { // Detect window close button or ESC key
rl.beginDrawing(); rl.beginDrawing();
defer rl.endDrawing(); defer rl.endDrawing();
rl.clearBackground(rl.Color{.r=181, rl.clearBackground(rl.Color{
.g =177, .r = 0,
.b =154, .g = 0,
.b = 0,
.a = 255, .a = 255,
}); });
@ -231,8 +204,7 @@ pub fn main() anyerror!void {
_ = rg.guiSlider(rect4, "-3", "3", &value2, ItoF(-10), ItoF(10)); _ = rg.guiSlider(rect4, "-3", "3", &value2, ItoF(-10), ItoF(10));
_ = rg.guiSlider(rect5, "-3", "3", &value3, -10, 10); _ = rg.guiSlider(rect5, "-3", "3", &value3, -10, 10);
if(rl.isMouseButtonDown(rl.MouseButton.mouse_button_left)) if (rl.isMouseButtonDown(rl.MouseButton.mouse_button_left)) {
{
emitter.emit(@as(u32, @intFromFloat(value)), value2, value3); emitter.emit(@as(u32, @intFromFloat(value)), value2, value3);
} }