move minionlogic to separate funciton
This commit is contained in:
parent
7e3c8b4a75
commit
0cf55b3ef1
260
src/app.zig
260
src/app.zig
@ -160,6 +160,7 @@ const Entity = struct {
|
|||||||
minion_chop_cooldown: Nanoseconds = 0,
|
minion_chop_cooldown: Nanoseconds = 0,
|
||||||
minion_chop_damage: u32 = 2,
|
minion_chop_damage: u32 = 2,
|
||||||
minion_burn_speed: f32 = 1,
|
minion_burn_speed: f32 = 1,
|
||||||
|
minion_carried_wood: ?EntityId = null,
|
||||||
|
|
||||||
tree_health: u32 = 0,
|
tree_health: u32 = 0,
|
||||||
tree_max_health: u32 = 0,
|
tree_max_health: u32 = 0,
|
||||||
@ -168,6 +169,8 @@ const Entity = struct {
|
|||||||
station_spawn_tree_cooldown: Nanoseconds = 0,
|
station_spawn_tree_cooldown: Nanoseconds = 0,
|
||||||
station_owned_tree: ?EntityId = null,
|
station_owned_tree: ?EntityId = null,
|
||||||
|
|
||||||
|
wood_carried_by: ?EntityId = null,
|
||||||
|
|
||||||
pos: Vec2 = .init(0, 0),
|
pos: Vec2 = .init(0, 0),
|
||||||
vel: Vec2 = .init(0, 0),
|
vel: Vec2 = .init(0, 0),
|
||||||
acc: Vec2 = .init(0, 0),
|
acc: Vec2 = .init(0, 0),
|
||||||
@ -264,19 +267,19 @@ pub fn init(self: *App, plt: Platform.Init) !?u8 {
|
|||||||
.font = font
|
.font = font
|
||||||
};
|
};
|
||||||
|
|
||||||
try self.spawnMinion(.init(0, 0));
|
self.spawnMinion(.init(0, 0));
|
||||||
// try self.spawnMinion(.init(32, 0));
|
// try self.spawnMinion(.init(32, 0));
|
||||||
|
|
||||||
try self.spawnStation(-100, .chop);
|
self.spawnStation(-100, .chop);
|
||||||
try self.spawnStation(-150, .chop);
|
self.spawnStation(-150, .chop);
|
||||||
try self.spawnStation(100, .burn);
|
self.spawnStation(100, .burn);
|
||||||
try self.spawnStation(150, .burn);
|
self.spawnStation(150, .burn);
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn spawnMinion(self: *App, pos: Vec2) !void {
|
pub fn spawnMinion(self: *App, pos: Vec2) void {
|
||||||
_ = try self.entities.insert(Entity{
|
_ = self.entities.insert(Entity{
|
||||||
.type = .minion,
|
.type = .minion,
|
||||||
.flags = .{
|
.flags = .{
|
||||||
.has_animation = true,
|
.has_animation = true,
|
||||||
@ -289,19 +292,25 @@ pub fn spawnMinion(self: *App, pos: Vec2) !void {
|
|||||||
.max_walk_speed = 300,
|
.max_walk_speed = 300,
|
||||||
|
|
||||||
.animation = .idle,
|
.animation = .idle,
|
||||||
});
|
}) catch {
|
||||||
|
log.warn("Failed to spawn minion, entity limit reached", .{});
|
||||||
|
return;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn spawnStation(self: *App, pos: f32, subtype: Entity.StationSubtype) !void {
|
pub fn spawnStation(self: *App, pos: f32, subtype: Entity.StationSubtype) void {
|
||||||
_ = try self.entities.insert(Entity{
|
_ = self.entities.insert(Entity{
|
||||||
.type = .station,
|
.type = .station,
|
||||||
.station_subtype = subtype,
|
.station_subtype = subtype,
|
||||||
|
|
||||||
.pos = .init(pos, 0),
|
.pos = .init(pos, 0),
|
||||||
});
|
}) catch {
|
||||||
|
log.warn("Failed to spawn station, entity limit reached", .{});
|
||||||
|
return;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn spawnTree(self: *App, station_id: EntityId) !void {
|
fn spawnTree(self: *App, station_id: EntityId) void {
|
||||||
const station = self.entities.get(station_id) orelse return;
|
const station = self.entities.get(station_id) orelse return;
|
||||||
assert(station.type == .station);
|
assert(station.type == .station);
|
||||||
if (station.station_owned_tree != null) {
|
if (station.station_owned_tree != null) {
|
||||||
@ -309,16 +318,38 @@ fn spawnTree(self: *App, station_id: EntityId) !void {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const health = 10;
|
const health = 10;
|
||||||
const id = try self.entities.insert(Entity{
|
const id = self.entities.insert(Entity{
|
||||||
.type = .tree,
|
.type = .tree,
|
||||||
.tree_health = health,
|
.tree_health = health,
|
||||||
.tree_max_health = health,
|
.tree_max_health = health,
|
||||||
|
|
||||||
.pos = station.pos
|
.pos = station.pos
|
||||||
});
|
}) catch {
|
||||||
|
log.warn("Failed to spawn tree, entity limit reached", .{});
|
||||||
|
return;
|
||||||
|
};
|
||||||
station.station_owned_tree = id;
|
station.station_owned_tree = id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn destroyTree(self: *App, tree_id: EntityId) void {
|
||||||
|
const tree = self.entities.get(tree_id) orelse return;
|
||||||
|
assert(tree.type == .tree);
|
||||||
|
|
||||||
|
self.spawnWood(tree.pos);
|
||||||
|
|
||||||
|
self.entities.removeAssumeExists(tree_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn spawnWood(self: *App, pos: Vec2) void {
|
||||||
|
_ = self.entities.insert(Entity{
|
||||||
|
.type = .wood,
|
||||||
|
.pos = pos
|
||||||
|
}) catch {
|
||||||
|
log.warn("Failed to spawn wood, entity limit reached", .{});
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
fn getNearestFreeStation(self: *App, from: Vec2, subtype: Entity.StationSubtype) ?EntityId {
|
fn getNearestFreeStation(self: *App, from: Vec2, subtype: Entity.StationSubtype) ?EntityId {
|
||||||
var result: ?EntityId = null;
|
var result: ?EntityId = null;
|
||||||
var result_distance: f32 = 0;
|
var result_distance: f32 = 0;
|
||||||
@ -347,14 +378,14 @@ fn getNearestFreeStation(self: *App, from: Vec2, subtype: Entity.StationSubtype)
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn getNearestFreeTree(self: *App, from: Vec2) ?EntityId {
|
fn getNearestFreeEntity(self: *App, from: Vec2, entity_type: Entity.Type) ?EntityId {
|
||||||
var result: ?EntityId = null;
|
var result: ?EntityId = null;
|
||||||
var result_distance: f32 = 0;
|
var result_distance: f32 = 0;
|
||||||
|
|
||||||
var station_iter = self.entities.iterator();
|
var station_iter = self.entities.iterator();
|
||||||
while (station_iter.next()) |id| {
|
while (station_iter.next()) |id| {
|
||||||
const station = self.entities.getAssumeExists(id);
|
const station = self.entities.getAssumeExists(id);
|
||||||
if (station.type != .tree) {
|
if (station.type != entity_type) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (station.targeted_by_entity != null) {
|
if (station.targeted_by_entity != null) {
|
||||||
@ -400,95 +431,26 @@ fn drawProgressBar(progress: f32, center: Vec2, size: Vec2, color: Color) void {
|
|||||||
Gfx.drawLine(from, from.add(.init(size.x * progress, 0)), color, size.y);
|
Gfx.drawLine(from, from.add(.init(size.x * progress, 0)), color, size.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn frame(self: *App, plt: Platform.Frame) !void {
|
fn minionLogic(self: *App, plt: Platform.Frame, minion_id: EntityId) !void {
|
||||||
const input = plt.input;
|
|
||||||
_ = input; // autofix
|
|
||||||
|
|
||||||
const dt = plt.deltaTime();
|
|
||||||
|
|
||||||
if (plt.input.isKeyDown(.D)) {
|
|
||||||
self.camera_pos.x += 50 * dt;
|
|
||||||
}
|
|
||||||
if (plt.input.isKeyDown(.A)) {
|
|
||||||
self.camera_pos.x -= 50 * dt;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (plt.input.isMouseDown(.left)) {
|
|
||||||
self.camera_pos = self.camera_pos.sub(plt.input.mouse_delta);
|
|
||||||
}
|
|
||||||
|
|
||||||
Gfx.viewTranslate(plt.input.window_size.x/2, plt.input.window_size.y/2);
|
|
||||||
Gfx.viewTranslate(-self.camera_pos.x, -self.camera_pos.y);
|
|
||||||
|
|
||||||
Gfx.setClearColor(.rgb(40, 40, 40));
|
|
||||||
Gfx.drawLine(.init(-800, 0), .init(800, 0), .white, 5);
|
|
||||||
|
|
||||||
var station_iter = self.entities.iterator();
|
|
||||||
while (station_iter.next()) |station_id| {
|
|
||||||
const station = self.entities.getAssumeExists(station_id);
|
|
||||||
if (station.type != .station) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (station.station_owned_tree) |station_owned_tree| {
|
|
||||||
if (!self.entities.exists(station_owned_tree)) {
|
|
||||||
station.station_owned_tree = null;
|
|
||||||
station.station_spawn_tree_cooldown = std.time.ns_per_s;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (station.station_subtype == .chop) {
|
|
||||||
station.station_spawn_tree_cooldown = @max(station.station_spawn_tree_cooldown - plt.dt, 0);
|
|
||||||
if (station.station_spawn_tree_cooldown == 0 and station.station_owned_tree == null) {
|
|
||||||
try self.spawnTree(station_id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const character_size = Vec2.init(96, 96);
|
|
||||||
|
|
||||||
{
|
|
||||||
var entity_iter = self.entities.iterator();
|
|
||||||
while (entity_iter.next()) |entity_id| {
|
|
||||||
const e = self.entities.getAssumeExists(entity_id);
|
|
||||||
|
|
||||||
if (e.target_entity) |other_id| {
|
|
||||||
if (self.entities.get(other_id)) |other| {
|
|
||||||
if (other.targeted_by_entity != entity_id) {
|
|
||||||
e.target_entity = null;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
e.target_entity = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (e.targeted_by_entity) |other_id| {
|
|
||||||
if (self.entities.get(other_id)) |other| {
|
|
||||||
if (other.target_entity != entity_id) {
|
|
||||||
e.targeted_by_entity = null;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
e.targeted_by_entity = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var minion_iter = self.entities.iterator();
|
|
||||||
while (minion_iter.next()) |minion_id| {
|
|
||||||
const minion = self.entities.getAssumeExists(minion_id);
|
const minion = self.entities.getAssumeExists(minion_id);
|
||||||
if (minion.type != .minion) {
|
assert(minion.type == .minion);
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
const goal_distance_threshold = 10;
|
const goal_distance_threshold = 10;
|
||||||
var walk_goal: ?Vec2 = null;
|
var walk_goal: ?Vec2 = null;
|
||||||
var station_goal: ?Entity.StationSubtype = null;
|
var station_goal: ?Entity.StationSubtype = null;
|
||||||
|
|
||||||
|
if (minion.minion_state == .goto_tree_station) {
|
||||||
|
// if (minion.minion_carried_wood == null) {
|
||||||
|
// if (self.getNearestFreeEntity(minion.pos, .tree)) |tree_id| {
|
||||||
|
// _ = tree_id; // autofix
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
switch (minion.minion_state) {
|
switch (minion.minion_state) {
|
||||||
.goto_tree_station => {
|
.goto_tree_station => {
|
||||||
// TODO: Steal tree is it is nearer to you compared to the current minion that has it reserved.
|
// TODO: Steal tree if it is nearer to you compared to the current minion that has it reserved.
|
||||||
if (self.getNearestFreeTree(minion.pos)) |tree_id| {
|
if (self.getNearestFreeEntity(minion.pos, .tree)) |tree_id| {
|
||||||
self.setEntityTarget(minion_id, tree_id);
|
self.setEntityTarget(minion_id, tree_id);
|
||||||
minion.minion_state = .chop_tree;
|
minion.minion_state = .chop_tree;
|
||||||
} else {
|
} else {
|
||||||
@ -526,7 +488,7 @@ pub fn frame(self: *App, plt: Platform.Frame) !void {
|
|||||||
minion.minion_state = .burn;
|
minion.minion_state = .burn;
|
||||||
|
|
||||||
self.unsetEntityTarget(minion_id);
|
self.unsetEntityTarget(minion_id);
|
||||||
self.entities.removeAssumeExists(tree_id);
|
self.destroyTree(tree_id);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
walk_goal = tree.pos;
|
walk_goal = tree.pos;
|
||||||
@ -555,7 +517,7 @@ pub fn frame(self: *App, plt: Platform.Frame) !void {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (is_at_station) {
|
if (is_at_station) {
|
||||||
minion.minion_work_progress += minion.minion_burn_speed * dt;
|
minion.minion_work_progress += minion.minion_burn_speed * plt.deltaTime();
|
||||||
}
|
}
|
||||||
|
|
||||||
minion.minion_work_progress = std.math.clamp(minion.minion_work_progress, 0, 1);
|
minion.minion_work_progress = std.math.clamp(minion.minion_work_progress, 0, 1);
|
||||||
@ -602,6 +564,90 @@ pub fn frame(self: *App, plt: Platform.Frame) !void {
|
|||||||
minion.acc = .init(-minion.max_walk_speed, 0);
|
minion.acc = .init(-minion.max_walk_speed, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn frame(self: *App, plt: Platform.Frame) !void {
|
||||||
|
const input = plt.input;
|
||||||
|
_ = input; // autofix
|
||||||
|
|
||||||
|
const dt = plt.deltaTime();
|
||||||
|
|
||||||
|
if (plt.input.isKeyDown(.D)) {
|
||||||
|
self.camera_pos.x += 50 * dt;
|
||||||
|
}
|
||||||
|
if (plt.input.isKeyDown(.A)) {
|
||||||
|
self.camera_pos.x -= 50 * dt;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (plt.input.isMouseDown(.left)) {
|
||||||
|
self.camera_pos = self.camera_pos.sub(plt.input.mouse_delta);
|
||||||
|
}
|
||||||
|
|
||||||
|
Gfx.viewTranslate(plt.input.window_size.x/2, plt.input.window_size.y/2);
|
||||||
|
Gfx.viewTranslate(-self.camera_pos.x, -self.camera_pos.y);
|
||||||
|
|
||||||
|
Gfx.setClearColor(.rgb(40, 40, 40));
|
||||||
|
Gfx.drawLine(.init(-800, 0), .init(800, 0), .white, 5);
|
||||||
|
|
||||||
|
var station_iter = self.entities.iterator();
|
||||||
|
while (station_iter.next()) |station_id| {
|
||||||
|
const station = self.entities.getAssumeExists(station_id);
|
||||||
|
if (station.type != .station) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (station.station_owned_tree) |station_owned_tree| {
|
||||||
|
if (!self.entities.exists(station_owned_tree)) {
|
||||||
|
station.station_owned_tree = null;
|
||||||
|
station.station_spawn_tree_cooldown = std.time.ns_per_s;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (station.station_subtype == .chop) {
|
||||||
|
station.station_spawn_tree_cooldown = @max(station.station_spawn_tree_cooldown - plt.dt, 0);
|
||||||
|
if (station.station_spawn_tree_cooldown == 0 and station.station_owned_tree == null) {
|
||||||
|
self.spawnTree(station_id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const character_size = Vec2.init(96, 96);
|
||||||
|
|
||||||
|
{
|
||||||
|
var entity_iter = self.entities.iterator();
|
||||||
|
while (entity_iter.next()) |entity_id| {
|
||||||
|
const e = self.entities.getAssumeExists(entity_id);
|
||||||
|
|
||||||
|
if (e.target_entity) |other_id| {
|
||||||
|
if (self.entities.get(other_id)) |other| {
|
||||||
|
if (other.targeted_by_entity != entity_id) {
|
||||||
|
e.target_entity = null;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
e.target_entity = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (e.targeted_by_entity) |other_id| {
|
||||||
|
if (self.entities.get(other_id)) |other| {
|
||||||
|
if (other.target_entity != entity_id) {
|
||||||
|
e.targeted_by_entity = null;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
e.targeted_by_entity = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var minion_iter = self.entities.iterator();
|
||||||
|
while (minion_iter.next()) |minion_id| {
|
||||||
|
const minion = self.entities.getAssumeExists(minion_id);
|
||||||
|
if (minion.type != .minion) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
try self.minionLogic(plt, minion_id);
|
||||||
|
|
||||||
if (minion.acc.x > 0) {
|
if (minion.acc.x > 0) {
|
||||||
minion.animation = .walk;
|
minion.animation = .walk;
|
||||||
@ -630,6 +676,7 @@ pub fn frame(self: *App, plt: Platform.Frame) !void {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{
|
||||||
var entity_iter = self.entities.iterator();
|
var entity_iter = self.entities.iterator();
|
||||||
while (entity_iter.next()) |id| {
|
while (entity_iter.next()) |id| {
|
||||||
const e = self.entities.getAssumeExists(id);
|
const e = self.entities.getAssumeExists(id);
|
||||||
@ -708,6 +755,19 @@ pub fn frame(self: *App, plt: Platform.Frame) !void {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
var wood_iter = self.entities.iterator();
|
||||||
|
while (wood_iter.next()) |id| {
|
||||||
|
const wood = self.entities.getAssumeExists(id);
|
||||||
|
if (wood.type != .wood) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
Gfx.drawRectangle(wood.pos.sub(.init(10, 10)), .init(20, 20), .rgb(100, 100, 20));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Gfx.drawText(self.font, .{
|
Gfx.drawText(self.font, .{
|
||||||
.height = 32,
|
.height = 32,
|
||||||
@ -719,7 +779,7 @@ pub fn frame(self: *App, plt: Platform.Frame) !void {
|
|||||||
defer ImGUI.endWindow();
|
defer ImGUI.endWindow();
|
||||||
|
|
||||||
if (ImGUI.button("spawn")) {
|
if (ImGUI.button("spawn")) {
|
||||||
try self.spawnMinion(.init(32, 0));
|
self.spawnMinion(.init(32, 0));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user