The Table Engine
gameobjectfactory.cpp
Go to the documentation of this file.
1 #include <iostream>
2 
3 #include "gameobjectfactory.h"
4 
5 #include "texture.h"
6 #include "transform.h"
7 #include "animation.h"
8 #include "animationsmanager.h"
9 #include "collide.h"
10 #include "input.h"
11 #include "health.h"
12 #include "resourcemanager.h"
13 #include "player_input_script.h"
14 #include "projectile_script.h"
15 #include "enemy_ai_script.h"
16 #include "collision_script.h"
17 #include "ranged_weapon_script.h"
18 #include "crosshair_script.h"
19 
21 {
22  GameObject* player = new GameObject("Player");
23 
24  // Create animation manager and load all animations
25  auto animations = new AnimationsManager();
26 
27  // Idle animation
28  SDL_Texture* idleTexture = ResourceManager::getInstance().loadTexture("../Assets/PixelCrawler/Heroes/Knight/Idle/Idle-Sheet.bmp");
29  int idleAnim = animations->createAnimation(idleTexture, 1, 4, 0.5f);
30 
31  // Run animation
32  SDL_Texture* runTexture = ResourceManager::getInstance().loadTexture("../Assets/PixelCrawler/Heroes/Knight/Run/Run-Sheet.bmp");
33  animations->createAnimation(runTexture, 1, 6, 0.5f);
34 
35  // Death animation
36  SDL_Texture* deathTexture = ResourceManager::getInstance().loadTexture("../Assets/PixelCrawler/Heroes/Knight/Death/Death-Sheet.bmp");
37  animations->createAnimation(deathTexture, 1, 6, 0.5f);
38 
39  // Set initial animation
40  animations->setCurrentAnimation(idleAnim);
41  player->addComponent<Texture>(animations);
42 
43  // Rest of the player setup remains the same
44  auto transform = new Transform();
45  transform->setWorldPosition(200, 200);
46  transform->setWorldSize(40, 40);
47  player->addComponent<Transform>(transform);
48 
49  auto input = new Input();
50  player->addComponent<Input>(input);
51 
52  auto health = new Health(1000);
53  player->addComponent<Health>(health);
54 
55  auto playerMovement = new PlayerInputScript();
56  player->addScript<PlayerInputScript>(playerMovement);
57 
58  auto playerCollide = new Collide();
59  playerCollide->setScreenSize(transform->getScreenSize().x * 0.4, transform->getScreenSize().y * 0.6);
60  playerCollide->setTransformOffset(transform->getScreenSize().x * 0.3, transform->getScreenSize().y * 0.45);
61  player->addComponent<Collide>(playerCollide);
62 
63  return player;
64 }
65 
67 {
68  GameObject* bow = new GameObject("Bow");
69 
70  SDL_Texture* sdl_texture = ResourceManager::getInstance().loadTexture("../Assets/PixelCrawler/Weapons/Wood/Wood.bmp");
71  auto animation = new Animation(sdl_texture);
72  animation->setPositionInSpriteMap(48, 48);
73  animation->setSizeInSpriteMap(48, 32);
74  animation->setRowsColsInSpriteMap(1, 3);
75  animation->setAnimationTime(0.4);
76  animation->setAutoPlay(false);
77  animation->setFrame(0);
78  bow->addComponent<Animation>(animation);
79 
80  auto transform = new Transform();
81  transform->setWorldPosition(240, 248);
82  transform->setWorldSize(25, 25);
83  bow->addComponent<Transform>(transform);
84 
85  auto bowScript = new RangedWeaponScript();
86  bow->addScript<RangedWeaponScript>(bowScript);
87 
88  auto input = new Input();
89  bow->addComponent<Input>(input);
90 
91  auto bowCollide = new Collide();
92  bow->addComponent<Collide>(bowCollide);
93 
94  return bow;
95 }
96 
98 {
99  GameObject* arrow = new GameObject("Arrow");
100 
101  SDL_Texture* sdl_texture = ResourceManager::getInstance().loadTexture("../Assets/PixelCrawler/Weapons/Wood/Wood.bmp");
102  auto texture = new Texture(sdl_texture);
103 
104  if (abs(dx) >= abs(dy)) texture->setPositionInSpriteMap(32, 0); // horizontal arrow
105  else texture->setPositionInSpriteMap(16, 0); // vertical arrow
106  if (dx < 0) texture->setFlipHorizontal(true);
107  if (dy > 0) texture->setFlipVertical(true);
108 
109  texture->setSizeInSpriteMap(16, 16);
110  arrow->addComponent<Texture>(texture);
111 
112  auto transform = new Transform();
113  transform->setWorldPosition(225, 208);
114  transform->setWorldSize(15, 15);
115  arrow->addComponent<Transform>(transform);
116 
117  auto arrowMovement = new ProjectileScript(dx, dy);
118  arrow->addScript<ProjectileScript>(arrowMovement);
119 
120  auto arrowCollide = new Collide();
121  arrowCollide->setScreenPosition(transform->getScreenPosition());
122  arrowCollide->setScreenSize(transform->getScreenSize());
123  arrow->addComponent<Collide>(arrowCollide);
124 
125  return arrow;
126 }
127 
129 {
130  GameObject* enemy = new GameObject("Warrior");
131 
132  SDL_Texture* sdl_texture = ResourceManager::getInstance().loadTexture("../Assets/PixelCrawler/Enemy/OrcCrew/Warrior/Run/Run-Sheet.bmp");
133  auto texture = new Animation(sdl_texture);
134  texture->setRowsColsInSpriteMap(1, 6);
135  texture->setAnimationTime(0.5);
136  enemy->addComponent<Texture>(texture);
137 
138  auto transform = new Transform();
139  transform->setWorldSize(40, 40);
140  transform->setWorldPosition(50, 50);
141  enemy->addComponent<Transform>(transform);
142 
143  auto health = new Health(200);
144  enemy->addComponent<Health>(health);
145 
146  auto enemyAI = new EnemyAIScript();
147  enemy->addScript<EnemyAIScript>(enemyAI);
148 
149  auto enemyCollide = new Collide();
150  enemyCollide->setScreenSize(transform->getScreenSize().x * 0.4, transform->getScreenSize().y * 0.6);
151  enemyCollide->setTransformOffset(transform->getScreenSize().x * 0.3, transform->getScreenSize().y * 0.45);
152  enemy->addComponent<Collide>(enemyCollide);
153 
154  auto collideTest = new CollisionScript();
155  enemy->addScript<CollisionScript>(collideTest);
156 
157  return enemy;
158 }
159 
160 GameObject* GameObjectFactory::createEnemyWarriorCustom(std::string path, int row, int col, float animationTime, int pos_x, int pos_y, int size_x, int size_y)
161 {
162  GameObject* enemy = new GameObject("Warrior");
163 
164  SDL_Texture* sdl_texture = ResourceManager::getInstance().loadTexture(path);
165  auto texture = new Animation(sdl_texture);
166  texture->setRowsColsInSpriteMap(row, col);
167  texture->setAnimationTime(animationTime);
168  std::cout << animationTime << "\n";
169  enemy->addComponent<Texture>(texture);
170 
171  auto transform = new Transform();
172  transform->setWorldSize(size_x, size_y);
173  transform->setWorldPosition(pos_x, pos_y);
174  enemy->addComponent<Transform>(transform);
175 
176  auto health = new Health(200);
177  enemy->addComponent<Health>(health);
178 
179  auto enemyAI = new EnemyAIScript();
180  enemy->addScript<EnemyAIScript>(enemyAI);
181 
182  auto enemyCollide = new Collide();
183  enemyCollide->setScreenSize(transform->getScreenSize().x * 0.4, transform->getScreenSize().y * 0.6);
184  enemyCollide->setTransformOffset(transform->getScreenSize().x * 0.3, transform->getScreenSize().y * 0.45);
185  enemy->addComponent<Collide>(enemyCollide);
186 
187  auto collideTest = new CollisionScript();
188  enemy->addScript<CollisionScript>(collideTest);
189 
190  return enemy;
191 }
192 
194 {
195  GameObject* tile = new GameObject("Tile");
196 
197  SDL_Texture* sdl_texture = ResourceManager::getInstance().loadTexture("../Assets/PixelCrawler/Environment/DungeonPrison/Assets/Tiles.bmp");
198  auto texture = new Texture(sdl_texture);
199  texture->setSizeInSpriteMap(32, 32);
200  tile->addComponent<Texture>(texture);
201 
202  auto transform = new Transform();
203  transform->setWorldSize(32, 32);
204  tile->addComponent<Transform>(transform);
205 
206  return tile;
207 }
208 
209 GameObject* GameObjectFactory::createTileCustom(std::string path, Vec2 pos, Vec2 size, Vec2 sprite_size, Vec2 sprite_pos, Vec2 rc)
210 {
211  GameObject* tile = new GameObject("Tile");
212 
213  SDL_Texture* sdl_texture = ResourceManager::getInstance().loadTexture(path);
214  auto texture = new Animation(sdl_texture);
215  texture->setRowsColsInSpriteMap(rc.x, rc.y);
216  texture->setSizeInSpriteMap(sprite_size.x, sprite_size.y);
217  texture->setPositionInSpriteMap(sprite_pos.x, sprite_pos.y);
218  tile->addComponent<Texture>(texture);
219 
220  auto transform = new Transform();
221  transform->setWorldSize(size.x, size.y);
222  transform->setWorldPosition(pos.x, pos.y);
223  tile->addComponent<Transform>(transform);
224 
225  return tile;
226 }
227 
228 GameObject* GameObjectFactory::createWallCustom(std::string path, Vec2 pos, Vec2 size, Vec2 sprite_size, Vec2 sprite_pos)
229 {
230  GameObject* wall = new GameObject("Wall");
231 
232  SDL_Texture* sdl_texture = ResourceManager::getInstance().loadTexture(path);
233  auto texture = new Texture(sdl_texture);
234  // texture->setRowsColsInSpriteMap(rowcols.x, rowcols.y);
235  texture->setSizeInSpriteMap(sprite_size.x, sprite_size.y);
236  texture->setPositionInSpriteMap(sprite_pos.x, sprite_pos.y);
237  wall->addComponent<Texture>(texture);
238 
239  auto transform = new Transform();
240  transform->setWorldSize(size.x, size.y);
241  transform->setWorldPosition(pos.x, pos.y);
242  wall->addComponent<Transform>(transform);
243 
244  auto wallCollide = new Collide();
245  wallCollide->setScreenSize(transform->getScreenSize());
246  wall->addComponent<Collide>(wallCollide);
247 
248  return wall;
249 }
250 
251 GameObject* GameObjectFactory::createTableCustom(std::string path, Vec2 pos, Vec2 size, Vec2 sprite_size, Vec2 sprite_pos)
252 {
253  // same as a wall expect projectiles can fly over it
254  GameObject* table = new GameObject("Table");
255 
256  SDL_Texture* sdl_texture = ResourceManager::getInstance().loadTexture(path);
257  auto texture = new Texture(sdl_texture);
258  texture->setSizeInSpriteMap(sprite_size.x, sprite_size.y);
259  texture->setPositionInSpriteMap(sprite_pos.x, sprite_pos.y);
260  table->addComponent<Texture>(texture);
261 
262  auto transform = new Transform();
263  transform->setWorldSize(size.x, size.y);
264  transform->setWorldPosition(pos.x, pos.y);
265  table->addComponent<Transform>(transform);
266 
267  auto tableCollide = new Collide();
268  tableCollide->setScreenSize(transform->getScreenSize());
269  table->addComponent<Collide>(tableCollide);
270 
271  return table;
272 
273 }
274 
276 {
277  GameObject* key = new GameObject("Key");
278 
279  SDL_Texture* sdl_texture = ResourceManager::getInstance().loadTexture("../Assets/PixelCrawler/Environment/DungeonPrison/Assets/Props.bmp");
280  auto texture = new Texture(sdl_texture);
281  texture->setPositionInSpriteMap(32, 64);
282  texture->setSizeInSpriteMap(16, 16);
283  key->addComponent<Texture>(texture);
284 
285  auto transform = new Transform();
286  transform->setWorldSize(32, 32);
287  key->addComponent<Transform>(transform);
288 
289  auto keyCollide = new Collide();
290  keyCollide->setScreenSize(transform->getScreenSize());
291  key->addComponent<Collide>(keyCollide);
292 
293  return key;
294 }
295 
297  GameObject* crosshair = new GameObject("Crosshair");
298 
299  SDL_Texture* texture = ResourceManager::getInstance().loadTexture("../Assets/Misc/crosshair.bmp");
300  auto textureComponent = new Texture(texture);
301  crosshair->addComponent<Texture>(textureComponent);
302 
303  auto transform = new Transform();
304  transform->setWorldSize(16, 16);
305  crosshair->addComponent<Transform>(transform);
306 
307  auto input = new Input();
308  crosshair->addComponent<Input>(input);
309 
310  auto script = new CrosshairScript();
311  crosshair->addScript<CrosshairScript>(script);
312 
313  return crosshair;
314 }
315 
316 
Defines the Animation class for handling sprite animations.
Component to handle animation of game objects. Derived from Texture.
Definition: animation.h:21
Manages a collection of animations for a game entity, with functionality to play, pause,...
Component to handle collisions between game objects.
Definition: collide.h:24
Script to manage collisions between game objects.
Script for managing the crosshair in the game.
Script to manage enemy artificial intelligence.
static GameObject * createEnemyWarriorCustom(std::string path, int row, int col, float animationTime, int pos_x, int pos_y, int size_x, int size_y)
Creates a customized enemy warrior object with specific texture and animations.
static GameObject * createArrow(int dx, int dy)
Creates an arrow object with specified dynamics.
static GameObject * createTableCustom(std::string path, Vec2 pos, Vec2 size, Vec2 sprite_size, Vec2 sprite_pos)
Creates a customized table object with specific texture.
static GameObject * createKey()
Creates a key object.
static GameObject * createEnemyWarrior()
Creates an enemy warrior object.
static GameObject * createTileCustom(std::string path, Vec2 pos, Vec2 size, Vec2 sprite_size, Vec2 sprite_pos, Vec2 rc)
Creates a customized tile object with specific texture.
static GameObject * createTile1()
Creates a basic tile object.
static GameObject * createCrosshair()
Creates a crosshair object.
static GameObject * createWallCustom(std::string path, Vec2 pos, Vec2 size, Vec2 sprite_size, Vec2 sprite_pos)
Creates a customized wall object with specific texture.
static GameObject * createPlayerTest()
Creates a test player object.
static GameObject * createBow()
Creates a bow object.
Core class representing an entity in the game world.
Definition: gameobject.h:20
void addComponent(T *component)
Adds a component to the GameObject.
Definition: gameobject.h:133
void addScript(T *script)
Adds a script to the GameObject.
Definition: gameobject.h:150
A component for managing health of game objects.
Definition: health.h:22
Component for handling and storing the state of input devices.
Definition: input.h:20
Handles player interactions, movements, and animation states based on game input.
Controls the movement of a projectile.
Controls the behavior of ranged weapons such as bows or guns.
SDL_Texture * loadTexture(std::string path)
Loads a texture from a specified file path.
static ResourceManager & getInstance()
Retrieves the singleton instance of ResourceManager.
Component for handling textures in the game engine.
Definition: texture.h:20
Component for handling the transformation of game objects.
Definition: transform.h:23
Defines the Collide class, a component for managing collision detection and response.
Defines the Health class, a component for managing the health of game objects.
Defines the Input component for handling input operations in the game engine.
Manages resources such as textures, fonts, and potentially sounds for a game engine.
A structure to represent 2D vectors.
Definition: vec2.h:16
float y
Y coordinate of the vector.
Definition: vec2.h:18
float x
X coordinate of the vector.
Definition: vec2.h:17
Defines the Texture component for managing textures within the Game Engine, encapsulating SDL texture...
Provides the Transform component for positioning and sizing game objects.