152 lines
3.1 KiB
Zig
152 lines
3.1 KiB
Zig
const std = @import("std");
|
|
const rl = @import("raylib");
|
|
|
|
const Vec3 = rl.Vector3;
|
|
const Vec2 = rl.Vector2;
|
|
|
|
const Frug = struct {
|
|
pos: Vec3,
|
|
radius: f32 = 24,
|
|
|
|
charging: bool = false,
|
|
charge: f32 = 0,
|
|
|
|
frugBurp: rl.Sound,
|
|
|
|
pub fn init(frugBurp: rl.Sound) Frug {
|
|
return .{
|
|
.pos = .{ .x = 200, .y = 400, .z = 10 },
|
|
.frugBurp = frugBurp,
|
|
};
|
|
}
|
|
|
|
pub fn handleInput(self: *Frug, dt: f32) void {
|
|
if (rl.isKeyDown(.space)) {
|
|
self.charging = true;
|
|
self.charge += dt * 20.0;
|
|
}
|
|
|
|
if (rl.isKeyReleased(.space)) {
|
|
self.jump();
|
|
}
|
|
}
|
|
|
|
pub fn jump(self: *Frug) void {
|
|
rl.playSound(self.frugBurp);
|
|
|
|
self.charging = false;
|
|
self.charge = 0;
|
|
}
|
|
|
|
pub fn draw(self: Frug) void {
|
|
rl.drawCircleV(
|
|
Vec2.init(self.pos.x, self.pos.y),
|
|
self.radius,
|
|
.green,
|
|
);
|
|
}
|
|
};
|
|
|
|
const Pad = struct {
|
|
pos: Vec2,
|
|
radius: f32 = 12,
|
|
|
|
pub fn draw(self: Pad) void {
|
|
rl.drawCircleV(
|
|
Vec2.init(self.pos.x, self.pos.y),
|
|
self.radius,
|
|
.red,
|
|
);
|
|
}
|
|
|
|
pub fn init() Pad {
|
|
return Pad{
|
|
.pos = Vec2.init(1, 1),
|
|
};
|
|
}
|
|
};
|
|
|
|
const Game = struct {
|
|
frug: Frug,
|
|
pads: [10]Pad,
|
|
charging: bool,
|
|
charge: f32,
|
|
|
|
pub fn init(frugBurp: rl.Sound) Game {
|
|
// just temporary non random pads
|
|
var pads: [10]Pad = undefined;
|
|
|
|
for (&pads, 0..) |*pad, i| {
|
|
pad.* = Pad.init();
|
|
|
|
pad.pos.x = 30 + 30 * @as(f32, @floatFromInt(i));
|
|
pad.pos.y = 20 + 20 * @as(f32, @floatFromInt(i));
|
|
}
|
|
|
|
return Game{
|
|
.frug = Frug.init(frugBurp),
|
|
.pads = pads,
|
|
.charging = false,
|
|
.charge = 0,
|
|
};
|
|
}
|
|
|
|
pub fn update(self: *Game, dt: f32) void {
|
|
self.frug.handleInput(dt);
|
|
}
|
|
|
|
pub fn draw(self: *Game) void {
|
|
rl.clearBackground(.sky_blue);
|
|
|
|
for (self.pads) |pad| {
|
|
pad.draw();
|
|
}
|
|
|
|
self.frug.draw();
|
|
rl.drawText("FRUGG!", 10, 10, 40, .green);
|
|
}
|
|
|
|
pub fn handleInput(self: *Game, dt: f32) void {
|
|
if (rl.isKeyDown(.space)) {
|
|
self.charging = true;
|
|
self.charge += dt * 20.0;
|
|
}
|
|
|
|
if (rl.isKeyReleased(.space)) {
|
|
self.frug.jump();
|
|
}
|
|
}
|
|
};
|
|
|
|
pub fn main(init: std.process.Init) anyerror!void {
|
|
const io = init.io;
|
|
try std.Io.File.stdout().writeStreamingAll(io, "FRUGG!\n");
|
|
|
|
const screenWidth = 800;
|
|
const screenHeight = 600;
|
|
|
|
rl.initWindow(screenWidth, screenHeight, "Zig Frugger");
|
|
defer rl.closeWindow();
|
|
|
|
rl.initAudioDevice();
|
|
defer rl.closeAudioDevice();
|
|
const frugBurp = try rl.loadSound("resources/burp.mp3");
|
|
defer rl.unloadSound(frugBurp);
|
|
|
|
rl.setTargetFPS(60);
|
|
|
|
var game = Game.init(frugBurp);
|
|
|
|
while (!rl.windowShouldClose()) {
|
|
const dt = rl.getFrameTime();
|
|
|
|
game.handleInput(dt);
|
|
game.update(dt);
|
|
|
|
rl.beginDrawing();
|
|
defer rl.endDrawing();
|
|
|
|
game.draw();
|
|
}
|
|
}
|