RTYPE TECHNICAL DOCUMENTATION
  • Introduction
  • Classes
    • utils::InputMap
    • utils::Window
    • utils::constant::ButtonValue
    • network::Client
    • network::LockedQueue
    • network::Server
    • ecs::EnemyFactory
    • ecs::Engine
    • ecs::Entity
    • ecs::Event
    • ecs::Registry
    • ecs::SparseArray
    • ecs::World
    • ecs::WorldManager
    • ecs::component::Activable
    • ecs::component::Animated
    • ecs::component::Animated::AnimFrame
    • ecs::component::AttackAI
    • ecs::component::AttackAI::Action
    • ecs::component::AttackAI::AI
    • ecs::component::AttackAI::AI::Pattern
    • ecs::component::Controllable
    • ecs::component::Direction
    • ecs::component::Drawable
    • ecs::component::EntityType
    • ecs::component::Faction
    • ecs::component::FollowEntity
    • ecs::component::Health
    • ecs::component::Hitbox
    • ecs::component::MovementAI
    • ecs::component::MovementAI::AI
    • ecs::component::NetworkId
    • ecs::component::Parallax
    • ecs::component::Position
    • ecs::component::Projectile
    • ecs::component::Score
    • ecs::component::Shootable
    • ecs::component::Size
    • ecs::component::Text
    • ecs::component::textColor
    • ecs::component::Velocity
    • ecs::component::Weapon
    • audio::AudioManager
    • asset::AssetLoader
    • anim::Animation
  • Namespaces
    • utils
    • utils::constant
    • network
    • ecs
    • ecs::component
    • ecs::systems
    • audio
    • asset
    • anim
  • Modules
    • Input
  • Files
    • src
    • src/client
    • Animation.cpp
    • Animation.hpp
    • AssetLoader.cpp
    • AssetLoader.hpp
    • AudioManager.cpp
    • AudioManager.hpp
    • entrypoint.cpp
    • GetWorld.cpp
    • GetWorld.hpp
    • NetworkClient.cpp
    • NetworkClient.hpp
    • src/ecs
    • src/ecs/components
    • src/ecs/components/client
    • Activable.hpp
    • Animated.hpp
    • Controllable.hpp
    • Drawable.hpp
    • Hitbox.hpp
    • Parallax.hpp
    • Shootable.hpp
    • Text.hpp
    • src/ecs/components/server
    • AttackAI.cpp
    • AttackAI.hpp
    • FollowEntity.hpp
    • Projectile.hpp
    • Direction.hpp
    • EntityType.hpp
    • Faction.hpp
    • Health.hpp
    • MovementAI.cpp
    • MovementAI.hpp
    • NetworkId.hpp
    • Position.hpp
    • Score.hpp
    • Size.hpp
    • Velocity.hpp
    • Weapon.hpp
    • src/ecs/systems
    • src/ecs/systems/client
    • Animate.hpp
    • Draw.hpp
    • ExecuteOnce.hpp
    • HandleIncomingMessages.hpp
    • HandleParallaxBounds.hpp
    • HandleSFMLEvents.hpp
    • HandleSFMLKeys.hpp
    • HealthBar.hpp
    • MenuSelect.hpp
    • ScoreUpdate.hpp
    • SendDirection.hpp
    • src/ecs/systems/server
    • DeathUpdate.hpp
    • FollowEntitySystem.hpp
    • HandleIncomingMessage.hpp
    • PlayerHealthUpdate.hpp
    • PositionUpdate.hpp
    • ProjectileCollision.hpp
    • RunAttackAI.hpp
    • Waves.hpp
    • ManageClientEvents.hpp
    • Movement.hpp
    • PositionLogger.hpp
    • RunMovementAI.hpp
    • EnemyFactory.cpp
    • EnemyFactory.hpp
    • Engine.hpp
    • Entity.hpp
    • Event.hpp
    • LockedQueue.hpp
    • Registry.hpp
    • SparseArray.hpp
    • World.hpp
    • WorldManager.cpp
    • WorldManager.hpp
    • src/server
    • entrypoint.cpp
    • Server.cpp
    • Server.hpp
    • src/utils
    • Constant.hpp
    • InputMap.cpp
    • InputMap.hpp
    • Window.cpp
    • README.md
  • Pages
    • deprecated
  • GitHub
Powered by GitBook
On this page
  • Namespaces
  • Source code
  1. Files

AttackAI.cpp

Namespaces

Name

Source code

#include "AttackAI.hpp"
#include "EnemyFactory.hpp"
#include "WorldManager.hpp"
#include "components/Direction.hpp"
#include "components/EntityType.hpp"
#include "components/Faction.hpp"
#include "components/Health.hpp"
#include "components/NetworkId.hpp"
#include "components/Position.hpp"
#include "components/Size.hpp"
#include "components/Velocity.hpp"
#include "components/server/Projectile.hpp"

namespace ecs::component
{
    void AttackAI::Action::spawnNewBullet(component::EntityType::Types type, int posX, int posY, char dirX, char dirY,
        int sizeX, int sizeY, int velX, int velY, int dmg, ecs::component::Faction::Factions fac, int bulletHealth)
    {
        ecs::Entity bullet = ecs::WorldManager::getWorld().registry.spawn_entity();
        ecs::WorldManager::getWorld().registry.addComponent<ecs::component::EntityType>(
            bullet, {static_cast<char>(type)});
        ecs::WorldManager::getWorld().registry.addComponent<ecs::component::NetworkId>(
            bullet, {static_cast<size_t>(bullet)});
        ecs::WorldManager::getWorld().registry.addComponent<ecs::component::Position>(bullet, {posX, posY});
        ecs::WorldManager::getWorld().registry.addComponent<ecs::component::Direction>(bullet, {dirX, dirY});
        ecs::WorldManager::getWorld().registry.addComponent<ecs::component::Size>(bullet, {sizeX, sizeY});
        ecs::WorldManager::getWorld().registry.addComponent<ecs::component::Velocity>(bullet, {velX, velY});
        ecs::WorldManager::getWorld().registry.addComponent<ecs::component::Projectile>(bullet, {dmg});
        ecs::WorldManager::getWorld().registry.addComponent<ecs::component::Faction>(bullet, {fac});
        ecs::WorldManager::getWorld().registry.addComponent<ecs::component::Health>(bullet, {bulletHealth});
    }

    void AttackAI::Action::waitAttack(const std::size_t shooter) {}

    void AttackAI::Action::shootBulletAttack(const std::size_t shooter)
    {
        auto const &positions = ecs::WorldManager::getWorld().registry.getComponents<component::Position>();
        auto const &factions = ecs::WorldManager::getWorld().registry.getComponents<component::Faction>();

        if (!(shooter < positions.size()))
            return;
        if (!(positions[shooter]))
            return;
        ecs::component::Faction::Factions fac = ecs::component::Faction::Factions::None;
        if (shooter < factions.size() && factions[shooter])
            fac = factions[shooter].value().faction;
        AttackAI::Action::spawnNewBullet(component::EntityType::Bullet, positions[shooter].value().x,
            positions[shooter].value().y, -1, 0, 20, 20, 10, 0, 10, fac);
    }

    void AttackAI::Action::shootEnerySphereAttack(const std::size_t shooter)
    {
        auto const &positions = ecs::WorldManager::getWorld().registry.getComponents<component::Position>();
        auto const &factions = ecs::WorldManager::getWorld().registry.getComponents<component::Faction>();

        if (!(shooter < positions.size()))
            return;
        if (!(positions[shooter]))
            return;
        ecs::component::Faction::Factions fac = ecs::component::Faction::Factions::None;
        if (shooter < factions.size() && factions[shooter])
            fac = factions[shooter].value().faction;
        spawnNewBullet(component::EntityType::EnergySphere, positions[shooter].value().x, positions[shooter].value().y,
            -1, 0, 50, 50, 5, 0, 50, fac);
    }

    void AttackAI::Action::shootLaserAttack(const std::size_t shooter)
    {
        auto const &positions = ecs::WorldManager::getWorld().registry.getComponents<component::Position>();
        auto const &factions = ecs::WorldManager::getWorld().registry.getComponents<component::Faction>();

        if (!(shooter < positions.size()))
            return;
        if (!(positions[shooter]))
            return;
        ecs::component::Faction::Factions fac = ecs::component::Faction::Factions::None;
        if (shooter < factions.size() && factions[shooter])
            fac = factions[shooter].value().faction;
        AttackAI::Action::spawnNewBullet(component::EntityType::Laser, positions[shooter].value().x,
            positions[shooter].value().y, -1, 0, 6, 32, 15, 0, 5, fac);
    }

    void AttackAI::Action::playerBotLaser(const std::size_t shooter)
    {
        auto const &positions = ecs::WorldManager::getWorld().registry.getComponents<component::Position>();
        auto const &factions = ecs::WorldManager::getWorld().registry.getComponents<component::Faction>();

        if (!(shooter < positions.size()))
            return;
        if (!(positions[shooter]))
            return;
        ecs::component::Faction::Factions fac = ecs::component::Faction::Factions::None;
        if (shooter < factions.size() && factions[shooter])
            fac = factions[shooter].value().faction;
        AttackAI::Action::spawnNewBullet(component::EntityType::Laser, positions[shooter].value().x,
            positions[shooter].value().y, 1, 0, 6, 32, 15, 0, 5, fac);
    }

    void AttackAI::Action::shootRocketAttack(const std::size_t shooter)
    {
        auto const &positions = ecs::WorldManager::getWorld().registry.getComponents<component::Position>();
        auto const &factions = ecs::WorldManager::getWorld().registry.getComponents<component::Faction>();

        if (!(shooter < positions.size()))
            return;
        if (!(positions[shooter]))
            return;
        ecs::component::Faction::Factions fac = ecs::component::Faction::Factions::None;
        if (shooter < factions.size() && factions[shooter])
            fac = factions[shooter].value().faction;
        AttackAI::Action::spawnNewBullet(component::EntityType::Rocket, positions[shooter].value().x,
            positions[shooter].value().y, -1, 0, 30, 20, 15, 0, 25, fac);
    }

    void AttackAI::Action::invokeAlliesAttack(const std::size_t shooter)
    {
        auto const &positions = ecs::WorldManager::getWorld().registry.getComponents<component::Position>();
        auto const &factions = ecs::WorldManager::getWorld().registry.getComponents<component::Faction>();

        if (!(shooter < positions.size() && shooter < factions.size()))
            return;
        if (!(positions[shooter] && factions[shooter]))
            return;

        ecs::EnemyFactory::spawnEnemy(ecs::WorldManager::getWorld(), ecs::EnemyFactory::EnemyType::Fighter,
            positions[shooter].value().x - 75, positions[shooter].value().y - 75, factions[shooter].value().faction,
            MovementAI::AIType::ClockwiseSmall);
        ecs::EnemyFactory::spawnEnemy(ecs::WorldManager::getWorld(), ecs::EnemyFactory::EnemyType::Fighter,
            positions[shooter].value().x, positions[shooter].value().y - 50, factions[shooter].value().faction,
            MovementAI::AIType::ClockwiseBig);
        ecs::EnemyFactory::spawnEnemy(ecs::WorldManager::getWorld(), ecs::EnemyFactory::EnemyType::Fighter,
            positions[shooter].value().x + 50, positions[shooter].value().y + 50, factions[shooter].value().faction,
            MovementAI::AIType::AntiClockwiseSmall);
        ecs::EnemyFactory::spawnEnemy(ecs::WorldManager::getWorld(), ecs::EnemyFactory::EnemyType::Fighter,
            positions[shooter].value().x + 50, positions[shooter].value().y - 75, factions[shooter].value().faction,
            MovementAI::AIType::AntiClockwiseBig);
    }

    void AttackAI::Action::spawnAsteroidsAttack(const std::size_t shooter)
    {
        auto const &factions = ecs::WorldManager::getWorld().registry.getComponents<component::Faction>();

        ecs::component::Faction::Factions fac = ecs::component::Faction::Factions::None;
        if (shooter < factions.size() && factions[shooter])
            fac = factions[shooter].value().faction;
        AttackAI::Action::spawnNewBullet(component::EntityType::Asteroid, 400 + std::rand() % utils::constant::mapWidth,
            ((-utils::constant::mapHeight) / 2) + std::rand() % (utils::constant::mapHeight / 2), -1, 1, 100, 100, 10,
            10, 10, fac, 500);
        AttackAI::Action::spawnNewBullet(component::EntityType::Asteroid, 400 + std::rand() % utils::constant::mapWidth,
            ((-utils::constant::mapHeight) / 2) + std::rand() % (utils::constant::mapHeight / 2), -1, 1, 100, 100, 10,
            10, 10, fac, 500);
        AttackAI::Action::spawnNewBullet(component::EntityType::Asteroid, 400 + std::rand() % utils::constant::mapWidth,
            ((-utils::constant::mapHeight) / 2) + std::rand() % (utils::constant::mapHeight / 2), -1, 1, 100, 100, 10,
            10, 10, fac, 500);
        AttackAI::Action::spawnNewBullet(component::EntityType::Asteroid, 400 + std::rand() % utils::constant::mapWidth,
            ((-utils::constant::mapHeight) / 2) + std::rand() % (utils::constant::mapHeight / 2), -1, 1, 100, 100, 10,
            10, 10, fac, 500);
        AttackAI::Action::spawnNewBullet(component::EntityType::Asteroid, 400 + std::rand() % utils::constant::mapWidth,
            ((-utils::constant::mapHeight) / 2) + std::rand() % (utils::constant::mapHeight / 2), -1, 1, 100, 100, 10,
            10, 10, fac, 500);
        AttackAI::Action::spawnNewBullet(component::EntityType::Asteroid, 400 + std::rand() % utils::constant::mapWidth,
            ((-utils::constant::mapHeight) / 2) + std::rand() % (utils::constant::mapHeight / 2), -1, 1, 100, 100, 10,
            10, 10, fac, 500);
        AttackAI::Action::spawnNewBullet(component::EntityType::Asteroid, 400 + std::rand() % utils::constant::mapWidth,
            ((-utils::constant::mapHeight) / 2) + std::rand() % (utils::constant::mapHeight / 2), -1, 1, 100, 100, 10,
            10, 10, fac, 500);
        AttackAI::Action::spawnNewBullet(component::EntityType::Asteroid, 400 + std::rand() % utils::constant::mapWidth,
            ((-utils::constant::mapHeight) / 2) + std::rand() % (utils::constant::mapHeight / 2), -1, 1, 100, 100, 10,
            10, 10, fac, 500);
        AttackAI::Action::spawnNewBullet(component::EntityType::Asteroid, 400 + std::rand() % utils::constant::mapWidth,
            ((-utils::constant::mapHeight) / 2) + std::rand() % (utils::constant::mapHeight / 2), -1, 1, 100, 100, 10,
            10, 10, fac, 500);
        AttackAI::Action::spawnNewBullet(component::EntityType::Asteroid, 400 + std::rand() % utils::constant::mapWidth,
            ((-utils::constant::mapHeight) / 2) + std::rand() % (utils::constant::mapHeight / 2), -1, 1, 100, 100, 10,
            10, 10, fac, 500);
    }

    void AttackAI::Action::invokeAnyoneAttack(const std::size_t shooter)
    {
        auto const &positions = ecs::WorldManager::getWorld().registry.getComponents<component::Position>();

        if (!(shooter < positions.size()))
            return;
        if (!(positions[shooter]))
            return;

        ecs::EnemyFactory::spawnEnemy(ecs::WorldManager::getWorld(), ecs::EnemyFactory::EnemyType::Fighter,
            positions[shooter].value().x - 100, positions[shooter].value().y + 50, Faction::Factions::Uranus,
            MovementAI::AIType::LongUpDown);
        ecs::EnemyFactory::spawnEnemy(ecs::WorldManager::getWorld(), ecs::EnemyFactory::EnemyType::Fighter,
            positions[shooter].value().x - 100, positions[shooter].value().y + 100, Faction::Factions::Janitor,
            MovementAI::AIType::LongUpDown);
        ecs::EnemyFactory::spawnEnemy(ecs::WorldManager::getWorld(), ecs::EnemyFactory::EnemyType::Fighter,
            positions[shooter].value().x - 100, positions[shooter].value().y + 150, Faction::Factions::Alien,
            MovementAI::AIType::LongUpDown);
        ecs::EnemyFactory::spawnEnemy(ecs::WorldManager::getWorld(), ecs::EnemyFactory::EnemyType::Fighter,
            positions[shooter].value().x - 100, positions[shooter].value().y + 200, Faction::Factions::Uranus,
            MovementAI::AIType::LongUpDown);
        ecs::EnemyFactory::spawnEnemy(ecs::WorldManager::getWorld(), ecs::EnemyFactory::EnemyType::Fighter,
            positions[shooter].value().x - 100, positions[shooter].value().y + 250, Faction::Factions::Janitor,
            MovementAI::AIType::LongUpDown);
        ecs::EnemyFactory::spawnEnemy(ecs::WorldManager::getWorld(), ecs::EnemyFactory::EnemyType::Fighter,
            positions[shooter].value().x - 100, positions[shooter].value().y + 300, Faction::Factions::Alien,
            MovementAI::AIType::LongUpDown);
    }

    const std::unordered_map<AttackAI::PatternType, AttackAI::AI::Pattern> AttackAI::AI::patterns(
        {{Wait, AI::Pattern(1000, AttackAI::Action::waitAttack)},
            {WaitShort, AI::Pattern(500, AttackAI::Action::waitAttack)},
            {WaitLong, AI::Pattern(2000, AttackAI::Action::waitAttack)},
            {ShootBullet, AI::Pattern(250, AttackAI::Action::shootBulletAttack)},
            {ShootEnergySphere, AI::Pattern(500, AttackAI::Action::shootEnerySphereAttack)},
            {ShootEnergySphereFast, AI::Pattern(10, AttackAI::Action::shootEnerySphereAttack)},
            {ShootLaser, AI::Pattern(50, AttackAI::Action::shootLaserAttack)},
            {ShootRocket, AI::Pattern(350, AttackAI::Action::shootRocketAttack)},
            {InvokeAllies, AI::Pattern(2500, AttackAI::Action::invokeAlliesAttack)},
            {InvokeAnyone, AI::Pattern(1000, AttackAI::Action::invokeAnyoneAttack)},
            {SpawnAsteroids, AI::Pattern(250, AttackAI::Action::spawnAsteroidsAttack)},
            {PlayerBotLaser, AI::Pattern(50, AttackAI::Action::playerBotLaser)}});

    const std::unordered_map<AttackAI::AIType, AttackAI::AI> AttackAI::_aiVector({{None, AttackAI::AI({WaitLong})},
        {Battlecruiser, AttackAI::AI({ShootLaser, ShootBullet, ShootEnergySphere})},
        {Dreadnought, AttackAI::AI({InvokeAllies})}, {Fighter, AttackAI::AI({ShootLaser, ShootBullet})},
        {Frigate, AttackAI::AI({ShootEnergySphere})}, {Scout, AttackAI::AI({ShootLaser})},
        {Torpedo, AttackAI::AI({ShootRocket})},
        {NoodleMonster,
            AttackAI::AI({SpawnAsteroids, SpawnAsteroids, InvokeAnyone, ShootEnergySphereFast, ShootEnergySphereFast,
                ShootEnergySphereFast, ShootEnergySphereFast, ShootEnergySphereFast, ShootEnergySphereFast})},
        {PlayerBot, AttackAI::AI({PlayerBotLaser})}});
} // namespace ecs::component

Updated on 2022-11-13 at 17:21:37 +0100

Previoussrc/ecs/components/serverNextAttackAI.hpp

Last updated 2 years ago

ecs
ecs::component