Components
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
const Graphics = @import("graphics.zig");
|
||||
const Entity = @import("entity.zig");
|
||||
const Time = @import("time.zig");
|
||||
const comp = @import("components.zig");
|
||||
|
||||
pub var time: Time = undefined;
|
||||
var next_stop: Time = undefined;
|
||||
var entities: [16]?Entity = undefined;
|
||||
var entities: comp.Storage(Entity, .{}) = undefined;
|
||||
|
||||
pub var plane_mesh: Graphics.Mesh = undefined;
|
||||
pub var cube_mesh: Graphics.Mesh = undefined;
|
||||
@@ -12,25 +13,25 @@ pub var texture: Graphics.Texture = undefined;
|
||||
|
||||
const World = @This();
|
||||
pub fn initDebug() void {
|
||||
entities = .{null} ** 16;
|
||||
entities[0] = .{
|
||||
entities = comp.Storage(Entity, .{}).init();
|
||||
_ = entities.add(.{
|
||||
.position = .{ 0, 0 },
|
||||
.player = true,
|
||||
};
|
||||
entities[1] = .{
|
||||
});
|
||||
_ = entities.add(.{
|
||||
.position = .{ 2, 0 },
|
||||
.enemy = true,
|
||||
.controller = .{
|
||||
.move_units = 0.25,
|
||||
},
|
||||
};
|
||||
entities[2] = .{
|
||||
});
|
||||
_ = entities.add(.{
|
||||
.position = .{ 3, 0 },
|
||||
.enemy = true,
|
||||
.controller = .{
|
||||
.move_units = 0.25,
|
||||
},
|
||||
};
|
||||
});
|
||||
time = Time.ZERO;
|
||||
World.plane_mesh = Graphics.loadMesh(@ptrCast(&PLANE_MESH_DATA));
|
||||
World.cube_mesh = Graphics.loadMesh(@ptrCast(&CUBE_MESH_DATA));
|
||||
@@ -41,6 +42,7 @@ pub fn deinit() void {
|
||||
Graphics.unloadMesh(World.plane_mesh);
|
||||
Graphics.unloadMesh(World.cube_mesh);
|
||||
Graphics.unloadTexture(World.texture);
|
||||
World.entities.deinit();
|
||||
}
|
||||
|
||||
pub fn updateReal(delta: f32) void {
|
||||
@@ -49,16 +51,18 @@ pub fn updateReal(delta: f32) void {
|
||||
const current = Time.earliest(World.next_stop, update_until);
|
||||
defer World.time = current;
|
||||
|
||||
for (&World.entities) |*entity| {
|
||||
if (entity.*) |*e| e.update();
|
||||
var iter = World.entities.iter();
|
||||
while (iter.next()) |entity| {
|
||||
entity.update();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn draw(delta: f32) void {
|
||||
Graphics.drawMesh(World.plane_mesh, World.texture, .{ .scale = @splat(5) });
|
||||
for (&World.entities) |*entity| {
|
||||
if (entity.*) |*e| e.draw(delta);
|
||||
var iter = World.entities.iter();
|
||||
while (iter.next()) |entity| {
|
||||
entity.draw(delta);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -67,11 +71,10 @@ pub fn requestUpdate(at: Time) void {
|
||||
}
|
||||
|
||||
pub fn entityAt(position: @Vector(2, i32)) ?*Entity {
|
||||
for (&World.entities) |*maybe_entity| {
|
||||
if (maybe_entity.*) |*entity| {
|
||||
if (@reduce(.And, entity.position == position))
|
||||
return entity;
|
||||
}
|
||||
var iter = World.entities.iter();
|
||||
while (iter.next()) |entity| {
|
||||
if (@reduce(.And, entity.position == position))
|
||||
return entity;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -81,11 +84,10 @@ pub fn isFree(position: @Vector(2, i32)) bool {
|
||||
}
|
||||
|
||||
pub fn getPlayer() ?*Entity {
|
||||
for (&World.entities) |*maybe_entity| {
|
||||
if (maybe_entity.*) |*entity| {
|
||||
if (entity.player)
|
||||
return entity;
|
||||
}
|
||||
var iter = World.entities.iter();
|
||||
while (iter.next()) |entity| {
|
||||
if (entity.player)
|
||||
return entity;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
Reference in New Issue
Block a user