Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(272)

Side by Side Diff: nacltoons/src/level_layer.cc

Issue 12579005: [nacltoons] Rename core classes in accordance with doc. (Closed) Base URL: https://nativeclient-sdk.googlecode.com/svn/trunk/src
Patch Set: Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « nacltoons/src/level_layer.h ('k') | nacltoons/src/physics_layer.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "physics_layer.h" 5 #include "level_layer.h"
6 #include "app_delegate.h" 6 #include "app_delegate.h"
7 #include "gameplay_scene.h" 7 #include "game_manager.h"
8 8
9 #include "physics_nodes/CCPhysicsSprite.h" 9 #include "physics_nodes/CCPhysicsSprite.h"
10 #include "CCLuaEngine.h" 10 #include "CCLuaEngine.h"
11 11
12 extern "C" { 12 extern "C" {
13 #include "lua.h" 13 #include "lua.h"
14 #include "tolua++.h" 14 #include "tolua++.h"
15 #include "lualib.h" 15 #include "lualib.h"
16 #include "lauxlib.h" 16 #include "lauxlib.h"
17 #include "tolua_fix.h" 17 #include "tolua_fix.h"
18 } 18 }
19 19
20 // Pixels-to-meters ratio for converting screen coordinates 20 // Pixels-to-meters ratio for converting screen coordinates
21 // to Box2D "meters". 21 // to Box2D "meters".
22 #define PTM_RATIO 32 22 #define PTM_RATIO 32
23 #define SCREEN_TO_WORLD(n) ((n) / PTM_RATIO) 23 #define SCREEN_TO_WORLD(n) ((n) / PTM_RATIO)
24 #define WORLD_TO_SCREEN(n) ((n) * PTM_RATIO) 24 #define WORLD_TO_SCREEN(n) ((n) * PTM_RATIO)
25 #define VELOCITY_ITERATIONS 8 25 #define VELOCITY_ITERATIONS 8
26 #define POS_ITERATIONS 1 26 #define POS_ITERATIONS 1
27 27
28 #define MAX_SPRITES 100 28 #define MAX_SPRITES 100
29 29
30 #define DEFAULT_DENSITY 1.0f 30 #define DEFAULT_DENSITY 1.0f
31 #define DEFAULT_FRICTION 0.2f 31 #define DEFAULT_FRICTION 0.2f
32 #define DEFAULT_RESTITUTION 0.1f 32 #define DEFAULT_RESTITUTION 0.1f
33 33
34 34
35 USING_NS_CC_EXT; 35 USING_NS_CC_EXT;
36 36
37 PhysicsLayer* PhysicsLayer::create(int level_number) 37 LevelLayer* LevelLayer::create(int level_number)
38 { 38 {
39 PhysicsLayer* layer = new PhysicsLayer(level_number); 39 LevelLayer* layer = new LevelLayer(level_number);
40 if (!layer) 40 if (!layer)
41 return NULL; 41 return NULL;
42 layer->init(); 42 layer->init();
43 return layer; 43 return layer;
44 } 44 }
45 45
46 bool PhysicsLayer::init() { 46 bool LevelLayer::init() {
47 if (!CCLayerColor::initWithColor(ccc4(0,0x8F,0xD8,0xD8))) 47 if (!CCLayerColor::initWithColor(ccc4(0,0x8F,0xD8,0xD8)))
48 return false; 48 return false;
49 49
50 setTouchEnabled(true); 50 setTouchEnabled(true);
51 51
52 InitPhysics(); 52 InitPhysics();
53 53
54 // Load level from lua file. For now we simple load level 1. 54 // Load level from lua file. For now we simple load level 1.
55 LoadLua(); 55 LoadLua();
56 56
57 // Calculate brush size 57 // Calculate brush size
58 CCSpriteBatchNode* brush_batch = (CCSpriteBatchNode*)getChildByTag(TAG_BRUSH); 58 CCSpriteBatchNode* brush_batch = (CCSpriteBatchNode*)getChildByTag(TAG_BRUSH);
59 assert(brush_batch); 59 assert(brush_batch);
60 brush_ = CCSprite::createWithTexture(brush_batch->getTexture()); 60 brush_ = CCSprite::createWithTexture(brush_batch->getTexture());
61 brush_->retain(); 61 brush_->retain();
62 CCSize brush_size = brush_->getContentSize(); 62 CCSize brush_size = brush_->getContentSize();
63 brush_radius_ = MAX(brush_size.height/2, brush_size.width/2); 63 brush_radius_ = MAX(brush_size.height/2, brush_size.width/2);
64 64
65 // Schedule physics updates each frame 65 // Schedule physics updates each frame
66 schedule(schedule_selector(PhysicsLayer::UpdateWorld)); 66 schedule(schedule_selector(LevelLayer::UpdateWorld));
67 return true; 67 return true;
68 } 68 }
69 69
70 PhysicsLayer::PhysicsLayer(int level_number) : 70 LevelLayer::LevelLayer(int level_number) :
71 level_number_(level_number), 71 level_number_(level_number),
72 goal_reached_(false), 72 goal_reached_(false),
73 current_touch_id_(-1), 73 current_touch_id_(-1),
74 render_target_(NULL), 74 render_target_(NULL),
75 #ifdef COCOS2D_DEBUG 75 #ifdef COCOS2D_DEBUG
76 debug_enabled_(false), 76 debug_enabled_(false),
77 #endif 77 #endif
78 box2d_density_(DEFAULT_DENSITY), 78 box2d_density_(DEFAULT_DENSITY),
79 box2d_restitution_(DEFAULT_RESTITUTION), 79 box2d_restitution_(DEFAULT_RESTITUTION),
80 box2d_friction_(DEFAULT_FRICTION) { 80 box2d_friction_(DEFAULT_FRICTION) {
81 memset(stars_collected_, 0, sizeof(stars_collected_)); 81 memset(stars_collected_, 0, sizeof(stars_collected_));
82 } 82 }
83 83
84 PhysicsLayer::~PhysicsLayer() { 84 LevelLayer::~LevelLayer() {
85 brush_->release(); 85 brush_->release();
86 delete box2d_world_; 86 delete box2d_world_;
87 #ifdef COCOS2D_DEBUG 87 #ifdef COCOS2D_DEBUG
88 delete box2d_debug_draw_; 88 delete box2d_debug_draw_;
89 #endif 89 #endif
90 } 90 }
91 91
92 void PhysicsLayer::registerWithTouchDispatcher() { 92 void LevelLayer::registerWithTouchDispatcher() {
93 CCDirector* director = CCDirector::sharedDirector(); 93 CCDirector* director = CCDirector::sharedDirector();
94 director->getTouchDispatcher()->addTargetedDelegate(this, 0, true); 94 director->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
95 } 95 }
96 96
97 void PhysicsLayer::CreateRenderTarget() { 97 void LevelLayer::CreateRenderTarget() {
98 // create render target for shape drawing 98 // create render target for shape drawing
99 assert(!render_target_); 99 assert(!render_target_);
100 CCSize win_size = CCDirector::sharedDirector()->getWinSize(); 100 CCSize win_size = CCDirector::sharedDirector()->getWinSize();
101 render_target_ = CCRenderTexture::create(win_size.width, 101 render_target_ = CCRenderTexture::create(win_size.width,
102 win_size.height, 102 win_size.height,
103 kCCTexture2DPixelFormat_RGBA8888); 103 kCCTexture2DPixelFormat_RGBA8888);
104 render_target_->setPosition(ccp(win_size.width / 2, win_size.height / 2)); 104 render_target_->setPosition(ccp(win_size.width / 2, win_size.height / 2));
105 addChild(render_target_); 105 addChild(render_target_);
106 } 106 }
107 107
108 bool PhysicsLayer::LoadLua() { 108 bool LevelLayer::LoadLua() {
109 CCScriptEngineManager* manager = CCScriptEngineManager::sharedManager(); 109 CCScriptEngineManager* manager = CCScriptEngineManager::sharedManager();
110 CCLuaEngine* engine = (CCLuaEngine*)manager->getScriptEngine(); 110 CCLuaEngine* engine = (CCLuaEngine*)manager->getScriptEngine();
111 assert(engine); 111 assert(engine);
112 lua_stack_ = engine->getLuaStack(); 112 lua_stack_ = engine->getLuaStack();
113 assert(lua_stack_); 113 assert(lua_stack_);
114 114
115 lua_stack_->pushString("sample_game/game.lua"); 115 lua_stack_->pushString("sample_game/game.lua");
116 lua_stack_->pushCCObject(this, "PhysicsLayer"); 116 lua_stack_->pushCCObject(this, "LevelLayer");
117 lua_stack_->pushInt(level_number_); 117 lua_stack_->pushInt(level_number_);
118 118
119 // Call 'main' with three arguments pushed above 119 // Call 'main' with three arguments pushed above
120 int rtn = lua_stack_->executeFunctionByName("main", 3); 120 int rtn = lua_stack_->executeFunctionByName("main", 3);
121 if (rtn != 1) 121 if (rtn != 1)
122 return false; 122 return false;
123 123
124 return true; 124 return true;
125 } 125 }
126 126
127 bool PhysicsLayer::InitPhysics() { 127 bool LevelLayer::InitPhysics() {
128 b2Vec2 gravity(0.0f, -9.8f); 128 b2Vec2 gravity(0.0f, -9.8f);
129 box2d_world_ = new b2World(gravity); 129 box2d_world_ = new b2World(gravity);
130 box2d_world_->SetAllowSleeping(true); 130 box2d_world_->SetAllowSleeping(true);
131 box2d_world_->SetContinuousPhysics(true); 131 box2d_world_->SetContinuousPhysics(true);
132 132
133 // Find visible rect, and convert to box2d space. 133 // Find visible rect, and convert to box2d space.
134 CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); 134 CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
135 CCSize visible_size = CCDirector::sharedDirector()->getVisibleSize(); 135 CCSize visible_size = CCDirector::sharedDirector()->getVisibleSize();
136 float world_width = SCREEN_TO_WORLD(visible_size.width); 136 float world_width = SCREEN_TO_WORLD(visible_size.width);
137 b2Vec2 world_origin(SCREEN_TO_WORLD(origin.x), SCREEN_TO_WORLD(origin.y)); 137 b2Vec2 world_origin(SCREEN_TO_WORLD(origin.x), SCREEN_TO_WORLD(origin.y));
(...skipping 22 matching lines...) Expand all
160 flags += b2Draw::e_shapeBit; 160 flags += b2Draw::e_shapeBit;
161 flags += b2Draw::e_jointBit; 161 flags += b2Draw::e_jointBit;
162 flags += b2Draw::e_centerOfMassBit; 162 flags += b2Draw::e_centerOfMassBit;
163 //flags += b2Draw::e_aabbBit; 163 //flags += b2Draw::e_aabbBit;
164 //flags += b2Draw::e_pairBit; 164 //flags += b2Draw::e_pairBit;
165 box2d_debug_draw_->SetFlags(flags); 165 box2d_debug_draw_->SetFlags(flags);
166 #endif 166 #endif
167 return true; 167 return true;
168 } 168 }
169 169
170 void PhysicsLayer::ToggleDebug() { 170 void LevelLayer::ToggleDebug() {
171 debug_enabled_ = !debug_enabled_; 171 debug_enabled_ = !debug_enabled_;
172 172
173 // Set visibility of all children based on debug_enabled_ 173 // Set visibility of all children based on debug_enabled_
174 CCArray* children = getChildren(); 174 CCArray* children = getChildren();
175 if (!children) 175 if (!children)
176 return; 176 return;
177 for (uint i = 0; i < children->count(); i++) 177 for (uint i = 0; i < children->count(); i++)
178 { 178 {
179 CCNode* child = static_cast<CCNode*>(children->objectAtIndex(i)); 179 CCNode* child = static_cast<CCNode*>(children->objectAtIndex(i));
180 if (child == render_target_) 180 if (child == render_target_)
181 continue; 181 continue;
182 child->setVisible(!debug_enabled_); 182 child->setVisible(!debug_enabled_);
183 } 183 }
184 } 184 }
185 185
186 CCRect CalcBoundingBox(CCSprite* sprite) { 186 CCRect CalcBoundingBox(CCSprite* sprite) {
187 CCSize size = sprite->getContentSize(); 187 CCSize size = sprite->getContentSize();
188 CCPoint pos = sprite->getPosition(); 188 CCPoint pos = sprite->getPosition();
189 return CCRectMake(pos.x - size.width, pos.y - size.height, 189 return CCRectMake(pos.x - size.width, pos.y - size.height,
190 size.width, size.height/2); 190 size.width, size.height/2);
191 } 191 }
192 192
193 void PhysicsLayer::UpdateWorld(float dt) { 193 void LevelLayer::UpdateWorld(float dt) {
194 // update physics 194 // update physics
195 box2d_world_->Step(dt, VELOCITY_ITERATIONS, POS_ITERATIONS); 195 box2d_world_->Step(dt, VELOCITY_ITERATIONS, POS_ITERATIONS);
196 } 196 }
197 197
198 void PhysicsLayer::LuaNotifyContact(b2Contact* contact, 198 void LevelLayer::LuaNotifyContact(b2Contact* contact,
199 const char* function_name) { 199 const char* function_name) {
200 // Return early if lua didn't define the function_name 200 // Return early if lua didn't define the function_name
201 lua_State* state = lua_stack_->getLuaState(); 201 lua_State* state = lua_stack_->getLuaState();
202 lua_getglobal(state, function_name); 202 lua_getglobal(state, function_name);
203 bool is_func = lua_isfunction(state, -1); 203 bool is_func = lua_isfunction(state, -1);
204 lua_pop(state, 1); 204 lua_pop(state, 1);
205 205
206 if (!is_func) 206 if (!is_func)
207 return; 207 return;
208 208
209 // Only send to lua collitions between body's that 209 // Only send to lua collitions between body's that
210 // have been tagged. 210 // have been tagged.
211 b2Body* body1 = contact->GetFixtureA()->GetBody(); 211 b2Body* body1 = contact->GetFixtureA()->GetBody();
212 b2Body* body2 = contact->GetFixtureB()->GetBody(); 212 b2Body* body2 = contact->GetFixtureB()->GetBody();
213 int tag1 = (int)body1->GetUserData(); 213 int tag1 = (int)body1->GetUserData();
214 int tag2 = (int)body2->GetUserData(); 214 int tag2 = (int)body2->GetUserData();
215 if (!tag1 || !tag2) 215 if (!tag1 || !tag2)
216 return; 216 return;
217 217
218 // Call 'ContactBegan' lua function passing in 'this' 218 // Call 'ContactBegan' lua function passing in 'this'
219 // as well as the tags of the two bodies that collided 219 // as well as the tags of the two bodies that collided
220 lua_stack_->pushCCObject(this, "PhysicsLayer"); 220 lua_stack_->pushCCObject(this, "LevelLayer");
221 lua_stack_->pushInt(tag1); 221 lua_stack_->pushInt(tag1);
222 lua_stack_->pushInt(tag2); 222 lua_stack_->pushInt(tag2);
223 lua_stack_->executeFunctionByName(function_name, 3); 223 lua_stack_->executeFunctionByName(function_name, 3);
224 } 224 }
225 225
226 void PhysicsLayer::BeginContact(b2Contact* contact) { 226 void LevelLayer::BeginContact(b2Contact* contact) {
227 LuaNotifyContact(contact, "BeginContact"); 227 LuaNotifyContact(contact, "BeginContact");
228 } 228 }
229 229
230 void PhysicsLayer::EndContact(b2Contact* contact) { 230 void LevelLayer::EndContact(b2Contact* contact) {
231 LuaNotifyContact(contact, "EndContact"); 231 LuaNotifyContact(contact, "EndContact");
232 } 232 }
233 233
234 void PhysicsLayer::LevelComplete() { 234 void LevelLayer::LevelComplete() {
235 // fade out the goal and trigger gameover callback when its 235 // fade out the goal and trigger gameover callback when its
236 // done 236 // done
237 CCPhysicsSprite* goal = (CCPhysicsSprite*)getChildByTag(TAG_GOAL); 237 CCPhysicsSprite* goal = (CCPhysicsSprite*)getChildByTag(TAG_GOAL);
238 CCActionInterval* fadeout = CCFadeOut::create(0.5f); 238 CCActionInterval* fadeout = CCFadeOut::create(0.5f);
239 CCFiniteTimeAction* fadeout_done = CCCallFuncN::create(this, 239 CCFiniteTimeAction* fadeout_done = CCCallFuncN::create(this,
240 callfuncN_selector(PhysicsLayer::LevelCompleteDone)); 240 callfuncN_selector(LevelLayer::LevelCompleteDone));
241 CCSequence* seq = CCSequence::create(fadeout, fadeout_done, NULL); 241 CCSequence* seq = CCSequence::create(fadeout, fadeout_done, NULL);
242 goal->runAction(seq); 242 goal->runAction(seq);
243 } 243 }
244 244
245 void PhysicsLayer::LevelCompleteDone(CCNode* sender) { 245 void LevelLayer::LevelCompleteDone(CCNode* sender) {
246 unschedule(schedule_selector(PhysicsLayer::UpdateWorld)); 246 unschedule(schedule_selector(LevelLayer::UpdateWorld));
247 setTouchEnabled(false); 247 setTouchEnabled(false);
248 GameplayScene* scene = static_cast<GameplayScene*>(getParent()); 248 CCScene* scene = static_cast<CCScene*>(getParent());
249 scene->GameOver(true); 249 GameManager::sharedManager()->GameOver(scene, true);
250 } 250 }
251 251
252 void PhysicsLayer::DrawPoint(CCPoint& location) { 252 void LevelLayer::DrawPoint(CCPoint& location) {
253 ClampBrushLocation(location); 253 ClampBrushLocation(location);
254 render_target_->begin(); 254 render_target_->begin();
255 brush_->setVisible(true); 255 brush_->setVisible(true);
256 brush_->setPosition(ccp(location.x, location.y)); 256 brush_->setPosition(ccp(location.x, location.y));
257 brush_->visit(); 257 brush_->visit();
258 brush_->setVisible(false); 258 brush_->setVisible(false);
259 render_target_->end(); 259 render_target_->end();
260 points_being_drawn_.push_back(location); 260 points_being_drawn_.push_back(location);
261 } 261 }
262 262
263 void PhysicsLayer::draw() { 263 void LevelLayer::draw() {
264 CCLayerColor::draw(); 264 CCLayerColor::draw();
265 265
266 #ifdef COCOS2D_DEBUG 266 #ifdef COCOS2D_DEBUG
267 if (debug_enabled_) { 267 if (debug_enabled_) {
268 ccGLEnableVertexAttribs(kCCVertexAttribFlag_Position); 268 ccGLEnableVertexAttribs(kCCVertexAttribFlag_Position);
269 kmGLPushMatrix(); 269 kmGLPushMatrix();
270 box2d_world_->DrawDebugData(); 270 box2d_world_->DrawDebugData();
271 kmGLPopMatrix(); 271 kmGLPopMatrix();
272 } 272 }
273 #endif 273 #endif
274 } 274 }
275 275
276 void PhysicsLayer::ClampBrushLocation(CCPoint& point) { 276 void LevelLayer::ClampBrushLocation(CCPoint& point) {
277 CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); 277 CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
278 CCSize visible_size = CCDirector::sharedDirector()->getVisibleSize(); 278 CCSize visible_size = CCDirector::sharedDirector()->getVisibleSize();
279 279
280 float min_x = origin.x + brush_radius_; 280 float min_x = origin.x + brush_radius_;
281 float min_y = origin.y + brush_radius_; 281 float min_y = origin.y + brush_radius_;
282 if (point.x < min_x) point.x = min_x; 282 if (point.x < min_x) point.x = min_x;
283 if (point.y < min_y) point.y = min_y; 283 if (point.y < min_y) point.y = min_y;
284 284
285 float max_x = origin.x + visible_size.width - brush_radius_; 285 float max_x = origin.x + visible_size.width - brush_radius_;
286 float max_y = origin.y + visible_size.height - brush_radius_; 286 float max_y = origin.y + visible_size.height - brush_radius_;
287 if (point.x > max_x) point.x = max_x; 287 if (point.x > max_x) point.x = max_x;
288 if (point.y > max_y) point.y = max_y; 288 if (point.y > max_y) point.y = max_y;
289 } 289 }
290 290
291 void PhysicsLayer::DrawLine(CCPoint& start, CCPoint& end) { 291 void LevelLayer::DrawLine(CCPoint& start, CCPoint& end) {
292 ClampBrushLocation(start); 292 ClampBrushLocation(start);
293 ClampBrushLocation(end); 293 ClampBrushLocation(end);
294 294
295 // calculate distance moved 295 // calculate distance moved
296 float distance = ccpDistance(start, end); 296 float distance = ccpDistance(start, end);
297 297
298 // draw the brush sprite into render texture at every point between the old 298 // draw the brush sprite into render texture at every point between the old
299 // and new cursor positions 299 // and new cursor positions
300 render_target_->begin(); 300 render_target_->begin();
301 for (int i = 0; i < int(distance + 0.5); i++) { 301 for (int i = 0; i < int(distance + 0.5); i++) {
302 float difx = end.x - start.x; 302 float difx = end.x - start.x;
303 float dify = end.y - start.y; 303 float dify = end.y - start.y;
304 float delta = (float)i / distance; 304 float delta = (float)i / distance;
305 brush_->setVisible(true); 305 brush_->setVisible(true);
306 brush_->setPosition( 306 brush_->setPosition(
307 ccp(start.x + (difx * delta), start.y + (dify * delta))); 307 ccp(start.x + (difx * delta), start.y + (dify * delta)));
308 308
309 brush_->visit(); 309 brush_->visit();
310 brush_->setVisible(false); 310 brush_->setVisible(false);
311 } 311 }
312 render_target_->end(); 312 render_target_->end();
313 points_being_drawn_.push_back(end); 313 points_being_drawn_.push_back(end);
314 } 314 }
315 315
316 bool PhysicsLayer::ccTouchBegan(CCTouch* touch, CCEvent* event) { 316 bool LevelLayer::ccTouchBegan(CCTouch* touch, CCEvent* event) {
317 if (current_touch_id_ != -1) 317 if (current_touch_id_ != -1)
318 return false; 318 return false;
319 319
320 current_touch_id_ = touch->getID(); 320 current_touch_id_ = touch->getID();
321 321
322 if (!render_target_) 322 if (!render_target_)
323 CreateRenderTarget(); 323 CreateRenderTarget();
324 324
325 points_being_drawn_.clear(); 325 points_being_drawn_.clear();
326 CCPoint location = touch->getLocation(); 326 CCPoint location = touch->getLocation();
327 DrawPoint(location); 327 DrawPoint(location);
328 return true; 328 return true;
329 } 329 }
330 330
331 void PhysicsLayer::ccTouchMoved(CCTouch* touch, CCEvent* event) { 331 void LevelLayer::ccTouchMoved(CCTouch* touch, CCEvent* event) {
332 assert(touch->getID() == current_touch_id_); 332 assert(touch->getID() == current_touch_id_);
333 CCPoint end = touch->getLocation(); 333 CCPoint end = touch->getLocation();
334 CCPoint start = touch->getPreviousLocation(); 334 CCPoint start = touch->getPreviousLocation();
335 DrawLine(start, end); 335 DrawLine(start, end);
336 } 336 }
337 337
338 void PhysicsLayer::ccTouchEnded(CCTouch* touch, CCEvent* event) { 338 void LevelLayer::ccTouchEnded(CCTouch* touch, CCEvent* event) {
339 assert(touch->getID() == current_touch_id_); 339 assert(touch->getID() == current_touch_id_);
340 b2Body* body = CreatePhysicsBody(); 340 b2Body* body = CreatePhysicsBody();
341 CCSprite* sprite = CreatePhysicsSprite(body); 341 CCSprite* sprite = CreatePhysicsSprite(body);
342 addChild(sprite); 342 addChild(sprite);
343 if (debug_enabled_) 343 if (debug_enabled_)
344 sprite->setVisible(false); 344 sprite->setVisible(false);
345 345
346 // release render target (it will get recreated on next touch). 346 // release render target (it will get recreated on next touch).
347 removeChild(render_target_, true); 347 removeChild(render_target_, true);
348 render_target_ = NULL; 348 render_target_ = NULL;
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
394 minX = WORLD_TO_SCREEN(minX); 394 minX = WORLD_TO_SCREEN(minX);
395 maxY = WORLD_TO_SCREEN(maxY); 395 maxY = WORLD_TO_SCREEN(maxY);
396 minY = WORLD_TO_SCREEN(minY); 396 minY = WORLD_TO_SCREEN(minY);
397 397
398 float width = maxX - minX; 398 float width = maxX - minX;
399 float height = maxY - minY; 399 float height = maxY - minY;
400 float remY = s.height - maxY; 400 float remY = s.height - maxY;
401 return CCRectMake(minX, remY, width, height); 401 return CCRectMake(minX, remY, width, height);
402 } 402 }
403 403
404 CCSprite* PhysicsLayer::CreatePhysicsSprite(b2Body* body) { 404 CCSprite* LevelLayer::CreatePhysicsSprite(b2Body* body) {
405 CCPhysicsSprite *sprite; 405 CCPhysicsSprite *sprite;
406 406
407 // create a new texture based on the current contents of the 407 // create a new texture based on the current contents of the
408 // render target 408 // render target
409 CCImage* image = render_target_->newCCImage(); 409 CCImage* image = render_target_->newCCImage();
410 CCTexture2D* tex = new CCTexture2D(); 410 CCTexture2D* tex = new CCTexture2D();
411 tex->initWithImage(image); 411 tex->initWithImage(image);
412 tex->autorelease(); 412 tex->autorelease();
413 delete image; 413 delete image;
414 414
(...skipping 18 matching lines...) Expand all
433 float anchorX = body_pos.x - sprite_rect.origin.x; 433 float anchorX = body_pos.x - sprite_rect.origin.x;
434 float anchorY = body_pos.y + sprite_rect.origin.y + sprite_rect.size.height; 434 float anchorY = body_pos.y + sprite_rect.origin.y + sprite_rect.size.height;
435 anchorY -= s.height; 435 anchorY -= s.height;
436 436
437 // anchor point goes from 0.0 to 1.0 with in bounds of the sprite itself. 437 // anchor point goes from 0.0 to 1.0 with in bounds of the sprite itself.
438 sprite->setAnchorPoint(ccp(anchorX / sprite_rect.size.width, 438 sprite->setAnchorPoint(ccp(anchorX / sprite_rect.size.width,
439 anchorY / sprite_rect.size.height)); 439 anchorY / sprite_rect.size.height));
440 return sprite; 440 return sprite;
441 } 441 }
442 442
443 b2Body* PhysicsLayer::CreatePhysicsBody() { 443 b2Body* LevelLayer::CreatePhysicsBody() {
444 assert(points_being_drawn_.size()); 444 assert(points_being_drawn_.size());
445 CCPoint start_point = points_being_drawn_.front(); 445 CCPoint start_point = points_being_drawn_.front();
446 446
447 assert(points_being_drawn_.size()); 447 assert(points_being_drawn_.size());
448 CCLog("new body from %d points", points_being_drawn_.size()); 448 CCLog("new body from %d points", points_being_drawn_.size());
449 // create initial body 449 // create initial body
450 b2BodyDef def; 450 b2BodyDef def;
451 def.type = b2_dynamicBody; 451 def.type = b2_dynamicBody;
452 def.position.Set(SCREEN_TO_WORLD(start_point.x), 452 def.position.Set(SCREEN_TO_WORLD(start_point.x),
453 SCREEN_TO_WORLD(start_point.y)); 453 SCREEN_TO_WORLD(start_point.y));
(...skipping 21 matching lines...) Expand all
475 continue; 475 continue;
476 } 476 }
477 AddLineToBody(body, start_point, end_point); 477 AddLineToBody(body, start_point, end_point);
478 start_point = *iter; 478 start_point = *iter;
479 } 479 }
480 480
481 points_being_drawn_.clear(); 481 points_being_drawn_.clear();
482 return body; 482 return body;
483 } 483 }
484 484
485 void PhysicsLayer::AddShapeToBody(b2Body *body, b2Shape* shape) { 485 void LevelLayer::AddShapeToBody(b2Body *body, b2Shape* shape) {
486 b2FixtureDef shape_def; 486 b2FixtureDef shape_def;
487 shape_def.shape = shape; 487 shape_def.shape = shape;
488 shape_def.density = box2d_density_; 488 shape_def.density = box2d_density_;
489 shape_def.friction = box2d_friction_; 489 shape_def.friction = box2d_friction_;
490 shape_def.restitution = box2d_restitution_; 490 shape_def.restitution = box2d_restitution_;
491 body->CreateFixture(&shape_def); 491 body->CreateFixture(&shape_def);
492 } 492 }
493 493
494 void PhysicsLayer::AddSphereToBody(b2Body *body, CCPoint* location) { 494 void LevelLayer::AddSphereToBody(b2Body *body, CCPoint* location) {
495 b2CircleShape shape; 495 b2CircleShape shape;
496 shape.m_radius = SCREEN_TO_WORLD(brush_radius_); 496 shape.m_radius = SCREEN_TO_WORLD(brush_radius_);
497 shape.m_p.x = SCREEN_TO_WORLD(location->x) - body->GetPosition().x; 497 shape.m_p.x = SCREEN_TO_WORLD(location->x) - body->GetPosition().x;
498 shape.m_p.y = SCREEN_TO_WORLD(location->y) - body->GetPosition().y; 498 shape.m_p.y = SCREEN_TO_WORLD(location->y) - body->GetPosition().y;
499 AddShapeToBody(body, &shape); 499 AddShapeToBody(body, &shape);
500 } 500 }
501 501
502 void PhysicsLayer::AddLineToBody(b2Body *body, CCPoint start, CCPoint end) { 502 void LevelLayer::AddLineToBody(b2Body *body, CCPoint start, CCPoint end) {
503 float distance = ccpDistance(start, end); 503 float distance = ccpDistance(start, end);
504 504
505 float sx = start.x; 505 float sx = start.x;
506 float sy = start.y; 506 float sy = start.y;
507 float ex = end.x; 507 float ex = end.x;
508 float ey = end.y; 508 float ey = end.y;
509 float dist_x = sx - ex; 509 float dist_x = sx - ex;
510 float dist_y = sy - ey; 510 float dist_y = sy - ey;
511 float angle = atan2(dist_y, dist_x); 511 float angle = atan2(dist_y, dist_x);
512 512
513 float posx = SCREEN_TO_WORLD((sx+ex)/2) - body->GetPosition().x; 513 float posx = SCREEN_TO_WORLD((sx+ex)/2) - body->GetPosition().x;
514 float posy = SCREEN_TO_WORLD((sy+ey)/2) - body->GetPosition().y; 514 float posy = SCREEN_TO_WORLD((sy+ey)/2) - body->GetPosition().y;
515 515
516 float width = SCREEN_TO_WORLD(abs(distance)); 516 float width = SCREEN_TO_WORLD(abs(distance));
517 float height = SCREEN_TO_WORLD(brush_->boundingBox().size.height); 517 float height = SCREEN_TO_WORLD(brush_->boundingBox().size.height);
518 518
519 b2PolygonShape shape; 519 b2PolygonShape shape;
520 shape.SetAsBox(width / 2, height / 2, b2Vec2(posx, posy), angle); 520 shape.SetAsBox(width / 2, height / 2, b2Vec2(posx, posy), angle);
521 AddShapeToBody(body, &shape); 521 AddShapeToBody(body, &shape);
522 } 522 }
OLDNEW
« no previous file with comments | « nacltoons/src/level_layer.h ('k') | nacltoons/src/physics_layer.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698