optimize minion iteration
This commit is contained in:
parent
e4307fb543
commit
ae088c5a0e
28
src/app.zig
28
src/app.zig
@ -36,6 +36,9 @@ entities_buffer: [max_entities]EntitySlotMap.Slot,
|
||||
entities: EntitySlotMap,
|
||||
eternal_flame_id: EntityId,
|
||||
|
||||
minions_buffer: [max_entities]EntityId,
|
||||
minions: std.ArrayList(EntityId),
|
||||
|
||||
upgrades: Upgrades,
|
||||
shop: Shop,
|
||||
|
||||
@ -63,7 +66,7 @@ const terrain_width = 160;
|
||||
const terrain_height = 70;
|
||||
const terrain_tile_size = Vec2.init(32, 32);
|
||||
|
||||
const EntitySlotMap = SlotMapType(u16, u16, Entity);
|
||||
const EntitySlotMap = SlotMapType(std.math.IntFittingRange(0, max_entities-1), u16, Entity);
|
||||
const EntityId = EntitySlotMap.Id;
|
||||
|
||||
const Tilesheet = struct {
|
||||
@ -662,6 +665,9 @@ pub fn init(self: *App, plt: Platform.Init) !?u8 {
|
||||
.shop = undefined,
|
||||
.gradient_sprite = gradient_sprite,
|
||||
|
||||
.minions_buffer = undefined,
|
||||
.minions = .initBuffer(&self.minions_buffer),
|
||||
|
||||
.terrain_sheet = terrain_sheet,
|
||||
.terrain_tiles = terrain_tiles,
|
||||
};
|
||||
@ -892,7 +898,7 @@ fn spawnEntity(self: *App, e: Entity) ?EntityId {
|
||||
pub fn spawnMinion(self: *App, pos: Vec2) void {
|
||||
const character_size = Vec2.init(96, 96);
|
||||
|
||||
_ = self.spawnEntity(Entity{
|
||||
const minion_id = self.spawnEntity(Entity{
|
||||
.type = .minion,
|
||||
.flags = .{
|
||||
.has_kinematics = true
|
||||
@ -905,7 +911,9 @@ pub fn spawnMinion(self: *App, pos: Vec2) void {
|
||||
.pos = .init(-character_size.x/2, -character_size.y*0.7),
|
||||
.size = character_size,
|
||||
}
|
||||
});
|
||||
}) orelse return;
|
||||
|
||||
self.minions.appendAssumeCapacity(minion_id);
|
||||
}
|
||||
|
||||
pub fn spawnWoodStation(self: *App, pos: f32) void {
|
||||
@ -1330,6 +1338,8 @@ fn removeMinion(self: *App, minion_id: EntityId) void {
|
||||
wood.flags.has_kinematics = true;
|
||||
}
|
||||
|
||||
_ = self.minions.swapRemove(std.mem.indexOfScalar(EntityId, self.minions.items, minion_id).?);
|
||||
|
||||
self.entities.removeAssumeExists(minion_id);
|
||||
}
|
||||
|
||||
@ -1733,8 +1743,7 @@ fn showDebug(self: *App, plt: Platform.Frame) !void {
|
||||
if (ImGUI.beginTabItem("Wood")) {
|
||||
defer ImGUI.endTabItem();
|
||||
|
||||
var minion_iter = self.initEntityIteratorByType(.minion);
|
||||
while (minion_iter.next()) |minion_id| {
|
||||
for (self.minions.items) |minion_id| {
|
||||
const minion = self.entities.getAssumeExists(minion_id);
|
||||
ImGUI.text("{f}, len: {}, wood_head: {?f}", .{minion_id, minion.minion_carried_wood_len, minion.minion_carried_wood_head});
|
||||
}
|
||||
@ -1750,8 +1759,7 @@ fn showDebug(self: *App, plt: Platform.Frame) !void {
|
||||
if (ImGUI.beginTabItem("Minions")) {
|
||||
defer ImGUI.endTabItem();
|
||||
|
||||
var minion_iter = self.initEntityIteratorByType(.minion);
|
||||
while (minion_iter.next()) |minion_id| {
|
||||
for (self.minions.items) |minion_id| {
|
||||
const minion = self.entities.getAssumeExists(minion_id);
|
||||
ImGUI.text("{f}, animation: {}, acc: {}, vel: {}", .{minion_id, minion.minion_animation, minion.acc.length(), minion.vel.length()});
|
||||
}
|
||||
@ -1954,8 +1962,7 @@ pub fn frame(self: *App, plt: Platform.Frame) !void {
|
||||
}
|
||||
|
||||
{
|
||||
var minion_iter = self.initEntityIteratorByType(.minion);
|
||||
while (minion_iter.next()) |minion_id| {
|
||||
for (self.minions.items) |minion_id| {
|
||||
const minion = self.entities.getAssumeExists(minion_id);
|
||||
|
||||
if (minion.minion_state == .wander and minion.target_entity != null) {
|
||||
@ -2129,8 +2136,7 @@ pub fn frame(self: *App, plt: Platform.Frame) !void {
|
||||
}
|
||||
|
||||
{ // Minion logic & animations
|
||||
var minion_iter = self.initEntityIteratorByType(.minion);
|
||||
while (minion_iter.next()) |minion_id| {
|
||||
for (self.minions.items) |minion_id| {
|
||||
const minion = self.entities.getAssumeExists(minion_id);
|
||||
|
||||
try self.minionLogic(plt, minion_id);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user