-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathGameLayer.lua
84 lines (63 loc) · 2.77 KB
/
GameLayer.lua
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
require("Cocos2d")
local GameLayer = class("GameLayer", function()
return cc.LayerColor:create(cc.c4b(255,255,255,255))
end)
function GameLayer:ctor()
self._heroCount = 0
self._controllers = {}
end
function GameLayer:init(heroCount)
self._heroCount = heroCount
self:addControllers()
self.touchListener = cc.EventListenerTouchOneByOne:create()
self.touchListener:setSwallowTouches(true)
local visibleSize = cc.Director:getInstance():getVisibleSize()
local score = 0
local scoreLabel = cc.Label:createWithTTF("","fonts/Marker Felt.ttf", 32)
scoreLabel:setColor(cc.c3b(0, 0, 0))
scoreLabel:setString(score)
self:addChild(scoreLabel)
scoreLabel:setPosition(visibleSize.width / 2, visibleSize.height - 30)
local function onTouchBegan(touch, event)
for key, controller in pairs(self._controllers) do
xx = controller:getEdge():getBoundingBox()
if (cc.rectContainsPoint(xx, touch:getLocation())) then
controller:onTouch()
break
end
end
return true
end
self.touchListener:registerScriptHandler(onTouchBegan,cc.Handler.EVENT_TOUCH_BEGAN)
cc.Director:getInstance():getEventDispatcher():addEventListenerWithSceneGraphPriority(self.touchListener, self)
local function onContactBegin(contact)
self:unscheduleUpdate()
cc.Director:getInstance():getEventDispatcher():removeEventListener(self.touchListener)
cc.Director:getInstance():getEventDispatcher():removeEventListener(self.contactListener)
cc.Director:getInstance():replaceScene(require("GameOver").create(self._heroCount, score))
end
self.contactListener = cc.EventListenerPhysicsContact:create();
self.contactListener:registerScriptHandler(onContactBegin, cc.Handler.EVENT_PHYSICS_CONTACT_BEGIN);
local eventDispatcher = self:getEventDispatcher()
eventDispatcher:addEventListenerWithSceneGraphPriority(self.contactListener, self);
function update(dt)
score = score + dt
scoreLabel:setString(string.sub(score, 1, 4))
-- update all hero controllers
for key, controller in pairs(self._controllers) do
controller:onUpdate()
end
end
self:scheduleUpdateWithPriorityLua(update, 0);
end
function GameLayer:addControllers()
local visibleSize = cc.Director:getInstance():getVisibleSize()
local startY = 30
local gap = (visibleSize.height - startY) / self._heroCount
for i=1, self._heroCount do
local heroController = require("HeroController")
local controller = heroController.createController(self, startY + gap * (i - 1))
table.insert(self._controllers,0,controller)
end
end
return GameLayer