Skip to content

Commit 2aced20

Browse files
grantaddisGrant Addis
authored and
Grant Addis
committed
added enemy factory
1 parent a47fd23 commit 2aced20

File tree

5 files changed

+86
-27
lines changed

5 files changed

+86
-27
lines changed

Splatter.xcodeproj/project.pbxproj

+6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
/* Begin PBXBuildFile section */
1010
2F63C02F1BC8366800533977 /* Player.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2F63C02D1BC8366800533977 /* Player.cpp */; settings = {ASSET_TAGS = (); }; };
11+
2F63C0321BC9ACDD00533977 /* EnemyFactory.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2F63C0301BC9ACDD00533977 /* EnemyFactory.cpp */; settings = {ASSET_TAGS = (); }; };
1112
AE9BA0E51BC82B8A00A91BF6 /* SDL2_image.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = AE9BA0E11BC82B8A00A91BF6 /* SDL2_image.framework */; };
1213
AE9BA0E61BC82B8A00A91BF6 /* SDL2_mixer.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = AE9BA0E21BC82B8A00A91BF6 /* SDL2_mixer.framework */; };
1314
AE9BA0E71BC82B8A00A91BF6 /* SDL2_ttf.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = AE9BA0E31BC82B8A00A91BF6 /* SDL2_ttf.framework */; };
@@ -50,6 +51,8 @@
5051
/* Begin PBXFileReference section */
5152
2F63C02D1BC8366800533977 /* Player.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Player.cpp; sourceTree = "<group>"; };
5253
2F63C02E1BC8366800533977 /* Player.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = Player.hpp; sourceTree = "<group>"; };
54+
2F63C0301BC9ACDD00533977 /* EnemyFactory.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = EnemyFactory.cpp; sourceTree = "<group>"; };
55+
2F63C0311BC9ACDD00533977 /* EnemyFactory.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = EnemyFactory.hpp; sourceTree = "<group>"; };
5356
AE9BA0CE1BC827FD00A91BF6 /* Splatter */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Splatter; sourceTree = BUILT_PRODUCTS_DIR; };
5457
AE9BA0E11BC82B8A00A91BF6 /* SDL2_image.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SDL2_image.framework; path = ../../../Library/Frameworks/SDL2_image.framework; sourceTree = "<group>"; };
5558
AE9BA0E21BC82B8A00A91BF6 /* SDL2_mixer.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SDL2_mixer.framework; path = ../../../Library/Frameworks/SDL2_mixer.framework; sourceTree = "<group>"; };
@@ -154,6 +157,8 @@
154157
AE9BA1061BC8351D00A91BF6 /* mainmenu_state.cpp */,
155158
AE9BA1071BC8351D00A91BF6 /* mainmenu_state.h */,
156159
AE9BA1081BC8351D00A91BF6 /* RunState.cpp */,
160+
2F63C0301BC9ACDD00533977 /* EnemyFactory.cpp */,
161+
2F63C0311BC9ACDD00533977 /* EnemyFactory.hpp */,
157162
AE9BA1091BC8351D00A91BF6 /* RunState.hpp */,
158163
AE9BA11E1BC9509000A91BF6 /* GameOverState.cpp */,
159164
AE9BA11F1BC9509000A91BF6 /* GameOverState.hpp */,
@@ -258,6 +263,7 @@
258263
AE9BA11D1BC848CB00A91BF6 /* Enemy.cpp in Sources */,
259264
BC08342A1BC8662400C4F054 /* Bullet.cpp in Sources */,
260265
AE9BA1121BC8351D00A91BF6 /* Timer.cpp in Sources */,
266+
2F63C0321BC9ACDD00533977 /* EnemyFactory.cpp in Sources */,
261267
2F63C02F1BC8366800533977 /* Player.cpp in Sources */,
262268
AE9BA10C1BC8351D00A91BF6 /* Engine.cpp in Sources */,
263269
AE9BA1111BC8351D00A91BF6 /* RunState.cpp in Sources */,

Splatter/EnemyFactory.cpp

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//
2+
// EnemyFactory.cpp
3+
// Splatter
4+
//
5+
// Created by Grant Addis on 10/10/15.
6+
// Copyright © 2015 WesHack. All rights reserved.
7+
//
8+
9+
#include "EnemyFactory.hpp"
10+
11+
void EnemyFactory::addEnemy(Engine* game, double x, double y, Player *player) {
12+
Enemy newEnemy;
13+
newEnemy.Init(game, x, y, player);
14+
enemies.push_back(newEnemy);
15+
}
16+
17+
void EnemyFactory::updateEnemies(Engine *game) {
18+
for (int i = 0; i < enemies.size(); i++){
19+
enemies[i].Update(game);
20+
}
21+
}
22+
23+
void EnemyFactory::drawEnemies(Engine *game) {
24+
for (int i = 0; i < enemies.size(); i++){
25+
enemies[i].Draw(game);
26+
}
27+
}
28+
29+
bool EnemyFactory::bulletDidHidEnemy(Bullet *bullet) {
30+
for (int i = 0; i < enemies.size(); i++) {
31+
if (bullet->HasCollided(&enemies[i])) {
32+
enemies[i].Destroy();
33+
enemies.erase(enemies.begin() + i);
34+
i--;
35+
return true;
36+
}
37+
}
38+
return false;
39+
}
40+
41+
void EnemyFactory::despawnAll() {
42+
enemies.clear();
43+
}

Splatter/EnemyFactory.hpp

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//
2+
// EnemyFactory.hpp
3+
// Splatter
4+
//
5+
// Created by Grant Addis on 10/10/15.
6+
// Copyright © 2015 WesHack. All rights reserved.
7+
//
8+
9+
#ifndef EnemyFactory_hpp
10+
#define EnemyFactory_hpp
11+
12+
#include <stdio.h>
13+
#include "Engine.h"
14+
#include "Enemy.hpp"
15+
#include "Player.hpp"
16+
#include "Bullet.hpp"
17+
18+
class EnemyFactory {
19+
public:
20+
void addEnemy(Engine* game, double x, double y, Player* player);
21+
void updateEnemies(Engine* game);
22+
void drawEnemies(Engine* game);
23+
bool bulletDidHidEnemy(Bullet* bullet);
24+
void despawnAll();
25+
26+
private:
27+
vector<Enemy> enemies;
28+
};
29+
30+
#endif /* EnemyFactory_hpp */

Splatter/RunState.cpp

+5-25
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,13 @@ void RunState::Init(Engine *game){
2828
p.map = map;
2929

3030
vector <Bullet> bullets (50);
31-
32-
enemies.push_back(Enemy());
33-
34-
for (int i = 0; i < enemies.size(); i ++){
35-
enemies[i].Init(game, 800, 100, &p);
36-
}
3731

32+
enemyFactory.addEnemy(game, 800, 100, &p);
3833

39-
4034
}
4135

4236
void RunState::Reset(){
43-
enemies.clear();
37+
enemyFactory.despawnAll();
4438
bullets.clear();
4539
}
4640

@@ -75,27 +69,15 @@ void RunState::HandleEvents(Engine *game, SDL_Event event){
7569
void RunState::Update(Engine *game){
7670
p.Update(game);
7771

78-
for (int i = 0; i < enemies.size(); i++){
79-
enemies[i].Update(game);
80-
81-
}
72+
enemyFactory.updateEnemies(game);
8273

8374
for (int i = 0; i < bullets.size(); i++){
8475
bullets[i].Update(game);
8576

86-
87-
88-
for (int j = 0; j < enemies.size(); j++){
89-
90-
if (bullets[i].HasCollided(&enemies[j])){
91-
enemies[j].Destroy();
92-
enemies.erase(enemies.begin() + j);
93-
j--;
94-
77+
if (enemyFactory.bulletDidHidEnemy(&bullets[i])) {
9578
bullets.erase(bullets.begin() + i);
9679
i--;
9780
}
98-
}
9981
//
10082
if (p.HasCollided(&bullets[i])){
10183
p.Destroy();
@@ -122,9 +104,7 @@ void RunState::Draw(Engine *game){
122104

123105
p.Draw(game);
124106

125-
for (int i = 0; i < enemies.size(); i++){
126-
enemies[i].Draw(game);
127-
}
107+
enemyFactory.drawEnemies(game);
128108

129109
for (int i = 0; i < bullets.size(); i++) {
130110
bullets[i].Draw(game);

Splatter/RunState.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "Player.hpp"
1717
#include "Enemy.hpp"
1818
#include "Bullet.hpp"
19+
#include "EnemyFactory.hpp"
1920

2021
using namespace std;
2122

@@ -49,8 +50,7 @@ class RunState : public GameState
4950
SDL_Rect map;
5051
Player p;
5152
vector<Bullet> bullets;
52-
vector<Enemy> enemies;
53-
53+
EnemyFactory enemyFactory;
5454
};
5555

5656

0 commit comments

Comments
 (0)