Skip to content

Commit 109a452

Browse files
committedFeb 10, 2019
Update README
1 parent ab449bf commit 109a452

File tree

3 files changed

+40
-37
lines changed

3 files changed

+40
-37
lines changed
 

‎README.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
![](/assets/game.gif)
2+
13
Spacewar is a 2D C++ game built on the [ponyo](https://github.com/sean1188/ponyo) game engine.
24

35
# Requirements

‎assets/game.gif

5.03 MB
Loading

‎src/enemy.h

+38-37
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,8 @@ struct CEnemy : public Component<CEnemy> {
1313
// Every point in a path is stored in the nodes vector as a Vec2(x,y)
1414
Array<Vec2> nodesVector = {};
1515

16-
void addNode(Vec2 node) {
17-
nodesVector.push_back(node);
18-
}
19-
Array<Vec2> getNodes() {
20-
return nodesVector;
21-
}
16+
void addNode(Vec2 node) { nodesVector.push_back(node); }
17+
Array<Vec2> getNodes() { return nodesVector; }
2218
int currentNode = 0;
2319
int pathDir = -1;
2420
};
@@ -43,13 +39,13 @@ class SEnemy : public System
4339
CCollidable* enemyCollider = (CCollidable*)components[3];
4440
CBulletEmitter* enemyBulletEmitter = (CBulletEmitter*)components[4];
4541

46-
enemy->enemyPosition = Vec2(enemySprite->getX(), enemySprite->getY());
42+
enemy->enemyPosition = Vec2(enemySprite->getX(), enemySprite->getY());
4743

4844
if (enemy->enabled == true) {
4945
updateEnemyMotion(delta, enemyMotion, enemy);
50-
//if (enemyCollider->colliding) {
51-
// enemy->enabled = false;
52-
//}
46+
// if (enemyCollider->colliding) {
47+
// enemy->enabled = false;
48+
//}
5349
if (enemyBulletEmitter->collidingWithBullet == true) {
5450
enemy->enabled = false;
5551
}
@@ -62,38 +58,43 @@ class SEnemy : public System
6258

6359
void updateEnemyMotion(float delta, CMotion* enemyMotion, CEnemy* enemy)
6460
{
65-
Vec2 pathVector = pathFollowing(enemy);
66-
enemyMotion->velocity = (pathVector - enemy->enemyPosition) * delta * 250;
61+
Vec2 pathVector = pathFollowing(enemy);
62+
enemyMotion->velocity = (pathVector - enemy->enemyPosition) * delta * 250;
6763
}
6864

69-
virtual Vec2 pathFollowing(CEnemy* newEnemy) {
70-
Vec2 target = Vec2();
71-
72-
// check if there is an path already
73-
bool isEmpty = newEnemy->nodesVector.empty();
74-
75-
if (!isEmpty) {
76-
Array<Vec2> nodesVector = newEnemy->getNodes(); // if there is a path, store these nodes in a vector: nodes
77-
target = nodesVector[newEnemy->currentNode]; // get the current target nodes[0]
78-
79-
// check distance between enemy position vector and target position vector
80-
if (distance(newEnemy->enemyPosition, target) <= 100) {
81-
/*newEnemy->currentNode += 1;*/
82-
newEnemy->currentNode += newEnemy->pathDir;
83-
84-
if (newEnemy->currentNode >= nodesVector.size() || newEnemy->currentNode < 0) {
85-
newEnemy->pathDir *= -1;
86-
newEnemy->currentNode += newEnemy->pathDir;
87-
//newEnemy->currentNode = nodesVector.size() - 1;
88-
}
89-
}
90-
}
65+
virtual Vec2 pathFollowing(CEnemy* newEnemy)
66+
{
67+
Vec2 target = Vec2();
68+
69+
// check if there is an path already
70+
bool isEmpty = newEnemy->nodesVector.empty();
71+
72+
if (!isEmpty) {
73+
Array<Vec2> nodesVector =
74+
newEnemy->getNodes(); // if there is a path, store these nodes in a
75+
// vector: nodes
76+
target =
77+
nodesVector[newEnemy->currentNode]; // get the current target nodes[0]
78+
79+
// check distance between enemy position vector and target position vector
80+
if (distance(newEnemy->enemyPosition, target) <= 100) {
81+
/*newEnemy->currentNode += 1;*/
82+
newEnemy->currentNode += newEnemy->pathDir;
83+
84+
if (newEnemy->currentNode >= nodesVector.size() ||
85+
newEnemy->currentNode < 0) {
86+
newEnemy->pathDir *= -1;
87+
newEnemy->currentNode += newEnemy->pathDir;
88+
}
89+
}
90+
}
9191

92-
return !isEmpty ? target : Vec2(0,0);
92+
return !isEmpty ? target : Vec2(0, 0);
9393
}
9494

9595
// check distance between two vectors
96-
inline int distance(Vec2 a, Vec2 b) {
97-
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
96+
inline int distance(Vec2 a, Vec2 b)
97+
{
98+
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
9899
}
99100
};

0 commit comments

Comments
 (0)
Please sign in to comment.