Skip to content

Instantly share code, notes, and snippets.

@interactivenyc
Created April 13, 2016 02:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save interactivenyc/039971dda0ba0926a155933146876a02 to your computer and use it in GitHub Desktop.
Save interactivenyc/039971dda0ba0926a155933146876a02 to your computer and use it in GitHub Desktop.
Joystick 1.1
--# Main
-- Joystick
-- Use this function to perform your initial setup
function setup()
log("setup main")
--setup left controller
joystickLeft = JoystickController("LEFT")
--setup right controller
joystickRight = JoystickController("RIGHT")
--setup hero
character = CharacterSprite()
parameter.watch("joystickLeft.jsDeltaPos")
parameter.watch("joystickRight.jsDeltaPos")
end
-- This function gets called once every frame
function draw()
-- Set Style Here
background(40, 40, 50)
strokeWidth(5)
-- Do your drawing here
joystickLeft:draw()
joystickRight:draw()
character:draw()
end
function touched(touch)
if touch.x < WIDTH/2 then
joystickLeft:touched(touch)
if touch.state == BEGAN then
joystickLeft.visible = true
elseif touch.state == ENDED then
joystickLeft.visible = false
end
elseif touch.x > WIDTH/2 then
joystickRight:touched(touch)
if touch.state == BEGAN then
joystickRight.visible = true
character:startFiring()
elseif touch.state == ENDED then
joystickRight.visible = false
character:stopFiring()
end
end
end
function log(msg)
print("Main: "..msg)
end
--# CharacterSprite
CharacterSprite = class()
function CharacterSprite:init(x)
self:log("init character")
-- you can accept and set parameters here
self.x = 200
self.y = 200
self.speed = 3
--parameters.watch("self.x")
--parameters.watch("self.y")
self.fireTimer = 0;
self.bullets = {}
end
function CharacterSprite:draw()
-- Codea does not automatically call this method
if joystickLeft.moving then
--self:log("character::move");
local newX = self.x + joystickLeft.jsDeltaPos[1] * self.speed
local newY = self.y + joystickLeft.jsDeltaPos[2] * self.speed
if newX < 0 then newX = 0 end
if newX > WIDTH then newX = WIDTH end
if newY < 0 then newY = 0 end
if newY > HEIGHT then newY = HEIGHT end
self.x = newX
self.y = newY
end
if joystickRight.firing then
self.fireTimer = self.fireTimer + 1
self:checkFire()
end
for i=1,#self.bullets do
log("move bullet: "..i)
if self.bullets[i] == nil then
--do nothing
elseif self.bullets[i].x < 0 or self.bullets[i].x > WIDTH then
self.bullets[i] = nil
elseif self.bullets[i].y < 0 or self.bullets[i].y > HEIGHT then
self.bullets[i] = nil
else
self.bullets[i]:draw()
end
end
sprite("Platformer Art:Guy Standing", self.x, self.y, 25)
end
function CharacterSprite:touched(touch)
-- Codea does not automatically call this method
self:log("touched name: "+self.name)
end
function CharacterSprite:hitTest(x,y)
self:log("hitTest")
--return vec2(self.x, self.y):dist(vec2(x,y)) < self.r
end
function CharacterSprite:moveLeft()
self:log("moveLeft")
self.x = self.x - 1
end
function CharacterSprite:moveRight()
self:log("moveRight")
self.x = self.x + 1
end
function CharacterSprite:startFiring()
self:log("startFiring: ")
self.fireTimer = 0;
end
function CharacterSprite:checkFire()
--log("checkFire: "..self.fireTimer)
if self.fireTimer > 10 then
self.fireTimer = 0
self:fire()
end
end
function CharacterSprite:fire()
self:log("FIRE!: ")
local bullet = Bullet(joystickRight.jsDeltaPos, self)
self.bullets[#self.bullets +1] = bullet
end
function CharacterSprite:destroyBullet()
self:log("FIRE!: ")
Bullet()
end
function CharacterSprite:stopFiring()
self:log("stopFiring: ")
end
function CharacterSprite:log(msg)
print("Character: "..msg)
end
--# Bullet
Bullet = class()
function Bullet:init(vector, callback)
self:log("init: "..vector[1]..","..vector[2])
-- you can accept and set parameters here
self.callback = callback
self.vector = vector
self.x = character.x
self.y = character.y
end
function Bullet:draw()
self:log("draw")
-- Codea does not automatically call this method
self.x = self.x + self.vector[1] * 20
self.y = self.y + self.vector[2] * 20
ellipse(self.x,self.y,5,5)
end
function Bullet:touched(touch)
-- Codea does not automatically call this method
end
function Bullet:log(msg)
print("Bullet: "..msg)
end
--# JoystickController
JoystickController = class()
function JoystickController:init(whichSide)
self:log("init joystick whichSide: "..whichSide)
-- you can accept and set parameters here
self.xpos = 100
self.ypos = 100
self.x = xpos
self.y = ypos
self.visible = false
self.graphic = ellipse(100,100)
self.LEFT = "LEFT"
self.RIGHT = "RIGHT"
self.WHICHSIDE = whichSide
self.jsDeltaPos = vec2(0,0)
end
function JoystickController:draw()
if self.WHICHSIDE == self.RIGHT then
--self:log("draw: "..self.WHICHSIDE)
end
-- Codea does not automatically call this method
if self.visible == true then
ellipse(self.xpos, self.ypos, 200, 200)
ellipse(CurrentTouch.x,CurrentTouch.y,50,50)
end
end
function JoystickController:touched(touch)
-- Codea does not automatically call this method
--self:log("js.touched")
self.jsDeltaPos = vec2(touch.x - self.xpos, touch.y - self.ypos):normalize()
--self.jsDeltaPos.normalize()
if touch.state == BEGAN then
self:log("touch began: "..self.WHICHSIDE)
self:log("touch began: "..touch.x..", "..touch.y)
self.xpos = touch.x
self.ypos = touch.y
self.visible = true
self.jsDeltaPos = vec2(0,0)
if self.WHICHSIDE == self.LEFT then
self.moving = true
elseif self.WHICHSIDE == self.RIGHT then
self.firing = true
end
elseif touch.state == ENDED then
self:log("touch ended: "..touch.x..", "..touch.y)
self.visible = false
self.jsDeltaPos = vec2(0,0)
if self.WHICHSIDE == self.LEFT then
self.moving = false
elseif self.WHICHSIDE == self.RIGHT then
self.firing = false
end
end
end
function JoystickController:log(msg)
print("JoystickController: "..msg)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment