R-Type  2
Doom but in better
Loading...
Searching...
No Matches
SpawnSystem.cpp
Go to the documentation of this file.
1#include "SpawnSystem.hpp"
2
3#include <iostream>
4#include "Random.hpp"
5#include "Registry.hpp"
6#include "Time.hpp"
7
10
12{
14 if (spawn_timer <= 0.0f) {
15 float x = 825.f;
16 float y = randint(60, 540);
17 spawn_monster(r, x, y);
18 std::cout << "spawning monster..." << std::endl;
20 }
21}
22
24{
26 if (spawn_obstacle_timer <= 0.0f) {
27 float x = 2000.f; //change to random when we know window size
28 float y = 400.f;
29 spawn_obstacle(r, x, y);
31 }
32}
33
34void spawn_player(Registry &r, const float pos_x, const float pos_y, const uint32_t client_id, const std::string &username)
35{
36 Entity player = r.spawn_entity();
37 r.add_component<Position>(player, {pos_x, pos_y});
38 r.add_component<Image>(player, {image_enum::PLAYER_ASSET, 20.f, 20.f});
39 r.add_component<Collider>(player, {10.f});
40 r.add_component<Health>(player, {3, 3});
41 r.add_component<Weapon>(player, {1, .5f, 0.f, false});
42 r.add_component<PowerUp>(player, {false});
44 r.add_component<PlayerInfo>(player, {client_id, username});
45 GameMessage msg = {messageType::P_SPAWN, player, {0, image_enum::PLAYER_ASSET, 0, "", {pos_x, pos_y}}};
46 //username.copy(msg.msg.username, 8, 0);
47 r.dispatcher->notify(msg);
48}
49
50void spawn_monster(Registry &r, const float pos_x, const float pos_y)
51{
52 Entity monster = r.spawn_entity();
53 r.add_component<Position>(monster, {pos_x, pos_y});
54 r.add_component<Velocity>(monster, {-10.f, 0.f});
55 r.add_component<Image>(monster, {image_enum::MONSTER1_ASSET, 20.f, 20.f});
56 r.add_component<Collider>(monster, {10.f});
57 r.add_component<Health>(monster, {3, 3});
58 r.add_component<Weapon>(monster, {1, .5f, 1.f, true});
62 r.add_component<Lifetime>(monster, {35.f});
63 r.dispatcher->notify({messageType::P_SPAWN, monster, {0, image_enum::MONSTER1_ASSET, 0, "", {pos_x, pos_y}}});
64}
65
66void spawn_obstacle(Registry &r, const float pos_x, const float pos_y)
67{
68 Entity obstacle = r.spawn_entity();
69 r.add_component<Position>(obstacle, {pos_x, pos_y});
70 r.add_component<Velocity>(obstacle, {-.25f, 0.f});
71 r.add_component<Image>(obstacle, {image_enum::OBSTACLE1_ASSET, 20.f, 20.f});
72 //r.add_component<Collider>(obstacle, {10.f});
73 //optional r.add_component<Health>(obstacle, {3, 3});
75 r.add_component<Lifetime>(obstacle, {60.f});
76 r.dispatcher->notify({messageType::P_SPAWN, obstacle, {0, image_enum::OBSTACLE1_ASSET, 0, "", {pos_x, pos_y}}});
77}
78
79void spawn_missile(Registry &r, const float pos_x, const float pos_y, const type_enum owner)
80{
81 Entity missile = r.spawn_entity();
82 r.add_component<Position>(missile, {pos_x, pos_y});
83 r.add_component<Collider>(missile, {10.f});
85 r.add_component<Lifetime>(missile, {10.f});
86 if (owner == type_enum::PLAYER) {
89 r.add_component<Velocity>(missile, {100.f, 0.f});
90 r.dispatcher->notify({messageType::P_SPAWN, missile, {0, image_enum::PLAYER_MISSILE_ASSET, 0, "", {pos_x, pos_y}}});
91 }
92 else {
95 r.add_component<Velocity>(missile, {-100.f, 0.f});
96 r.dispatcher->notify({messageType::P_SPAWN, missile, {0, image_enum::MONSTER_MISSILE_ASSET, 0, "", {pos_x, pos_y}}});
97 }
98}
99
100void spawn_powerup(Registry &r, const float pos_x, const float pos_y, const loot_enum type)
101{
102 Entity powerup = r.spawn_entity();
103 r.add_component<Position>(powerup, {pos_x, pos_y});
104 r.add_component<Velocity>(powerup, {0.f, 0.f});
105 r.add_component<Image>(powerup, {image_enum::POWERUP_ASSET, 20.f, 20.f});
106 r.add_component<Collider>(powerup, {10.f});
108 r.add_component<LootDrop>(powerup, {type});
109 r.dispatcher->notify({messageType::P_SPAWN, powerup, {0, image_enum::POWERUP_ASSET, 0, "", {pos_x, pos_y}}});
110}
@ DEFAULT
Definition Behaviour.hpp:8
@ P_SPAWN
Represents a spawn event.
@ PLAYER_MISSILE_ASSET
Definition Image.hpp:14
@ PLAYER_ASSET
Definition Image.hpp:11
@ POWERUP_ASSET
Definition Image.hpp:18
@ MONSTER_MISSILE_ASSET
Definition Image.hpp:13
@ MONSTER1_ASSET
Definition Image.hpp:12
@ OBSTACLE1_ASSET
Definition Image.hpp:16
loot_enum
Defines the different types of loot that can be dropped in the game.
Definition LootDrop.hpp:10
@ NONE
Definition LootDrop.hpp:11
int randint(int min, int max)
Function to generate a random integer.
Definition Random.cpp:6
void spawn_obstacle(Registry &r, const float pos_x, const float pos_y)
Spawns an obstacle at the specified position.
void spawn_missile(Registry &r, const float pos_x, const float pos_y, const type_enum owner)
Spawns a missile at the specified position.
float spawn_timer
void spawn_monster_system(Registry &r)
Spawns a monster periodically based on the spawn timer.
void spawn_monster(Registry &r, const float pos_x, const float pos_y)
Spawns a monster at the specified position.
float spawn_obstacle_timer
void spawn_powerup(Registry &r, const float pos_x, const float pos_y, const loot_enum type)
Spawns a power-up at the specified position.
void spawn_player(Registry &r, const float pos_x, const float pos_y, const uint32_t client_id, const std::string &username)
Spawns a player with the specified username at the given position.
void spawn_obstacle_system(Registry &r)
Spawns obstacles periodically based on the spawn timer.
#define DEFAULT_TIMER
@ ALLY
Definition Team.hpp:11
@ ENEMY
Definition Team.hpp:12
Registry * r
Definition Tests.cpp:4
type_enum
Defines the different types of entities in the game.
Definition Type.hpp:11
@ OBSTACLE
Definition Type.hpp:15
@ POWERUP
Definition Type.hpp:16
@ MISSILE
Definition Type.hpp:14
@ PLAYER
Definition Type.hpp:12
@ MONSTER
Definition Type.hpp:13
Represents an entity in an Entity-Component-System (ECS) architecture.
Definition Entity.hpp:12
void notify(const GameMessage &event)
Notifies the subscribed listener of an event.
Manages entities and their associated components, enabling the creation, deletion,...
Definition Registry.hpp:23
Entity spawn_entity()
Creates a new entity.
Definition Registry.cpp:23
EventDispatcher * dispatcher
Definition Registry.hpp:191
reference< Component > add_component(const Entity &entity, Component &&component)
Adds a component to an entity.
Definition Registry.hpp:134
float deltaTime
The time difference between the current frame and the last frame.
Definition Time.cpp:4
Represents the behaviour of an entity, including the type and a timer for timed behaviours.
Definition Behaviour.hpp:20
Represents a circular collider for an entity, defined by its radius.
Definition Collider.hpp:10
A structure representing a game message.
Represents the health of an entity, including current and maximum health values.
Definition Health.hpp:10
Represents an image asset with a specified width and height.
Definition Image.hpp:29
Represents the remaining lifetime of an entity or object.
Definition Lifetime.hpp:10
Represents a loot drop with a specific loot type.
Definition LootDrop.hpp:23
Represents information about a player, specifically their username.
Represents the position of an entity in a 2D space.
Definition Position.hpp:10
Represents a power-up item and its enabled state.
Definition PowerUp.hpp:11
Represents the team affiliation of an entity.
Definition Team.hpp:22
Represents the type of an entity in the game.
Definition Type.hpp:26
Represents the velocity of an entity in a 2D space.
Definition Velocity.hpp:10
Represents a weapon and its attributes in the game.
Definition Weapon.hpp:11