Files
PixelDefense/game/systems/systems.c

126 lines
4.0 KiB
C

#include "systems.h"
#include "../game_state.h"
bool entityGetHitBox(ecs_entity_t entity, Position *outPos, Rectangle *outHitBox) {
if (!ecs_is_alive(ECS, entity))
return false;
const Position *pos = ecs_get(ECS, entity, Position);
if (!pos)
return false;
if (outPos)
*outPos = *pos;
const HitBox *hitbox = ecs_get(ECS, entity, HitBox);
if (!hitbox)
return false;
if (outHitBox) {
*outHitBox = *hitbox;
outHitBox->x += pos->x;
outHitBox->y = pos->y - hitbox->y - hitbox->height;
}
return true;
}
Rectangle entityTransformHitBox(Position position, HitBox hitBox) {
return (Rectangle) {
position.x + hitBox.x,
position.y - hitBox.y - hitBox.height,
hitBox.width,
hitBox.height
};
}
Vector2 entityGetCenter(Position position, HitBox hitBox) {
return (Vector2) {
position.x + hitBox.x + hitBox.width * 0.5f,
position.y - hitBox.y - hitBox.height * 0.5f
};
}
ecs_entity_t renderCollidersSystem;
ecs_entity_t renderOrientDirSystem;
ecs_entity_t renderDebugPathSystem;
ECS_DTOR(SpatialGridID, gridID, {
Game *game = ecs_singleton_get_mut(ECS, Game);
bzSpatialGridRemove(game->entityGrid, *gridID);
})
ECS_MOVE(SpatialGridID, dst, src, {
*dst = *src;
})
ECS_DTOR(Path, path, {
Game *game = ecs_singleton_get_mut(ECS, Game);
BzObjectPool *pool = game->pools.pathData;
PathData *cur = path[i].paths;
while (cur) {
bzObjectPoolRelease(pool, cur);
cur = cur->next;
}
})
ECS_MOVE(Path, dst, src, {
*dst = *src;
})
ECS_DTOR(Building, building, {
Vec2i pos = building->pos;
Vec2i size = building->size;
Game *game = ecs_singleton_get_mut(ECS, Game);
bzTileMapSetCollisions(&game->map, false, COLL_LAYER_BUILDINGS, pos.x, pos.y, size.x, size.y);
})
ECS_MOVE(Building, dst, src, {
*dst = *src;
})
void setupSystems() {
ecs_set_hooks(ECS, SpatialGridID, {
.dtor = ecs_dtor(SpatialGridID),
.move_dtor = ecs_move(SpatialGridID)
});
ecs_set_hooks(ECS, Path, {
.dtor = ecs_dtor(Path),
.move_dtor = ecs_move(Path)
});
ecs_set_hooks(ECS, Building, {
.dtor = ecs_dtor(Building),
.move_dtor = ecs_move(Building)
});
ECS_OBSERVER(ECS, entityPathRemove, EcsOnRemove, Path);
ECS_OBSERVER(ECS, updateTextureOwnerTile, EcsOnSet, TextureRegion, Owner);
ECS_OBSERVER(ECS, buildingAddPopCapacity, EcsOnSet, AddPopCapacity, Owner);
ECS_OBSERVER(ECS, buildingRemovePopCapacity, EcsOnRemove, AddPopCapacity, Owner);
ECS_OBSERVER(ECS, entityConsumePopCapacity, EcsOnSet, ConsumePopCapacity, Owner);
ECS_OBSERVER(ECS, entityUnConsumePopCapacity, EcsOnRemove, ConsumePopCapacity, Owner);
ECS_SYSTEM(ECS, entityUpdateSpatialID, EcsOnUpdate, Position, HitBox, Velocity, SpatialGridID);
ECS_SYSTEM(ECS, entityUpdateKinematic, EcsOnUpdate, Position, Velocity, Steering, Unit);
ECS_SYSTEM(ECS, entityUpdatePhysics, EcsOnUpdate, Position, HitBox, Velocity, SpatialGridID);
ECS_SYSTEM(ECS, entityMoveToTarget, EcsOnUpdate, Position, Velocity, TargetPosition, Steering);
ECS_SYSTEM(ECS, entityFollowPath, EcsOnUpdate, Path);
ECS_SYSTEM(ECS, resetHarvestCount, EcsOnUpdate, Harvestable);
ECS_SYSTEM(ECS, updateAISystem, EcsOnUpdate, BzBTState);
ECS_SYSTEM(ECS, updateAnimationState, EcsOnUpdate, Animation, TextureRegion);
ECS_SYSTEM(ECS, updateAnimation, EcsOnUpdate, Animation, TextureRegion);
ECS_SYSTEM(ECS, updateEasingSystem, EcsOnUpdate, Easing, Position, HitBox, Rotation);
ECS_SYSTEM(ECS, renderDebugPath, EcsOnUpdate, Path);
ECS_SYSTEM(ECS, renderColliders, EcsOnUpdate, Position, HitBox);
ECS_SYSTEM(ECS, renderOrientationDirection, EcsOnUpdate, Position, Orientation);
renderDebugPathSystem = renderDebugPath;
renderOrientDirSystem = renderOrientationDirection;
renderCollidersSystem = renderColliders;
//ecs_enable(ECS, renderOrientDirSystem, false);
//ecs_enable(ECS, renderArmPositionSystem, false);
}