Skip to content

Commit 0d76d1c

Browse files
authored
Merge pull request #20 from BrandoZhang/master
Update ItemController.
2 parents 038211b + c2ed3ba commit 0d76d1c

File tree

8 files changed

+87
-33
lines changed

8 files changed

+87
-33
lines changed

BombingAdventure/CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,8 @@ set(GAME_SRC
166166
Classes/MonsterController.cpp
167167
Classes/Item.cpp
168168
Classes/Bomb.cpp
169-
Classes/ItemController.cpp)
169+
Classes/ItemController.cpp
170+
)
170171

171172

172173
set(GAME_HEADERS
@@ -181,7 +182,8 @@ set(GAME_HEADERS
181182
Classes/MonsterController.h
182183
Classes/Item.h
183184
Classes/Bomb.h
184-
Classes/ItemController.h)
185+
Classes/ItemController.h
186+
)
185187

186188
# add the executable
187189
if(ANDROID)

BombingAdventure/Classes/Entity.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ void Entity::bind_sprite(Sprite *sprite) {
3030
}
3131
void Entity::reset_position() {
3232
if(get_sprite() != NULL) {
33-
setPosition(Vec2(CCRANDOM_0_1() * 960, CCRANDOM_0_1() * 640));
33+
/* Generate a position in unit Tile */
34+
int target_tile_x = (int) floor(CCRANDOM_0_1() * MAP_SIZE.width);
35+
int target_tile_y = (int) floor(CCRANDOM_0_1() * MAP_SIZE.height);
36+
/* Set to the position in unit pixel, did not add robust part */
37+
setPosition(Vec2(target_tile_x * TILE_SIZE.width + TILE_SIZE.width / 2, target_tile_y * TILE_SIZE.height + TILE_SIZE.height / 2));
3438
}
3539
}
3640
Vec2 Entity::tileCoordFromPosition(Vec2 position)

BombingAdventure/Classes/GameScene.cpp

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,12 @@ bool GameScene::init() {
157157

158158
/* Initialize an ItemController */
159159
ItemController * item_controller = ItemController::create();
160-
this->addChild(item_controller,498);
160+
item_controller->bind_player(hero);
161+
this->addChild(item_controller);
162+
163+
/* If uncomment the following statement, items will be hid under the bricks */
164+
// item_controller->setGlobalZOrder(0);
165+
monster_controller->setGlobalZOrder(99);
161166
return true;
162167
}
163168

@@ -183,15 +188,15 @@ void GameScene::update(float delta) {
183188
}
184189

185190
/* Test pick_item method */
186-
Item * speed_up_item = Item::create();
187-
speed_up_item->setPosition(Vec2(500, 380));
188-
this->addChild(speed_up_item);
189-
190-
auto item_position = tileCoordFromPosition(speed_up_item->getPosition());
191-
auto hero_position = tileCoordFromPosition(hero->getPosition());
192-
if (item_position == hero_position) {
193-
hero->pick_item(*speed_up_item);
194-
}
191+
// Item * speed_up_item = Item::create();
192+
// speed_up_item->setPosition(Vec2(500, 380));
193+
// this->addChild(speed_up_item);
194+
//
195+
// auto item_position = tileCoordFromPosition(speed_up_item->getPosition());
196+
// auto hero_position = tileCoordFromPosition(hero->getPosition());
197+
// if (item_position == hero_position) {
198+
// hero->pick_item(*speed_up_item);
199+
// }
195200
}
196201

197202
void GameScene::bomb_explode(Bomb *bomb)

BombingAdventure/Classes/Item.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,36 @@
33
//
44

55
#include "Item.h"
6+
#include "Player.h"
67

78
Item::Item() {
89
/* Default attributes of a new item */
910
is_picked = false;
10-
// item_id = floor(CCRANDOM_0_1() * sizeof(items));
11+
item_id = (int) floor(CCRANDOM_0_1() * sizeof(items));
1112
item_id = 1;
1213
/* Default view of a new item */
1314
bind_sprite(Sprite::create("Item_1.png"));
1415
this->setAnchorPoint(Vec2(0.5,0.5));
16+
this->setGlobalZOrder(0);
1517
}
1618
Item::~Item() {
1719
/* Not yet */
1820
}
1921
bool Item::init() {
2022
/* Enable update */
21-
this->scheduleUpdate();
23+
// this->scheduleUpdate();
2224
return true;
2325
}
2426
int Item::get_item_id() {
2527
return item_id;
2628
}
27-
void Item::update(float delta) {
28-
if (get_sprite() == NULL) {
29-
return;
30-
}
31-
if (is_picked) {
32-
this->removeAllChildren();
33-
this->removeFromParent();
34-
log("Item is removed");
35-
}
36-
}
29+
//void Item::update(float delta) {
30+
// if (get_sprite() == NULL) {
31+
// return;
32+
// }
33+
// if (is_picked) {
34+
// this->removeAllChildren();
35+
// this->removeFromParent();
36+
// log("Item is removed");
37+
// }
38+
//}

BombingAdventure/Classes/Item.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ USING_NS_CC;
1313
/* Following are the attributes of items */
1414
const int ITEM_CURE_BY = 1;
1515
const int ITEM_ADD_BOMBS_BY = 1;
16-
const float ITEM_SPEED_UP_BY = 0.5;
16+
const float ITEM_SPEED_UP_BY = 2.f;
1717

1818
class Item : public Entity {
1919
public:
@@ -29,7 +29,7 @@ class Item : public Entity {
2929
virtual bool init();
3030

3131
/* Override update method */
32-
virtual void update(float delta);
32+
// virtual void update(float delta);
3333

3434
int get_item_id();
3535

BombingAdventure/Classes/ItemController.cpp

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,26 @@
66

77
bool ItemController::init() {
88
/* 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);
9+
// timeval now;
10+
// gettimeofday(&now, NULL);
11+
// auto seed = (unsigned) (now.tv_sec * 1000 + now.tv_usec / 1000);
12+
// srand(seed);
1313

1414
create_item();
1515
/* Enable update */
1616
this->scheduleUpdate();
1717
return true;
1818
}
1919
void ItemController::update(float delta) {
20-
20+
for (Item * item : current_item_vector) {
21+
if (is_picked(item)) {
22+
monitored_player->pick_item(*item);
23+
item->removeAllChildren();
24+
item->removeFromParent();
25+
current_item_vector.eraseObject(item);
26+
log("Item is removed.");
27+
}
28+
}
2129
}
2230
void ItemController::create_item() {
2331
for (int i = 0; i < NUM_ITEMS; i++) {
@@ -30,4 +38,15 @@ void ItemController::create_item() {
3038
this->addChild(item);
3139
current_item_vector.pushBack(item);
3240
}
41+
}
42+
void ItemController::bind_player(Player *player) {
43+
monitored_player = player;
44+
}
45+
bool ItemController::is_picked(Item *item) {
46+
/* Get the bounding box of the player */
47+
Rect player_bounding_box = monitored_player->getBoundingBox();
48+
Vec2 item_position = item->getPosition();
49+
/* Check whether the player and the item has intersection */
50+
item->is_picked = player_bounding_box.containsPoint(item_position);
51+
return item->is_picked;
3352
}

BombingAdventure/Classes/ItemController.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include "cocos2d.h"
99
#include "Item.h"
10+
#include "Player.h"
1011

1112
USING_NS_CC;
1213

@@ -21,6 +22,25 @@ class ItemController : public Node {
2122

2223
/* Override update method */
2324
virtual void update(float delta);
25+
26+
/* Method: bind_player
27+
* ----------------------
28+
* Bind the player to item controller, which
29+
* enables it to monitor the operation of
30+
* this player.
31+
*/
32+
void bind_player(Player * player);
33+
34+
/* Method: is_picked
35+
* Usage: if (is_picked(item)) do ...
36+
* ---------------------------------------
37+
* Returns true if the bounding box of the player
38+
* intersects with the position of this item. i.e.
39+
* The item is picked by the monitored player.
40+
* It will also set the status of item to is_picked = true.
41+
*/
42+
bool is_picked(Item * item);
43+
2444
private:
2545
/* Method: create_item
2646
* ----------------------
@@ -30,6 +50,8 @@ class ItemController : public Node {
3050

3151
/* Store all the Items */
3252
Vector<Item*> current_item_vector;
53+
54+
Player * monitored_player = NULL;
3355
};
3456

3557
#endif //BOMBING_ADVENTURE_ITEMCONTROLLER_H

BombingAdventure/Classes/Monster.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Monster::Monster() {
1515

1616
/* Default view of a new monster */
1717
bind_sprite(Sprite::create("stone_monster.png"));
18-
this->setAnchorPoint(Vec2(0.5,0.5));
18+
this->setAnchorPoint(Vec2(0.35, 0.25));
1919
}
2020
Monster::~Monster() {
2121
/* Not yet */

0 commit comments

Comments
 (0)