diff --git a/src/main.zig b/src/main.zig index e99266f..b89f5b5 100644 --- a/src/main.zig +++ b/src/main.zig @@ -38,7 +38,7 @@ const Particle = struct { velocity : pr.Vec = .{.a = .{.x=0,.y=0,.z=0}}, acceleration : pr.Vec = .{.a = .{.x=0,.y=0,.z=0}}, lifespan : u8 = 0, - spawm : u8 = 1, + show : u8 = 0, pub fn new(self:Self) void { _ = self; } @@ -53,27 +53,93 @@ const Particle = struct { if (self.lifespan > 0) { self.lifespan -= 1; - } else + } + else { - const xrnr = randMinMaxFloat(-1,1); - const yrnr = randMinMaxFloat(-0.5,0.5); - - self.lifespan = rand.intRangeAtMost(u8,75,255); - self.acceleration.a.x = xrnr; - self.acceleration.a.y = yrnr; - self.position.a.x = @floatFromInt(rl.getMouseX()-screenWidth/2); - self.position.a.y = @floatFromInt(rl.getMouseY()-screenHeight/2); - self.velocity.a.x = 0; - self.velocity.a.y = -20; - self.position.a.color.r = rand.intRangeAtMost(u8,0,30); - self.position.a.color.g = rand.intRangeAtMost(u8,0,255); - self.position.a.color.b = rand.intRangeAtMost(u8,0,255); + self.show = 0; } } + pub fn spawn(self:*Self) void + { + const xrnr = randMinMaxFloat(-1,1); + const yrnr = randMinMaxFloat(-0.5,0.5); + + self.lifespan = rand.intRangeAtMost(u8,75,255); + self.acceleration.a.x = xrnr; + self.acceleration.a.y = yrnr; + self.position.a.x = @floatFromInt(rl.getMouseX()-screenWidth/2); + self.position.a.y = @floatFromInt(rl.getMouseY()-screenHeight/2); + self.velocity.a.x = 0; + self.velocity.a.y = -20; + self.position.a.color.r = rand.intRangeAtMost(u8,0,30); + self.position.a.color.g = rand.intRangeAtMost(u8,0,255); + self.position.a.color.b = rand.intRangeAtMost(u8,0,255); + self.show = 1; + } pub fn applyGravity(self : *Self,val : f32) void { self.velocity.a.y += val; } }; + +const Emitter = struct +{ + + particleCount : u32 = 0, + particles : []Particle = undefined, + + pub fn init(self:*Emitter) !void + { + var xrnr = randMinMaxFloat(-1,1); + var yrnr = randMinMaxFloat(-0.5,0.5); + self.particles = try allocator.alloc(Particle,500000); + for (self.particles) |*p| { + xrnr = randMinMaxFloat(0,2); + yrnr = randMinMaxFloat(0,2); + p.* = Particle{.velocity = .{.a = .{.x = 1,.y = -3}},.acceleration = .{.a = .{.x=xrnr,.y=yrnr}}}; + } + } + pub fn emit(self:*Emitter,count : u32) void + { + var i : u32 = 0; + for(self.particles) |*p| + { + if(p.show == 0) + { + i+=1; + p.spawn(); + } + if(i >= count) + { + break; + } + } + } + pub fn update(self:*Emitter) void + { + var posx : i32 = 0; + var posy : i32 = 0; + + + for (self.particles) |*p| { + if(p.show == 1) + { + p.update(); + p.applyGravity(2); + if(p.position.a.x >= -(screenWidth/2)){ + posx = @intFromFloat(screenWidth / 2 + p.position.a.x); + } else { + posx = 0; + } + if(p.position.a.y >= -(screenHeight/2)){ + posy = @intFromFloat(screenHeight / 2 + p.position.a.y); + } else { + posy = 0; + } + rl.drawRectangle(posx,posy,2,2,rl.Color{.r=p.position.a.color.r,.g=p.position.a.color.g,.b=p.position.a.color.b,.a=255}); + } + } + } +}; fn ItoF(x:i32) f32 { return @as(f32,@floatFromInt(x)); @@ -84,7 +150,7 @@ pub fn main() anyerror!void { //-------------------------------------------------------------------------------------- const rect2 = rl.Rectangle{ .x = @as(f32, @floatFromInt(10)), .y = @as(f32, @floatFromInt(10)), .width = @as(f32, @floatFromInt(300)), .height = @as(f32, @floatFromInt(100)) }; - const rect3 = rl.Rectangle{ .x = @as(f32, @floatFromInt(200)), .y = @as(f32, @floatFromInt(150)), .width = @as(f32, @floatFromInt(600)), .height = @as(f32, @floatFromInt(10)) }; + const rect3 = rl.Rectangle{ .x = @as(f32, @floatFromInt(10)), .y = @as(f32, @floatFromInt(150)), .width = @as(f32, @floatFromInt(600)), .height = @as(f32, @floatFromInt(10)) }; var msg_res : i32=-1; var state : i32=-1; @@ -92,19 +158,11 @@ pub fn main() anyerror!void { try std.posix.getrandom(std.mem.asBytes(&seed)); prng = std.rand.DefaultPrng.init(seed); - var xrnr : f32 = 0; - var yrnr : f32 = 0; - var posx : i32 = 0; - var posy : i32 = 0; var value : f32 = 0; - const p1 = try allocator.alloc(Particle,500000); - for (p1) |*p| { - xrnr = rand.float(f32); - yrnr = rand.float(f32); - p.* = Particle{.velocity = .{.a = .{.x = 1,.y = -3}},.acceleration = .{.a = .{.x=xrnr,.y=yrnr}}}; - } + var emitter = Emitter{}; + try emitter.init(); //const img : rl.Texture = rl.loadTexture("img.png"); @@ -139,30 +197,14 @@ pub fn main() anyerror!void { else =>{}, } } - _ = rg.guiSlider(rect3,"0","500000",&value,ItoF(0),ItoF(500000)); - for (0..,p1) |i,*p| { - if(i<@as(usize,@intFromFloat(value))) - { - if(rl.isMouseButtonPressed(rl.MouseButton.mouse_button_left)) - { - p.spawm = 1; - } - p.update(); - p.applyGravity(2); - if(p.position.a.x >= -(screenWidth/2)){ - posx = @intFromFloat(screenWidth / 2 + p.position.a.x); - } else { - posx = 0; - } - if(p.position.a.y >= -(screenHeight/2)){ - posy = @intFromFloat(screenHeight / 2 + p.position.a.y); - } else { - posy = 0; - } - rl.drawRectangle(posx,posy,2,2,rl.Color{.r=p.position.a.color.r,.g=p.position.a.color.g,.b=p.position.a.color.b,.a=255}); - } + _ = rg.guiSlider(rect3,"0","5000",&value,ItoF(0),ItoF(5000)); + + if(rl.isMouseButtonDown(rl.MouseButton.mouse_button_left)) + { + emitter.emit(@as(u32,@intFromFloat(value))); } + emitter.update(); } } diff --git a/zig-out/bin/raylib-test b/zig-out/bin/raylib-test index aa1173b..7d8775d 100755 Binary files a/zig-out/bin/raylib-test and b/zig-out/bin/raylib-test differ