Skip to content

Commit 7f5bbc8

Browse files
committed
2 parents 98bb162 + 42cd311 commit 7f5bbc8

File tree

12 files changed

+260
-29
lines changed

12 files changed

+260
-29
lines changed

BombingAdventure/CMakeLists.txt

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,11 @@ set(GAME_SRC
162162
Classes/RulesScene.cpp
163163
Classes/Entity.cpp
164164
Classes/Player.cpp
165-
# Classes/test_moving_scene.cpp
166-
# Classes/Monster.cpp
167-
# Classes/MonsterController.cpp
165+
Classes/Monster.cpp
166+
Classes/MonsterController.cpp
168167
Classes/Item.cpp
169168
Classes/Bomb.cpp
170-
)
169+
Classes/ItemController.cpp)
171170

172171

173172
set(GAME_HEADERS
@@ -178,12 +177,11 @@ set(GAME_HEADERS
178177
Classes/RulesScene.h
179178
Classes/Entity.h
180179
Classes/Player.h
181-
# Classes/test_moving_scene.h
182-
# Classes/Monster.h
183-
# Classes/MonsterController.h
180+
Classes/Monster.h
181+
Classes/MonsterController.h
184182
Classes/Item.h
185183
Classes/Bomb.h
186-
)
184+
Classes/ItemController.h)
187185

188186
# add the executable
189187
if(ANDROID)

BombingAdventure/Classes/Entity.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,21 @@ Sprite* Entity::get_sprite() {
1818
void Entity::bind_sprite(Sprite *sprite) {
1919
this->entity_sprite = sprite;
2020
this->addChild(entity_sprite);
21+
22+
/* Reset the content size and fit the mid-points */
23+
24+
Size entity_size = entity_sprite->getContentSize();
25+
entity_sprite->setAnchorPoint(Vec2(0.5,0.5));
26+
this->setAnchorPoint(Vec2(0.5, 0.5));
27+
entity_sprite->setPosition(Vec2(entity_size.width * 0.5f, entity_size.height * 0.5f));
28+
this->setContentSize(entity_size);
29+
2130
}
22-
const Size& Entity::getContentSize() {
23-
if (get_sprite() == NULL) {
24-
return ENTITY_DEFAULT_SIZE;
31+
void Entity::reset_position() {
32+
if(get_sprite() != NULL) {
33+
setPosition(Vec2(CCRANDOM_0_1() * 960, CCRANDOM_0_1() * 640));
2534
}
26-
return this->get_sprite()->getContentSize();
2735
}
28-
2936
Vec2 Entity::tileCoordFromPosition(Vec2 position)
3037
{
3138
int x = position.x / TILE_SIZE.width;

BombingAdventure/Classes/Entity.h

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,7 @@ class Entity : public Node {
1717
~Entity();
1818
Sprite* get_sprite(); /* Get the sprite of this entity */
1919
void bind_sprite(Sprite* sprite); /* Bind to sprite object */
20-
Point get_position(); /* Return the position of this entity */
21-
22-
/* Method Overriding: getContentSize
23-
* Usage: Size size = Entity->getContentSize();
24-
* -------------------------------------------------------
25-
* Get the size of the Entity, which equals to its sprite.
26-
*/
27-
virtual const Size & getContentSize();
28-
20+
virtual void reset_position(); /* Reset the position of this entity */
2921
cocos2d::Vec2 tileCoordFromPosition(cocos2d::Vec2 position);
3022

3123
private:

BombingAdventure/Classes/GameScene.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,13 @@ bool GameScene::init() {
149149
/* Enable update function of the GameScene, which controls the refreshments */
150150
this->scheduleUpdate();
151151

152+
/* Initialize a MonsterController */
153+
MonsterController * monster_controller = MonsterController::create();
154+
this->addChild(monster_controller,499);
155+
156+
/* Initialize an ItemController */
157+
ItemController * item_controller = ItemController::create();
158+
this->addChild(item_controller,498);
152159
return true;
153160
}
154161

@@ -183,7 +190,6 @@ void GameScene::update(float delta) {
183190
if (item_position == hero_position) {
184191
hero->pick_item(*speed_up_item);
185192
}
186-
187193
}
188194

189195
void GameScene::bomb_explode(Bomb *bomb)

BombingAdventure/Classes/GameScene.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33

44
#include "cocos2d.h"
55
#include "Player.h"
6-
7-
#include "Bomb.h"
6+
#include "MonsterController.h"
7+
#include "Monster.h"
8+
#include "ItemController.h"
89
#include "Item.h"
9-
//#include "Monster.h"
1010

1111
const cocos2d::Size TILE_SIZE(40, 40);
1212
const cocos2d::Size MAP_SIZE(24, 16);
@@ -76,7 +76,7 @@ class GameScene : public cocos2d::Layer
7676
* | | | | |
7777
* | | | | |
7878
* *-----*-----*-----*-----*
79-
* |
79+
* |
8080
* |
8181
* v
8282
* y-axis (tile)
@@ -110,8 +110,7 @@ class GameScene : public cocos2d::Layer
110110
// Monster * monster3;
111111

112112
/* Test class Player part */
113-
Player * hero; /* This is the player control by human */
114-
113+
Player * hero = NULL; /* This is the player control by human */
115114
// Map<Vec2, STATUS_AT*> status_map;
116115

117116
};
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//
2+
// Created by Brando Zhang on 2018/5/19.
3+
//
4+
5+
#include "ItemController.h"
6+
7+
bool ItemController::init() {
8+
/* Get current time to initialize a random seed */
9+
timeval now;
10+
gettimeofday(&now, NULL);
11+
auto seed = (unsigned) (now.tv_sec * 1000 + now.tv_usec / 1000);
12+
srand(seed);
13+
14+
create_item();
15+
/* Enable update */
16+
this->scheduleUpdate();
17+
return true;
18+
}
19+
void ItemController::update(float delta) {
20+
21+
}
22+
void ItemController::create_item() {
23+
for (int i = 0; i < NUM_ITEMS; i++) {
24+
25+
/* Create NUM_ITEMS items */
26+
Item * item = Item::create();
27+
item->reset_position();
28+
29+
/* Make the item under the control of ItemController */
30+
this->addChild(item);
31+
current_item_vector.pushBack(item);
32+
}
33+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//
2+
// Created by Brando Zhang on 2018/5/19.
3+
//
4+
5+
#ifndef BOMBING_ADVENTURE_ITEMCONTROLLER_H
6+
#define BOMBING_ADVENTURE_ITEMCONTROLLER_H
7+
8+
#include "cocos2d.h"
9+
#include "Item.h"
10+
11+
USING_NS_CC;
12+
13+
/* Following are the attributes of Item Controller */
14+
const int NUM_ITEMS = 5;
15+
16+
class ItemController : public Node {
17+
public:
18+
CREATE_FUNC(ItemController);
19+
/* Initiate Method */
20+
virtual bool init();
21+
22+
/* Override update method */
23+
virtual void update(float delta);
24+
private:
25+
/* Method: create_item
26+
* ----------------------
27+
* Create an item
28+
*/
29+
void create_item();
30+
31+
/* Store all the Items */
32+
Vector<Item*> current_item_vector;
33+
};
34+
35+
#endif //BOMBING_ADVENTURE_ITEMCONTROLLER_H
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//
2+
// Created by Brando Zhang on 2018/5/16.
3+
//
4+
#include "Monster.h"
5+
6+
Monster::Monster() {
7+
/* Avoid inheriting unnecessary children from superclass */
8+
this->removeAllChildren();
9+
10+
/* Default attributes of a new monster */
11+
HP = MONSTER_DEFAULT_HP;
12+
moving_speed = MONSTER_DEFAULT_MOVING_SPEED;
13+
num_max_available_bombs = MONSTER_DEFAULT_AVAILABLE_BOMBS;
14+
num_present_bombs = 0;
15+
16+
/* Default view of a new monster */
17+
bind_sprite(Sprite::create("stone_monster.png"));
18+
this->setAnchorPoint(Vec2(0.5,0.5));
19+
}
20+
Monster::~Monster() {
21+
/* Not yet */
22+
}
23+
bool Monster::init() {
24+
return true;
25+
}
26+
int Monster::get_new_direction() {
27+
int new_direction = floor(CCRANDOM_0_1() * 4);
28+
return new_direction;
29+
}
30+
//void Monster::move_forward(int new_direction) {
31+
// step = CCRANDOM_0_1() * 100;
32+
// switch (new_direction) {
33+
// case MOVE_UP:
34+
//
35+
// }
36+
//}

BombingAdventure/Classes/Monster.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//
2+
// Created by Brando Zhang on 2018/5/16.
3+
//
4+
5+
#ifndef BOMBING_ADVENTURE_MONSTER_H
6+
#define BOMBING_ADVENTURE_MONSTER_H
7+
8+
#include "cocos2d.h"
9+
#include "Player.h"
10+
11+
USING_NS_CC;
12+
//enum direction { MOVE_UP, MOVE_DOWN, MOVE_LEFT, MOVE_RIGHT };
13+
14+
/* Following are the attributes of Monster */
15+
const int MONSTER_DEFAULT_HP = 2;
16+
const int MONSTER_DEFAULT_AVAILABLE_BOMBS = 10;
17+
const float MONSTER_DEFAULT_MOVING_SPEED = 8.0f;
18+
19+
class Monster : public Player {
20+
public:
21+
/* Constructor */
22+
Monster();
23+
/* Destructor */
24+
~Monster();
25+
26+
CREATE_FUNC(Monster);
27+
28+
/* Initiate Method */
29+
virtual bool init();
30+
31+
/* Method: get_new_direction */
32+
int get_new_direction();
33+
/* Method: move_forward */
34+
// void move_forward(int new_direction);
35+
36+
private:
37+
enum direction { MOVE_UP, MOVE_DOWN, MOVE_LEFT, MOVE_RIGHT };
38+
39+
};
40+
41+
#endif //BOMBING_ADVENTURE_MONSTER_H
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//
2+
// Created by Brando Zhang on 2018/5/16.
3+
//
4+
5+
#include "MonsterController.h"
6+
7+
bool MonsterController::init() {
8+
/* Get current time to initialize a random seed */
9+
timeval now;
10+
gettimeofday(&now, NULL);
11+
auto seed = (unsigned) (now.tv_sec * 1000 + now.tv_usec / 1000);
12+
srand(seed);
13+
14+
create_monster();
15+
/* Enable update */
16+
this->scheduleUpdate();
17+
return true;
18+
}
19+
void MonsterController::update(float delta) {
20+
for (Monster* monster: current_monster_vector) {
21+
if (monster->is_alive()) {
22+
Vec2 monster_pos_in_pixel = monster->getPosition();
23+
Vec2 monster_pos_in_tile = monster->tileCoordFromPosition(monster_pos_in_pixel);
24+
if (monster_pos_in_tile.x < 0 || monster_pos_in_tile.x > 40
25+
|| monster_pos_in_tile.y < 0 || monster_pos_in_tile.y > 40) {
26+
monster->reset_position();
27+
}
28+
}
29+
}
30+
}
31+
void MonsterController::create_monster() {
32+
for (int i = 0; i < NUM_MONSTERS; i++) {
33+
34+
/* Create NUM_MONSTERS monsters */
35+
Monster * monster = Monster::create();
36+
monster->reset_position();
37+
38+
/* Make the monster under the control of MonsterController */
39+
this->addChild(monster);
40+
current_monster_vector.pushBack(monster);
41+
}
42+
}

0 commit comments

Comments
 (0)