-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathPopsicleScene.cpp
192 lines (157 loc) · 6.29 KB
/
PopsicleScene.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <iomanip>
#include "PopsicleScene.h"
#include "GroundManager.h"
#include "Config.h"
#include "EnemyManager.h"
#include "GameOverMenu.h"
USING_NS_CC;
cocos2d::Scene *PopsicleScene::createScene() {
return PopsicleScene::create();
}
bool PopsicleScene::init() {
auto physicsCreated = Scene::initWithPhysics();
if (!physicsCreated) {
return false;
}
// setup physics
auto physicsWorld = this->getPhysicsWorld();
#ifdef DEBUG_PHYSICS
physicsWorld->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);
#endif
physicsWorld->setGravity(Vec2(0, Config::kGravity));
auto visibleSize = _director->getVisibleSize();
// load textures
auto textureCache = _director->getTextureCache();
auto playerTexture = textureCache->addImage("purple.png");
auto groundTexture = textureCache->addImage("grass-middle.png");
auto sunTexture = textureCache->addImage("sun.png");
auto crabTexture = textureCache->addImage("crab.png");
// create the player
_player = PopsiclePlayer::createWithTextureAndGameOverListener(playerTexture, std::bind(
&PopsicleScene::gameOver, this));
_player->retain();
_player->setPosition(Vec2(0.f, 0.f));
this->addChild(_player);
// configure the camera
auto camera = this->getDefaultCamera();
camera->setBackgroundBrush(
CameraBackgroundBrush::createColorBrush(Color4F(Config::kCornflowerBlue), 1.f));
_cameraNode = Node::create();
_cameraNode->retain();
addChild(_cameraNode);
camera->setParent(_cameraNode);
camera->setPosition(Vec2(0, 0));
// add a happy little sun
auto sunSprite = Sprite::createWithTexture(sunTexture);
sunSprite->setAnchorPoint(Vec2(1, 1));
sunSprite->setPosition(visibleSize / 2);
_cameraNode->addChild(sunSprite);
// setup the ground
auto groundManager = GroundManager::createWithCameraAndGroundTexture(camera, groundTexture);
addChild(groundManager);
// offset the camera by the ground height
_cameraOffset = Vec2(0, -groundManager->getGroundHeight());
centerCamera(camera, visibleSize);
// setup enemy spawning
EnemyManager::Config enemyManagerConfig = {
.enemyTexture = crabTexture,
.spawnChance = Config::kEnemySpawnChance,
.spawnDistance = Config::kEnemySpawnDensity * crabTexture->getContentSize().width,
.camera = camera
};
auto enemyManager = EnemyManager::createWithConfig(enemyManagerConfig);
addChild(enemyManager);
// setup collision handling
auto eventDispatcher = _director->getEventDispatcher();
_physicsEventListener = EventListenerPhysicsContact::create();
_physicsEventListener->retain();
_physicsEventListener->onContactBegin = CC_CALLBACK_1(PopsicleScene::handleCollision, this);
eventDispatcher->addEventListenerWithSceneGraphPriority(_physicsEventListener, this);
// create a label to show how far a player's gone
_distanceLabel = Label::createWithTTF("Distance: 0", Config::kMenuFont, 24);
_distanceLabel->retain();
_distanceLabel->setAnchorPoint(Vec2(0, 1));
_distanceLabel->setPosition(
Vec2(-visibleSize.width / 2, visibleSize.height / 2) + Config::kDistanceLabelOffset);
_cameraNode->addChild(_distanceLabel);
// set the start distance for the camera
_cameraStartDistance = _cameraNode->getPosition().x;
// start running the level
scheduleUpdate();
return true;
}
void PopsicleScene::update(float delta) {
auto camera = getDefaultCamera();
auto director = Director::getInstance();
centerCamera(camera, director->getVisibleSize());
updateDistanceLabel();
Node::update(delta);
}
void PopsicleScene::cleanup() {
CC_SAFE_RELEASE(_player);
CC_SAFE_RELEASE(_cameraNode);
CC_SAFE_RELEASE(_distanceLabel);
if (_physicsEventListener) {
auto eventDispatcher = _director->getEventDispatcher();
eventDispatcher->removeEventListener(_physicsEventListener);
_physicsEventListener->release();
_physicsEventListener = nullptr;
}
Scene::cleanup();
}
void PopsicleScene::centerCamera(Camera *camera, const Size &visibleSize) {
Vec2 cameraPosition;
cameraPosition.x += _player->getPositionX() + visibleSize.width / 4;
cameraPosition.y += visibleSize.height / 2;
cameraPosition += _cameraOffset;
_cameraNode->setPosition(cameraPosition);
}
// cocos uses a signed in, so disable related warnings
#pragma clang diagnostic push
#pragma ide diagnostic ignored "hicpp-signed-bitwise"
bool PopsicleScene::handleCollision(PhysicsContact &contact) {
int collisionCombined =
contact.getShapeA()->getCategoryBitmask() | contact.getShapeB()->getCategoryBitmask();
if (collisionCombined == (Config::kPlayerCollisionCategory | Config::kEnemyCollisionCategory)) {
// something tagged as the player collided with something tagged as an enemy
_player->collidedWithEnemy();
}
return true;
}
void PopsicleScene::gameOver() {
// handle a game over
if (!_gameOver) {
auto gameOverMenu = GameOverMenu::create();
_cameraNode->addChild(gameOverMenu);
gameOverMenu->setPosition(Vec2::ZERO);
// TODO: log kEventLevelEnd here!
_gameOver = true;
}
}
void PopsicleScene::updateDistanceLabel() {
float distance = (_cameraNode->getPosition().x - _cameraStartDistance) * Config::kDistanceScale;
if (distance > _lastDistance) {
std::stringstream distanceString;
distanceString << "Distance: " << std::fixed << std::setprecision(2) << distance;
_distanceLabel->setString(distanceString.str());
_lastDistance = distance;
}
}
void PopsicleScene::onEnter() {
// TODO: log kEventLevelStart here
Node::onEnter();
}
#pragma clang diagnostic pop