feat: add basic tower mecanic

This commit is contained in:
2021-09-28 19:19:54 +02:00
parent 26d1dd9d36
commit 1e35146c4b
8 changed files with 104 additions and 26 deletions

View File

@@ -6,6 +6,12 @@
namespace td {
namespace game {
bool Tower::isMobInRange(MobPtr mob) {
if(!mob->isAlive())
return false;
return (m_X - mob->getX()) * (m_X - mob->getX()) + (m_Y - mob->getY()) * (m_Y - mob->getY()) < (getStats()->getRange() * getStats()->getRange());
}
const std::map<std::pair<TowerType, TowerLevel>, TowerStats> TowerConstants = {
// // rate damage range
{{TowerType::Archer, {1, TowerPath::Top}}, {2, 5, 10}},
@@ -161,7 +167,28 @@ TowerPtr createTower(TowerType type, TowerID id, std::int32_t x, std::int32_t y,
void ArcherTower::tick(std::uint64_t delta, World* world) {
if (m_Timer.update(delta)) {
std::uint8_t arrowsShot = 0;
bool explosiveArrows = getLevel().getPath() == TowerPath::Bottom;
std::uint8_t arrows = explosiveArrows ? 2 : getLevel().getLevel();
for (MobPtr mob : world->getMobList()) {
if (isMobInRange(mob)) {
shootArrow(mob);
arrowsShot++;
if(arrowsShot >= arrows)
break;
}
}
}
}
void ArcherTower::shootArrow(MobPtr target){
bool explosiveArrows = getLevel().getPath() == TowerPath::Bottom;
if(explosiveArrows){
}else{
target->damage(getStats()->getDamage());
}
}
void IceTower::tick(std::uint64_t delta, World* world) {

View File

@@ -322,8 +322,9 @@ bool World::saveMap(const std::string& fileName) const {
void World::tick(std::uint64_t delta) {
moveMobs(delta);
for(TowerPtr tower : m_Towers){
tower->tick(delta);
tower->tick(delta, this);
}
cleanDeadMobs();
}
void World::spawnMobAt(MobID id, MobType type, std::uint8_t level, PlayerID sender, float x, float y, Direction dir) {
@@ -447,6 +448,15 @@ bool World::CanPlaceBigTower(const glm::vec2& worldPos, PlayerID playerID) const
return false;
}
void World::cleanDeadMobs(){
for(auto it = m_Mobs.begin(); it != m_Mobs.end(); it++){
MobPtr mob = *it;
if(!mob->isAlive()){
m_Mobs.erase(it);
}
}
}
Team& World::getRedTeam() {
return m_Game->getRedTeam();
}

View File

@@ -4,7 +4,7 @@
namespace td {
namespace utils {
void Timer::update() {
void AutoTimer::update() {
m_InternalTime += getTime() - m_LastTime;
if (m_InternalTime >= m_Interval) {
if (m_Function != nullptr)
@@ -14,7 +14,7 @@ void Timer::update() {
m_LastTime = getTime();
}
void Timer::update(std::uint64_t delta) {
void AutoTimer::update(std::uint64_t delta) {
m_InternalTime += delta;
if (m_InternalTime >= m_Interval) {
if (m_Function != nullptr)
@@ -24,11 +24,24 @@ void Timer::update(std::uint64_t delta) {
m_LastTime = getTime();
}
void Timer::reset() {
void AutoTimer::reset() {
m_InternalTime = 0;
m_LastTime = getTime();
}
bool Timer::update(std::uint64_t delta){
m_InternalTime += delta;
if (m_InternalTime >= m_Interval) {
m_InternalTime %= m_Interval;
return true;
}
return false;
}
void Timer::reset(){
m_InternalTime = 0;
}
std::uint64_t getTime() {
return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock().now().time_since_epoch()).count();
}