Skip to content

Commit

Permalink
Player class
Browse files Browse the repository at this point in the history
  • Loading branch information
nickav committed Dec 8, 2015
1 parent f36418a commit 5adc054
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 34 deletions.
69 changes: 46 additions & 23 deletions src/GameInput.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
--]]
local GameInput = {}

local target = cc.Application:getInstance():getTargetPlatform()
local _touch = nil
local _visibleSize = nil
local _key_right = false
Expand Down Expand Up @@ -42,33 +43,55 @@ function GameInput.create(layer)

-- keyboard handlers
-- {
-- TODO: figure out Mac keyboard code differences
local function onKeyPressed(keyCode, event)
--print("pressing " .. keyCode)
--if (keyCode == cc.KeyCode.KEY_D or keyCode == cc.KeyCode.KEY_RIGHT_ARROW) then
-- D or right arrow
if (keyCode == 127 or keyCode == 27) then
_key_right = true
end
-- A or left arrow
if (keyCode == 124 or keyCode == 26) then
_key_left = true
end
-- W or up arrow
if (keyCode == 146 or keyCode == 28) then
end
-- S or down arrow
if (keyCode == 142 or keyCode == 29) then
-- for some reason Mac keyboard codes are different
if (target == cc.PLATFORM_OS_MAC) then
if (keyCode == 127 or keyCode == 27) then
_key_right = true
end
if (keyCode == 124 or keyCode == 26) then
_key_left = true
end
if (keyCode == 146 or keyCode == 28) then
end
if (keyCode == 142 or keyCode == 29) then
end
else
if (keyCode == cc.KeyCode.KEY_D or keyCode == cc.KeyCode.KEY_RIGHT_ARROW) then
_key_right = true
end
if (keyCode == cc.KeyCode.KEY_A or keyCode == cc.KeyCode.KEY_LEFT_ARROW) then
_key_left = true
end
if (keyCode == cc.KeyCode.KEY_W or keyCode == cc.KeyCode.KEY_UP_ARROW) then
end
if (keyCode == cc.KeyCode.KEY_S or keyCode == cc.KeyCode.KEY_DOWN_ARROW) then
end
end
end
local function onKeyReleased(keyCode, event)
-- D or right arrow
if (keyCode == 127 or keyCode == 27) then
_key_right = false
end
-- A or left arrow
if (keyCode == 124 or keyCode == 26) then
_key_left = false
if (target == cc.PLATFORM_OS_MAC) then
if (keyCode == 127 or keyCode == 27) then
_key_right = false
end
if (keyCode == 124 or keyCode == 26) then
_key_left = false
end
if (keyCode == 146 or keyCode == 28) then
end
if (keyCode == 142 or keyCode == 29) then
end
else
if (keyCode == cc.KeyCode.KEY_D or keyCode == cc.KeyCode.KEY_RIGHT_ARROW) then
_key_right = false
end
if (keyCode == cc.KeyCode.KEY_A or keyCode == cc.KeyCode.KEY_LEFT_ARROW) then
_key_left = false
end
if (keyCode == cc.KeyCode.KEY_W or keyCode == cc.KeyCode.KEY_UP_ARROW) then
end
if (keyCode == cc.KeyCode.KEY_S or keyCode == cc.KeyCode.KEY_DOWN_ARROW) then
end
end
end
local keyListener = cc.EventListenerKeyboard:create()
Expand Down
24 changes: 13 additions & 11 deletions src/PlayScene.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local GameInput = require("GameInput")
local Timer = require("Timer")
local Player = require("Player")

local PlayScene = class("PlayScene", function()
return cc.Scene:create()
Expand Down Expand Up @@ -32,9 +33,8 @@ function PlayScene:createLayer()
layer:addChild(draw)

-- create player
local player = cc.Sprite:create("player.png")
local player = Player.new(layer, self.center, radius)
layer:addChild(player)
player:setAnchorPoint(0.5, 0)

-- move camera
layer:setPosition(0, -self.center.y - radius/2)
Expand All @@ -52,26 +52,28 @@ function PlayScene:createLayer()
layer:runAction(cc.EaseInOut:create(moveBottomAndZoomIn, 6))
--]]

local rot = 0
-- update every frame
do
local godSpeed = 150

local time = 0
local function update(dt)
time = time + dt

-- rotate the world
if GameInput.pressingLeft() then
layer:setRotation(layer:getRotation() - 150*dt)
layer:setRotation(layer:getRotation() - godSpeed*dt)
if layer:getRotation() < 0 then
layer:setRotation(layer:getRotation() + 360)
end
elseif GameInput.pressingRight() then
layer:setRotation(layer:getRotation() + 150*dt)
layer:setRotation(layer:getRotation() + godSpeed*dt)
if layer:getRotation() >= 360 then
layer:setRotation(layer:getRotation() - 360)
end
end

-- make player on top of the world
local angle = math.rad(layer:getRotation() + 90)
local x = (radius - 1) * math.cos(angle) + self.center.x
local y = (radius - 1) * math.sin(angle) + self.center.y
player:setPosition(x, y)
player:setRotation(-layer:getRotation())
player:update(dt)
end
self.schedulerID = cc.Director:getInstance():getScheduler():scheduleScriptFunc(update, 0, false)

Expand Down
25 changes: 25 additions & 0 deletions src/Player.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
local Player = class("Player", function()
return cc.Sprite:create("player.png")
end)

function Player:ctor(layer, center, radius)
self:setAnchorPoint(0.5, 0.05)
self.layer = layer
self.radius = radius
self.center = center
self.rotation = nil
end

function Player:update(dt)
-- make player on top of the world
if self.rotation ~= self.layer:getRotation() then
local angle = math.rad(self.layer:getRotation() + 90)
local x = self.radius * math.cos(angle) + self.center.x
local y = self.radius * math.sin(angle) + self.center.y
self:setPosition(x, y)
self.rotation = self.layer:getRotation()
self:setRotation(-self.rotation)
end
end

return Player

0 comments on commit 5adc054

Please sign in to comment.