Skip to content

Commit

Permalink
enemy wave pattern, mouse as touch input
Browse files Browse the repository at this point in the history
  • Loading branch information
nickav committed Jan 8, 2016
1 parent 2979af6 commit 47095fd
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 36 deletions.
Binary file modified res/planet.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions src/Bullet.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local Entity = require("Entity")

local Bullet = class("Bullet", function()
return Entity.extend(cc.Sprite:create("bullet.png"))
end)
Expand All @@ -19,4 +20,5 @@ function Bullet:update(dt)
end
end


return Bullet
1 change: 1 addition & 0 deletions src/Enemy.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ function Enemy:update(dt)
end
end


return Enemy
14 changes: 9 additions & 5 deletions src/Entity.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ function Entity.create(sprite)
end
--]]

-- Entity: the base game object class. Supports object pooling. Objects ready to be
-- reused can be accessed with: parent:getChildByName("className")
--
-- psuedo-multiple inheritence thing
-- pretty much just did this so auto completions for cc.Sprite still works
--[[
Entity: the base game object class.
Supports object pooling. Objects ready to be reused
can be accessed with: parent:getChildByName("className")
Psuedo-multiple inheritence thing...
Just did this so auto completions for cc.Sprite still work.
--]]
local Entity = {}

Entity.default = "Alive"
Expand Down Expand Up @@ -209,4 +212,5 @@ function Entity:moveTo(position, speed)
self.speed.y = speed * dy / distance
end


return Entity
53 changes: 49 additions & 4 deletions src/GameInput.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ local GameInput = {}

local target = cc.Application:getInstance():getTargetPlatform()
local _touch = nil
local _visibleSize = nil
local _size = nil
local _key_right = false
local _key_left = false
local _key_up = false
local _special_touch_area = false

-- attach input to layer
function GameInput.create(layer)
_visibleSize = cc.Director:getInstance():getVisibleSize()
_size = cc.Director:getInstance():getVisibleSize()
_special_touch_area = cc.rect(_size.width * 0.5 - 127, -127, 255, 255)
-- touch handlers
-- {
local function onTouchBegan(touch, event)
Expand Down Expand Up @@ -39,6 +42,34 @@ function GameInput.create(layer)
listener:registerScriptHandler(onTouchCancelled, cc.Handler.EVENT_TOUCH_CANCELLED)
layer:getEventDispatcher():addEventListenerWithSceneGraphPriority(listener, layer)
-- }

-- treat mouse as touch input
-- {
local mouseDown = false
local function onMouseDown(event)
mouseDown = true
local touch = cc.p(event:getCursorX(), event:getCursorY())
onTouchBegan(touch, event)
end

local function onMouseMoved(event)
if not mouseDown then return end
local touch = cc.p(event:getCursorX(), event:getCursorY())
onTouchMoved(touch,event)
end

local function onMouseUp(event)
mouseDown = false
local touch = cc.p(event:getCursorX(), event:getCursorY())
onTouchEnded(touch,event)
end

local mouseListener = cc.EventListenerMouse:create()
mouseListener:registerScriptHandler(onMouseDown, cc.Handler.EVENT_MOUSE_DOWN)
mouseListener:registerScriptHandler(onMouseMoved, cc.Handler.EVENT_MOUSE_MOVE)
mouseListener:registerScriptHandler(onMouseUp, cc.Handler.EVENT_MOUSE_UP)
layer:getEventDispatcher():addEventListenerWithSceneGraphPriority(mouseListener, layer)
-- }

-- keyboard handlers
-- {
Expand All @@ -52,6 +83,7 @@ function GameInput.create(layer)
_key_left = true
end
if (keyCode == 146 or keyCode == 28) then
_key_up = true
end
if (keyCode == 142 or keyCode == 29) then
end
Expand All @@ -63,6 +95,7 @@ function GameInput.create(layer)
_key_left = true
end
if (keyCode == cc.KeyCode.KEY_W or keyCode == cc.KeyCode.KEY_UP_ARROW) then
_key_up = true
end
if (keyCode == cc.KeyCode.KEY_S or keyCode == cc.KeyCode.KEY_DOWN_ARROW) then
end
Expand All @@ -77,6 +110,7 @@ function GameInput.create(layer)
_key_left = false
end
if (keyCode == 146 or keyCode == 28) then
_key_up = false
end
if (keyCode == 142 or keyCode == 29) then
end
Expand All @@ -88,6 +122,7 @@ function GameInput.create(layer)
_key_left = false
end
if (keyCode == cc.KeyCode.KEY_W or keyCode == cc.KeyCode.KEY_UP_ARROW) then
_key_up = false
end
if (keyCode == cc.KeyCode.KEY_S or keyCode == cc.KeyCode.KEY_DOWN_ARROW) then
end
Expand All @@ -101,11 +136,21 @@ function GameInput.create(layer)
end

function GameInput.pressingLeft()
return ((not _touch == nil and _touch.x < _visibleSize.width / 2) or _key_left)
return (_touch ~= nil and _touch.x < _size.width / 2) or _key_left
end

function GameInput.pressingRight()
return ((not _touch == nil and _touch.x >= _visibleSize.width / 2) or _key_right)
return (_touch ~= nil and _touch.x >= _size.width / 2) or _key_right
end

function GameInput.pressingSpecial()
local special = false
if _touch ~= nil then
local touchRect = cc.rect(_touch.x, _touch.y, 40, 40)
special = cc.rectIntersectsRect(touchRect, _special_touch_area)
end
return special or _key_up
end


return GameInput
85 changes: 58 additions & 27 deletions src/PlayScene.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ function PlayScene:ctor()
self.enemySpawnDist = math.sqrt(width * width + self.size.height * self.size.height)
self.bullets = {}

self.bg = self:createLayerBackground()
self:addChild(self.bg, -10)
--self.bg = self:createLayerBackground()
--self:addChild(self.bg, -10)
self:addChild(self:createLayerMain())
end

Expand Down Expand Up @@ -50,12 +50,15 @@ function PlayScene:createLayerMain()
local zoomOut = cc.ScaleTo:create(1, 0.5)
local moveCenterAndZoomOut = cc.Spawn:create(moveCenter, zoomOut)
layer:runAction(cc.EaseInOut:create(moveCenterAndZoomOut, 6))
layer:setScale(1)
layer:setPosition(0, -self.center.y - radius/2)
local moveBottom = cc.MoveTo:create(1, cc.p(0, -self.center.y - radius/2))
local zoomIn = cc.ScaleTo:create(1, 1)
local moveBottomAndZoomIn = cc.Spawn:create(moveBottom, zoomIn)
layer:runAction(cc.EaseInOut:create(moveBottomAndZoomIn, 6))
layer:setScale(0.5)
layer:setPosition(0,0)
layer:runAction(cc.EaseInOut:create(moveBottomAndZoomIn, 6))
--]]

-- update every frame
Expand All @@ -69,15 +72,17 @@ function PlayScene:createLayerMain()
local player = Player.new(layer, self.center, radius)
layer:addChild(player)

local rightSpeed = 0
local leftSpeed = 0
local godSpeed = 0
local accel = 4
local minSpeed = 5
local maxSpeed = 120
local shootCooldown = Timer.create(0.3)

local enemyTimer = Timer.create(2)
self:createEnemy()

local didSpecial = false
local specialTimer = Timer.create(4)

-- game speed, 1 = normal speed
local speed = 1
Expand All @@ -89,38 +94,59 @@ function PlayScene:createLayerMain()

debug:clear()

local bgSpeed = 0.5
local bgSpeed = 1

-- rotate the world
-- handle input
if GameInput.pressingRight() then
local rot = layer:getRotation() - rightSpeed*dt
if rot >= 360 then rot = rot - 360 end
layer:setRotation(rot)
self.bg:setRotation(self.bg:getRotation() - rightSpeed*dt*bgSpeed)
if godSpeed >= 0 then godSpeed = -minSpeed end

if rightSpeed < maxSpeed then
rightSpeed = rightSpeed + accel
if godSpeed > -maxSpeed then
godSpeed = godSpeed - accel
else
rightSpeed = maxSpeed
godSpeed = -maxSpeed
end
leftSpeed = minSpeed
elseif GameInput.pressingLeft() then
local rot = layer:getRotation() + leftSpeed*dt
if rot < 0 then rot = rot + 360 end
layer:setRotation(rot)
self.bg:setRotation(self.bg:getRotation() + leftSpeed*dt*bgSpeed)
if godSpeed <= 0 then godSpeed = minSpeed end

if leftSpeed < maxSpeed then
leftSpeed = leftSpeed + accel
if godSpeed < maxSpeed then
godSpeed = godSpeed + accel
else
leftSpeed = maxSpeed
godSpeed = maxSpeed
end
rightSpeed = minSpeed
else
rightSpeed = minSpeed
leftSpeed = minSpeed
godSpeed = 0
end

-- rotate the world
local rot = layer:getRotation() + godSpeed*dt
if rot >= 360 then rot = rot - 360 end
if rot < 0 then rot = rot + 360 end
layer:setRotation(rot)

-- special power up
if GameInput.pressingSpecial() then
if not didSpecial then
didSpecial = true
local moveCenter = cc.MoveTo:create(1, cc.p(0,0))
local zoomOut = cc.ScaleTo:create(1, 0.5)
local moveCenterAndZoomOut = cc.Spawn:create(moveCenter, zoomOut)
layer:runAction(cc.EaseInOut:create(moveCenterAndZoomOut, 6))
specialTimer:reset()
shootCooldown:reset(0.05)
end
end

if didSpecial then
if specialTimer.finished then
local moveBottom = cc.MoveTo:create(1, cc.p(0, -self.center.y - radius/2))
local zoomIn = cc.ScaleTo:create(1, 1)
local moveBottomAndZoomIn = cc.Spawn:create(moveBottom, zoomIn)
layer:runAction(cc.EaseInOut:create(moveBottomAndZoomIn, 6))
shootCooldown:reset(0.3)
didSpecial = false
end
end

-- shoot bullets
if shootCooldown.finished then
self:shoot(player)
Expand Down Expand Up @@ -151,7 +177,8 @@ function PlayScene:createLayerMain()
-- create enemies
if enemyTimer.finished then
self:createEnemy()
enemyTimer:reset(math.random(1, 4))
--enemyTimer:reset(math.random(1, 4))
enemyTimer:reset(0.3)
end
end
self.schedulerID = cc.Director:getInstance():getScheduler():scheduleScriptFunc(update, 0, false)
Expand Down Expand Up @@ -209,8 +236,11 @@ function PlayScene:createLayerBackground()
return layer
end

local ang = 90
function PlayScene:createEnemy()
local angle = math.random()*2*math.pi
--local angle = math.random()*2*math.pi
local angle = ang
ang = ang - 2*math.pi/36
local distance = self.enemySpawnDist

local x = math.cos(angle) * distance + self.center.x
Expand Down Expand Up @@ -268,4 +298,5 @@ function PlayScene:shoot(player)
bullet2:update(20/1000)
end


return PlayScene
1 change: 1 addition & 0 deletions src/Player.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ function Player:update(dt)
end
end


return Player

0 comments on commit 47095fd

Please sign in to comment.