Exploring raycasters and possibly make a little "doom like" game based on it.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
raycaster/components.cpp

62 lines
2.0 KiB

#include "components.hpp"
#include "point.hpp"
namespace components {
ENROLL_COMPONENT(Position, location.x, location.y);
ENROLL_COMPONENT(EnemyConfig, hearing_distance);
ENROLL_COMPONENT(Motion, dx, dy, random);
ENROLL_COMPONENT(Combat, hp, max_hp, damage, dead);
ENROLL_COMPONENT(Device, config, events);
ENROLL_COMPONENT(Animation, scale, simple, frames, speed);
ENROLL_COMPONENT(Sound, attack, death);
void configure_entity(const ComponentMap& component_map, DinkyECS::World& world, DinkyECS::Entity ent, json& data) {
for (auto &i : data) {
dbc::check(i.contains("_type") && i["_type"].is_string(), fmt::format("component has no _type: {}", data.dump()));
dbc::check(component_map.contains(i["_type"]), fmt::format("component_map doesn't have type {}", std::string(i["_type"])));
component_map.at(i["_type"])(world, ent, i);
}
}
void configure(ComponentMap& component_map) {
components::enroll<Combat>(component_map);
components::enroll<Loot>(component_map);
components::enroll<Position>(component_map);
components::enroll<Weapon>(component_map);
components::enroll<Curative>(component_map);
components::enroll<EnemyConfig>(component_map);
components::enroll<Tile>(component_map);
components::enroll<Motion>(component_map);
components::enroll<LightSource>(component_map);
components::enroll<Device>(component_map);
components::enroll<Sprite>(component_map);
components::enroll<Animation>(component_map);
components::enroll<Sound>(component_map);
}
void Animation::play() {
if(!playing) {
current = 0;
playing = true;
}
}
void Animation::step(sf::Vector2f& scale_out, sf::IntRect& rect_out) {
if(playing && current < frames) {
scale_out.x += scale;
scale_out.y += scale;
if(!simple) {
rect_out.position.x += current * TEXTURE_WIDTH;
}
subframe += speed;
current = int(subframe);
} else {
playing = false;
current = 0;
subframe = 0.0f;
}
}
}