This commit is contained in:
jonathan santis 2024-11-15 13:36:32 +01:00
parent c0a211e073
commit 8452d41247
2 changed files with 82 additions and 8 deletions

View File

@ -3,17 +3,64 @@ const rl = @import("raylib");
const rg = @import("raygui"); const rg = @import("raygui");
const pr = @import("primitives.zig"); const pr = @import("primitives.zig");
var prng = std.rand.DefaultPrng.init(124346556);
const rand = prng.random();
fn randMinMaxFloat(min:f32,max:f32) f32 {
var r1 = rand.float(f32);
r1 = 2*(r1 - 0.5);//-0.5..0.5
return (@abs(min)+@abs(max))/2 * r1;
}
test "random" {
var seed:u64 = undefined;
var r1 :f32 = undefined;
try std.posix.getrandom(std.mem.asBytes(&seed));
prng = std.rand.DefaultPrng.init(seed);
while(true) {
r1 = randMinMaxFloat(-10,10);
//std.debug.print("randMinMaxFloat:{}\n",.{r1});
if (r1 > 8 or r1 < -8 ) {
std.debug.print("Greater:{}\n",.{r1});
}
}
}
const Particle = struct { const Particle = struct {
const Self = @This(); const Self = @This();
position : pr.Vec = .{.a = .{.x=0,.y=0,.z=0}}, position : pr.Vec = .{.a = .{.x=0,.y=0,.z=0}},
velocity : pr.Vec = .{.a = .{.x=0,.y=0,.z=0}}, velocity : pr.Vec = .{.a = .{.x=0,.y=0,.z=0}},
acceleration : pr.Vec = .{.a = .{.x=0,.y=0,.z=0}}, acceleration : pr.Vec = .{.a = .{.x=0,.y=0,.z=0}},
lifespan : u8 = 0,
pub fn new(self:Self) void { pub fn new(self:Self) void {
_ = self; _ = self;
} }
pub fn update(self:Self)void{ pub fn update(self:*Self) void {
self.velocity = self.velocity + self.acceleration; self.velocity.add(self.acceleration);
self.position = self.position + self.velocity; self.position.add(self.velocity);
if (self.lifespan > 0) {
self.lifespan -= 1;
} else {
const xrnr = randMinMaxFloat(-1,1);
const yrnr = randMinMaxFloat(-1,1);
std.debug.print("rngx:{}\nyrng:{}\n",.{xrnr,yrnr});
self.lifespan = rand.intRangeAtMost(u8,1,100);
self.acceleration.a.x = xrnr;
self.acceleration.a.y = yrnr;
self.position.a.x = 0;
self.position.a.y = 0;
self.velocity.a.x = 0;
self.velocity.a.y = 0;
self.position.a.color.r = rand.intRangeAtMost(u8,0,255);
self.position.a.color.g = rand.intRangeAtMost(u8,0,255);
self.position.a.color.b = rand.intRangeAtMost(u8,0,255);
}
} }
}; };
@ -26,7 +73,22 @@ pub fn main() anyerror!void {
var msg_res : i32=-1; var msg_res : i32=-1;
var state : i32=-1; var state : i32=-1;
const p1 = Particle{.velocity = .{.a = .{.x = 1,.y = 1}}}; var seed:u64 = undefined;
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 p1 : [100]Particle = undefined;
for (&p1) |*p| {
xrnr = rand.float(f32);
yrnr = rand.float(f32);
p.* = Particle{.velocity = .{.a = .{.x = 5,.y = 1}},.acceleration = .{.a = .{.x=xrnr,.y=yrnr}}};
}
rl.initWindow(screenWidth, screenHeight, "raylib-zig [core] example - basic window"); rl.initWindow(screenWidth, screenHeight, "raylib-zig [core] example - basic window");
defer rl.closeWindow(); // Close window and OpenGL context defer rl.closeWindow(); // Close window and OpenGL context
@ -59,8 +121,20 @@ pub fn main() anyerror!void {
else =>{}, else =>{},
} }
} }
p1.update(); for (&p1) |*p| {
rl.drawCircle(p1.position.a.x,p1.position.a.y,40,rl.Color{.r=124,.g=195,.b=217,.a=100}); p.update();
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.drawCircle(posx,posy,5,rl.Color{.r=p.position.a.color.r,.g=p.position.a.color.g,.b=p.position.a.color.b,.a=255});
}
} }

Binary file not shown.