commit 6e4566f2f0fbab792afa3c8459fc8c444c451941
parent afb767d3a8c9bbdfe1d53cdda9d41591d444fd34
Author: bsandro <email@bsandro.tech>
Date: Thu, 10 Sep 2026 00:56:31 +0300
basic movement
Diffstat:
5 files changed, 41 insertions(+), 11 deletions(-)
diff --git a/actions/attack_melee.c b/actions/attack_melee.c
@@ -6,11 +6,11 @@
#include "core/ecs/entity.h"
#include "core/world.h"
-bool Attack_Melee(Entity *e, World *w) {
+bool Action_Attack_Melee(Entity *e, World *w) {
+ printf("unit %d try melee\n", e->id);
int strength = Entity_Find_Component(e, COMPONENT_MELEE)->melee.strength; //@todo NULL check
int hp = Entity_Find_Component(e, COMPONENT_HEALTH)->health.hp; //@todo NULL check
Position p = Entity_Find_Component(e, COMPONENT_POSITION)->position; //@todo NULL check
- printf("[%d with %d hp] action try melee attack with strength %d\n", e->id, hp, strength);
for (int i=0;i<arrlen(w->entities);++i) {
if (w->entities[i].id==e->id) continue;
Position pe = Entity_Find_Component(&w->entities[i], COMPONENT_POSITION)->position;
diff --git a/actions/attack_melee.h b/actions/attack_melee.h
@@ -3,4 +3,4 @@
struct Entity; // fwd
struct World; // fwd
-bool Attack_Melee(struct Entity *e, struct World *w);
+bool Action_Attack_Melee(struct Entity *e, struct World *w);
diff --git a/actions/movement.c b/actions/movement.c
@@ -0,0 +1,26 @@
+
+#include <stdlib.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include "stb_ds.h"
+#include "actions/movement.h"
+#include "core/ecs/entity.h"
+#include "core/world.h"
+
+bool Action_Movement(Entity *e, World *w) {
+ printf("unit %d try movement\n", e->id);
+ MovementTarget mt = Entity_Find_Component(e, COMPONENT_MOVEMENT_TARGET)->movement_target;
+ Component *p = Entity_Find_Component(e, COMPONENT_POSITION);
+ // melee action already should've taken care of any neighbor enemies so we don't check for that
+ if (mt.x!=p->position.x||mt.y!=p->position.y) {
+ Position old = p->position;
+ // @todo dumb
+ if (mt.x>p->position.x) p->position.x++;
+ else if (mt.x<p->position.x) p->position.x--;
+ if (mt.y>p->position.y) p->position.y++;
+ else if (mt.y<p->position.y) p->position.y--;
+ printf("unit %d moved from %d:%d to %d:%d (target is %d:%d)\n", e->id, old.x, old.y, p->position.x, p->position.y, mt.x, mt.y);
+ return true;
+ }
+ return false;
+}
diff --git a/actions/movement.h b/actions/movement.h
@@ -0,0 +1,6 @@
+#pragma once
+
+struct Entity; // fwd
+struct World; // fwd
+
+bool Action_Movement(struct Entity *e, struct World *w);
diff --git a/commands/SPAWN_SWORDSMAN.c b/commands/SPAWN_SWORDSMAN.c
@@ -2,6 +2,7 @@
#include "core/command.h"
#include "core/ecs/entity.h"
#include "actions/attack_melee.h"
+#include "actions/movement.h"
void spawn_swordsman2(World *w) {
//@todo parse va_args
@@ -20,12 +21,8 @@ void spawn_swordsman2(World *w) {
.type = COMPONENT_POSITION,
.position = { .x=1, .y=1 }
});
- Entity_Add_Component(&swordsman, (Component){
- .type = COMPONENT_MOVEMENT_TARGET,
- .movement_target = { .x=5, .y=5 }
- });
Actions actions = {0};
- arrput(actions.items, (struct Actions_Item){ .run = Attack_Melee });
+ arrput(actions.items, (struct Actions_Item){ .run = Action_Attack_Melee });
Entity_Add_Component(&swordsman, (Component){
.type = COMPONENT_ACTIONS,
.actions = actions
@@ -50,14 +47,15 @@ void cmd_spawn_swordsman(World *w, ...) {
});
Entity_Add_Component(&swordsman, (Component){
.type = COMPONENT_POSITION,
- .position = { .x=2, .y=2 }
+ .position = { .x=8, .y=5 }
});
Entity_Add_Component(&swordsman, (Component){
.type = COMPONENT_MOVEMENT_TARGET,
- .movement_target = { .x=5, .y=5 }
+ .movement_target = { .x=2, .y=1 }
});
Actions actions = {0};
- arrput(actions.items, (struct Actions_Item){ .run = Attack_Melee });
+ arrput(actions.items, (struct Actions_Item){ .run = Action_Attack_Melee });
+ arrput(actions.items, (struct Actions_Item){ .run = Action_Movement });
Entity_Add_Component(&swordsman, (Component){
.type = COMPONENT_ACTIONS,
.actions = actions