| OLD | NEW |
| 1 -- Main entry point for use of lua in the game. | 1 -- Main entry point for use of lua in the game. |
| 2 -- Currently when this file is run it will load the game data from | 2 -- Currently when this file is run it will load the game data from |
| 3 -- the 'game1' folder and populate the scene with sprites. | 3 -- the 'game1' folder and populate the scene with sprites. |
| 4 | 4 |
| 5 -- Tags used when lua creates nodes. This allows the C++ side to | 5 -- Tags used when lua creates nodes. This allows the C++ side to |
| 6 -- look up the nodes using these tags. | 6 -- look up the nodes using these tags. |
| 7 local tags = { | 7 local tags = { |
| 8 LAYER_PHYSICS = 100, | 8 LAYER_PHYSICS = 100, |
| 9 LAYER_UI = 101, | 9 LAYER_UI = 101, |
| 10 BALL = 1, | 10 BALL = 1, |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 --- Load game data structure from the given lua file | 31 --- Load game data structure from the given lua file |
| 32 local function LoadGame(filename) | 32 local function LoadGame(filename) |
| 33 Log('loading game: '..filename) | 33 Log('loading game: '..filename) |
| 34 local game = dofile(filename) | 34 local game = dofile(filename) |
| 35 Log('loading backgound image: '..game.background_image) | 35 Log('loading backgound image: '..game.background_image) |
| 36 Log('loading '..table.getn(game.levels)..' level(s)') | 36 Log('loading '..table.getn(game.levels)..' level(s)') |
| 37 game.filename = filename | 37 game.filename = filename |
| 38 return game | 38 return game |
| 39 end | 39 end |
| 40 | 40 |
| 41 local function AddShapeToBody(body, shape) | 41 local function AddShapeToBody(body, shape, sensor) |
| 42 Log('adding fixture') | 42 Log('adding fixture') |
| 43 local fixture_def = b2FixtureDef:new_local() | 43 local fixture_def = b2FixtureDef:new_local() |
| 44 fixture_def.shape = shape | 44 fixture_def.shape = shape |
| 45 fixture_def.density = 1.0 | 45 fixture_def.density = 1.0 |
| 46 fixture_def.friction = 0.5 | 46 fixture_def.friction = 0.5 |
| 47 fixture_def.restitution = 0.3 | 47 fixture_def.restitution = 0.3 |
| 48 fixture_def.isSensor = sensor |
| 48 Log('adding fixture 2') | 49 Log('adding fixture 2') |
| 49 body:CreateFixture(fixture_def) | 50 body:CreateFixture(fixture_def) |
| 50 Log('adding fixture done') | 51 Log('adding fixture done') |
| 51 end | 52 end |
| 52 | 53 |
| 53 local function AddSphereToBody(body, radius) | 54 local function AddSphereToBody(body, radius, sensor) |
| 54 local shape = b2CircleShape:new_local() | 55 local shape = b2CircleShape:new_local() |
| 55 shape.m_radius = ScreenToWorld(radius) | 56 shape.m_radius = ScreenToWorld(radius) |
| 56 AddShapeToBody(body, shape) | 57 AddShapeToBody(body, shape, sensor) |
| 57 end | 58 end |
| 58 | 59 |
| 59 --- Create a fix pivot point between the world and the given object. | 60 --- Create a fix pivot point between the world and the given object. |
| 60 local function CreatePivot(world, anchor, body) | 61 local function CreatePivot(world, anchor, body) |
| 61 Log('creating anchor: '..anchor.x..'x'..anchor.y) | 62 Log('creating anchor: '..anchor.x..'x'..anchor.y) |
| 62 local anchor_point = b2Vec2:new_local(ScreenToWorld(anchor.x), | 63 local anchor_point = b2Vec2:new_local(ScreenToWorld(anchor.x), |
| 63 ScreenToWorld(anchor.y)) | 64 ScreenToWorld(anchor.y)) |
| 64 | 65 |
| 65 -- create a new fixed body to pivot against | 66 -- create a new fixed body to pivot against |
| 66 local ground_def = b2BodyDef:new_local() | 67 local ground_def = b2BodyDef:new_local() |
| (...skipping 17 matching lines...) Expand all Loading... |
| 84 Log('creating dynamic object') | 85 Log('creating dynamic object') |
| 85 body_def.type = b2_dynamicBody | 86 body_def.type = b2_dynamicBody |
| 86 end | 87 end |
| 87 local body = world:CreateBody(body_def) | 88 local body = world:CreateBody(body_def) |
| 88 | 89 |
| 89 if object.anchor ~= nil then | 90 if object.anchor ~= nil then |
| 90 CreatePivot(world, object.anchor, body) | 91 CreatePivot(world, object.anchor, body) |
| 91 end | 92 end |
| 92 | 93 |
| 93 -- calculate thickness based on brush sprite size | 94 -- calculate thickness based on brush sprite size |
| 94 local brush_size = brush:getContentSize(); | 95 local brush_tex = brush:getTexture() |
| 95 local thinkness = math.max(brush_size.height/2, brush_size.width/2); | 96 local brush_size = brush_tex:getContentSizeInPixels() |
| 97 local thickness = math.max(brush_size.height/2, brush_size.width/2); |
| 98 Log('thickness: '..thickness) |
| 96 | 99 |
| 97 -- calculate length and angle of line based on start and end points | 100 -- calculate length and angle of line based on start and end points |
| 98 local length = ccpDistance(from, to); | 101 local length = ccpDistance(from, to); |
| 99 local dist_x = to.x - from.x | 102 local dist_x = to.x - from.x |
| 100 local dist_y = to.y - from.y | 103 local dist_y = to.y - from.y |
| 101 local angle = math.atan2(dist_y, dist_x) | 104 local angle = math.atan2(dist_y, dist_x) |
| 102 Log('loading object at: '..from.x..'x'..from.y..' angle: '..angle) | 105 Log('loading object at: '..from.x..'x'..from.y..' angle: '..angle) |
| 103 | 106 |
| 104 -- create fixture | 107 -- create fixture |
| 105 local center = b2Vec2:new_local(ScreenToWorld(dist_x/2), | 108 local center = b2Vec2:new_local(ScreenToWorld(dist_x/2), |
| 106 ScreenToWorld(dist_y/2)) | 109 ScreenToWorld(dist_y/2)) |
| 107 local shape = b2PolygonShape:new_local() | 110 local shape = b2PolygonShape:new_local() |
| 108 shape:SetAsBox(ScreenToWorld(length/2), ScreenToWorld(thinkness), | 111 shape:SetAsBox(ScreenToWorld(length/2), ScreenToWorld(thickness), |
| 109 center, angle) | 112 center, angle) |
| 110 local fixture_def = b2FixtureDef:new_local() | 113 local fixture_def = b2FixtureDef:new_local() |
| 111 fixture_def.shape = shape | 114 fixture_def.shape = shape |
| 112 fixture_def.density = 1.0 | 115 fixture_def.density = 1.0 |
| 113 fixture_def.friction = 0.5 | 116 fixture_def.friction = 0.5 |
| 114 fixture_def.restitution = 0.3 | 117 fixture_def.restitution = 0.3 |
| 115 body:CreateFixture(fixture_def) | 118 body:CreateFixture(fixture_def) |
| 116 | 119 |
| 117 -- Now create a visible CCPhysicsSprite that the body is attached to | 120 -- Now create a visible CCPhysicsSprite that the body is attached to |
| 118 local brush_tex = brush:getTexture() | |
| 119 local sprite = CCPhysicsSprite:createWithTexture(brush_tex) | 121 local sprite = CCPhysicsSprite:createWithTexture(brush_tex) |
| 120 sprite:setB2Body(body) | 122 sprite:setB2Body(body) |
| 121 sprite:setPTMRatio(PTM_RATIO) | 123 sprite:setPTMRatio(PTM_RATIO) |
| 122 | 124 |
| 123 -- And add a sequence of non-physics sprites as children of the first | 125 -- And add a sequence of non-physics sprites as children of the first |
| 124 local dist = CCPointMake(dist_x, dist_y) | 126 local dist = CCPointMake(dist_x, dist_y) |
| 125 local num_children = length / thinkness | 127 local num_children = length / thickness |
| 126 local inc_x = dist_x / num_children | 128 local inc_x = dist_x / num_children |
| 127 local inc_y = dist_y / num_children | 129 local inc_y = dist_y / num_children |
| 128 local child_location = CCPointMake(0, 0) | 130 local child_location = CCPointMake(thickness, thickness) |
| 129 for i = 2,num_children do | 131 for i = 1,num_children do |
| 130 child_location.x = child_location.x + inc_x | 132 child_location.x = child_location.x + inc_x |
| 131 child_location.y = child_location.y + inc_y | 133 child_location.y = child_location.y + inc_y |
| 132 | 134 |
| 133 local child_sprite = CCSprite:createWithTexture(brush_tex) | 135 local child_sprite = CCSprite:createWithTexture(brush_tex) |
| 134 child_sprite:setPosition(child_location) | 136 child_sprite:setPosition(child_location) |
| 135 child_sprite:setAnchorPoint(CCPointMake(0, 0)) | |
| 136 sprite:addChild(child_sprite) | 137 sprite:addChild(child_sprite) |
| 137 end | 138 end |
| 138 | 139 |
| 139 return sprite | 140 return sprite |
| 140 end | 141 end |
| 141 | 142 |
| 142 --- Create CCPoint from a lua table containing 2 elements. | 143 --- Create CCPoint from a lua table containing 2 elements. |
| 143 -- The point is then offset according the origin. | 144 -- The point is then offset according the origin. |
| 144 local function PointFromLua(origin, point) | 145 local function PointFromLua(origin, point) |
| 145 return CCPointMake(point[1] + origin.x, point[2] + origin.y) | 146 return CCPointMake(point[1] + origin.x, point[2] + origin.y) |
| 146 end | 147 end |
| 147 | 148 |
| 148 --- Create ball, a physics object | 149 --- Create a physics sprite at a fiven location with a given image |
| 149 local function CreateTheBall(image, location, b2d_world) | 150 local function CreatePhysicsSprite(b2d_world, location, image, sensor) |
| 150 Log('goal: '..location.x..'x'..location.y) | 151 Log('goal: '..location.x..'x'..location.y) |
| 151 local ball = CCPhysicsSprite:create(image) | 152 local ball = CCPhysicsSprite:create(image) |
| 152 local body_def = b2BodyDef:new_local() | 153 local body_def = b2BodyDef:new_local() |
| 153 body_def.type = b2_dynamicBody | 154 if not sensor then |
| 155 body_def.type = b2_dynamicBody |
| 156 end |
| 154 local body = b2d_world:CreateBody(body_def) | 157 local body = b2d_world:CreateBody(body_def) |
| 155 ball:setB2Body(body) | 158 ball:setB2Body(body) |
| 156 ball:setPTMRatio(PTM_RATIO) | 159 ball:setPTMRatio(PTM_RATIO) |
| 157 ball:setPosition(location) | 160 ball:setPosition(location) |
| 158 AddSphereToBody(body, ball:boundingBox().size.height/2) | 161 AddSphereToBody(body, ball:boundingBox().size.height/2, sensor) |
| 159 return ball | 162 return ball |
| 160 end | 163 end |
| 161 | 164 |
| 162 --- Load the given level of the given game | 165 --- Load the given level of the given game |
| 163 -- @param game lua dictionary containing game data | 166 -- @param game lua dictionary containing game data |
| 164 -- @param level_number the level to load | 167 -- @param level_number the level to load |
| 165 local function LoadLevel(game, level_number) | 168 local function LoadLevel(game, level_number) |
| 166 Log('loading level '..level_number..' from '..game.filename) | 169 Log('loading level '..level_number..' from '..game.filename) |
| 167 -- Get level descrition object | 170 -- Get level descrition object |
| 168 local level = game.levels[level_number] | 171 local level = game.levels[level_number] |
| 169 | 172 |
| 170 -- Find the layer into which we want to load objects. | 173 -- Find the layer into which we want to load objects. |
| 171 local origin = CCDirector:sharedDirector():getVisibleOrigin() | 174 local origin = CCDirector:sharedDirector():getVisibleOrigin() |
| 172 local layer = PhysicsLayer:GetCurrent() | 175 local layer = PhysicsLayer:GetCurrent() |
| 173 local world = layer:GetWorld() | 176 local world = layer:GetWorld() |
| 174 local cache = CCTextureCache:sharedTextureCache() | |
| 175 | 177 |
| 176 local ball = CreateTheBall(game.ball_image, | 178 local ball = CreatePhysicsSprite(world, PointFromLua(origin, level.start), |
| 177 PointFromLua(origin, level.start), world) | 179 game.ball_image, false) |
| 178 layer:addChild(ball, 1, tags.BALL) | 180 layer:addChild(ball, 1, tags.BALL) |
| 179 | 181 |
| 180 -- Load brush image | 182 -- Load brush image |
| 181 game.brush_image = cache:addImage(game.brush_image) | 183 local brush = CCSpriteBatchNode:create(game.brush_image) |
| 182 local brush = CCSprite:createWithTexture(game.brush_image) | |
| 183 brush:setVisible(false) | |
| 184 layer:addChild(brush, 1, tags.BRUSH) | 184 layer:addChild(brush, 1, tags.BRUSH) |
| 185 | 185 |
| 186 -- Create goal and stars | 186 -- Create goal and stars |
| 187 Log('goal: '..level.goal[1]..'x'..level.goal[2]) | 187 Log('goal: '..level.goal[1]..'x'..level.goal[2]) |
| 188 local goal = CCSprite:create(game.goal_image) | 188 local goal = CreatePhysicsSprite(world, PointFromLua(origin, level.goal), |
| 189 goal:setPosition(PointFromLua(origin, level.goal)) | 189 game.goal_image, true) |
| 190 layer:addChild(goal, 1, tags.GOAL) | 190 layer:addChild(goal, 1, tags.GOAL) |
| 191 | 191 |
| 192 game.star_image = cache:addImage(game.star_image) | |
| 193 for i, star in ipairs(level.stars) do | 192 for i, star in ipairs(level.stars) do |
| 194 local sprite = CCSprite:createWithTexture(game.star_image) | 193 local sprite = CreatePhysicsSprite(world, PointFromLua(origin, star), |
| 194 game.star_image, true) |
| 195 local tag = tags.STAR1 + i - 1 | 195 local tag = tags.STAR1 + i - 1 |
| 196 Log('loading star [tag='..tag..']: '..star[1]..'x'..star[2]) | 196 Log('loading star [tag='..tag..']: '..star[1]..'x'..star[2]) |
| 197 layer:addChild(sprite, 1, tag) | 197 layer:addChild(sprite, 1, tag) |
| 198 sprite:setPosition(PointFromLua(origin, star)) | |
| 199 end | 198 end |
| 200 | 199 |
| 201 -- Load fixtures | 200 -- Load fixtures |
| 202 for i, object in ipairs(level.objects) do | 201 for i, object in ipairs(level.objects) do |
| 203 local start = PointFromLua(origin, object.start) | 202 local start = PointFromLua(origin, object.start) |
| 204 local finish = PointFromLua(origin, object.finish) | 203 local finish = PointFromLua(origin, object.finish) |
| 205 if object.anchor ~= nil then | 204 if object.anchor ~= nil then |
| 206 object.anchor = PointFromLua(origin, object.anchor) | 205 object.anchor = PointFromLua(origin, object.anchor) |
| 207 end | 206 end |
| 208 local line = CreateLine(world, brush, start, finish, object) | 207 local line = CreateLine(world, brush, start, finish, object) |
| 209 layer:addChild(line, 1, tags.OBJECTS_START+i-1) | 208 brush:addChild(line, 1, tags.OBJECTS_START+i-1) |
| 210 end | 209 end |
| 211 | |
| 212 game.background = cache:addImage(game.background_image) | |
| 213 end | 210 end |
| 214 | 211 |
| 215 game = LoadGame('sample_game/game.lua') | 212 -- Global variables that can be used in the lua data files. |
| 213 height = 600 |
| 214 width = 800 |
| 215 |
| 216 local game = LoadGame('sample_game/game.lua') |
| 216 LoadLevel(game, 1) | 217 LoadLevel(game, 1) |
| OLD | NEW |