producer supports output node push

efd/v1
Dominik Madarász 2022-10-17 18:28:40 +02:00
parent 3f6235e8a4
commit 6385ea4461
2 changed files with 106 additions and 40 deletions

View File

@ -1,3 +1,58 @@
static inline
asset_id FetchAssetAtPos(float x, float y) {
world_block_lookup lookup = world_block_from_realpos(x, y);
if (lookup.is_outer) {
return blocks_get_asset(lookup.bid);
}
return ASSET_INVALID;
}
static inline
bool CheckForNearbyBelt(Position *p, float *dx, float *dy) {
asset_id bid = ASSET_INVALID;
float o = WORLD_BLOCK_SIZE;
// up
bid = FetchAssetAtPos(p->x + 0, p->y - o);
{
debug_v2 a = {p->x, p->y};
debug_v2 b = {p->x, p->y-WORLD_BLOCK_SIZE};
debug_push_line(a, b, 0xFFFFFFFF);
}
if (bid == ASSET_BELT_UP) {
*dx = 0;
*dy = -o;
return true;
}
// down
bid = FetchAssetAtPos(p->x + 0, p->y + o);
if (bid == ASSET_BELT_DOWN) {
*dx = 0;
*dy = o;
return true;
}
// left
bid = FetchAssetAtPos(p->x - o, p->y + 0);
if (bid == ASSET_BELT_LEFT) {
*dx = -o;
*dy = 0;
return true;
}
// right
bid = FetchAssetAtPos(p->x + o, p->y + 0);
if (bid == ASSET_BELT_RIGHT) {
*dx = o;
*dy = 0;
return true;
}
return false;
}
void ProduceItems(ecs_iter_t *it) {
ItemContainer *storage = ecs_field(it, ItemContainer, 1);
Producer *producer = ecs_field(it, Producer, 2);
@ -5,6 +60,9 @@ void ProduceItems(ecs_iter_t *it) {
Device *d = ecs_field(it, Device, 4);
for (int i = 0; i < it->count; i++) {
float push_dx=0.0f, push_dy=0.0f;
bool has_output_node = CheckForNearbyBelt(&p[i], &push_dx, &push_dy);
for (int j = 0; j < ITEMS_CONTAINER_SIZE; j++) {
ecs_entity_t item_slot_ent = storage[i].items[j];
Item *item = item_get_data(item_slot_ent);
@ -23,8 +81,16 @@ void ProduceItems(ecs_iter_t *it) {
if (producer[i].process_time < game_time()) {
if (producer[i].processed_item > 0) {
uint64_t e = item_spawn(producer[i].processed_item, 1);
entity_set_position(e, p[i].x, p[i].y);
producer[i].processed_item = 0;
if (has_output_node) {
entity_set_position(e, p[i].x+push_dx, p[i].y+push_dy);
Velocity *e_vel = ecs_get_mut_ex(it->world, e, Velocity);
e_vel->x = push_dx;
e_vel->y = push_dy;
} else {
entity_set_position(e, p[i].x, p[i].y);
}
} else {
const Ingredient *ing = 0;
if ((ing = ecs_get_if(it->world, item_slot_ent, Ingredient))) {

View File

@ -25,8 +25,8 @@ void LeaveVehicle(ecs_iter_t *it) {
{
float px = zpl_cos(veh->heading)*400.0f;
float py = zpl_sin(veh->heading)*400.0f;
v->x += py;
v->y -= px;
v[i].x += py;
v[i].y -= px;
}
} else {
ZPL_PANIC("unreachable code");