Skip to content

Instantly share code, notes, and snippets.

@ptom98
Created July 25, 2012 14:22
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 ptom98/3176438 to your computer and use it in GitHub Desktop.
Save ptom98/3176438 to your computer and use it in GitHub Desktop.
Codea Lady Killer Attempt
Button = class()
function Button:init(args)
-- you can accept and set parameters here
self.width=args.width or 100
self.height=args.height or 100
self.x=args.x or WIDTH - 100
self.y=args.y or 0
self.isPressed=false
self.touchId=nil
end
function Button:draw()
-- Codea does not automatically call this method
if self.isPressed==false then
fill(223, 28, 188, 255)
else
fill(207, 40, 40, 255)
end
rect(self.x,self.y,self.width,self.height)
end
function Button:touched(t)
-- Codea does not automatically call this method
-- only recognise one touch and track it
if t.state == BEGAN and self.touchId == nil then
self:pressed(t)
elseif t.id == self.touchId then
if t.state == MOVING then
self:pressed(t)
elseif t.state == ENDED or t.state == CANCELLED then
self:reset()
end
end
end
function Button:pressed(t)
if t.x>=self.x and t.x<=self.width+self.x and
t.y>=self.y and t.y<=self.height+self.y then
self.touchId=t.id
self.isPressed=true
end
end
function Button:isButtonDown()
return self.isPressed
end
function Button:getTouchId()
return self.touchId
end
function Button:reset()
self.isPressed=false
self.touchId=nil
end
Diver = class()
function Diver:init(args)
-- you can accept and set parameters here
self.pos=vec2(args.x or HEIGHT/2, args.y or WIDTH/2)
--self.pos2=vec2(self.pos.x, self.pos.y)
self.speed=args.speed or 20 -- pixels per second
self.anim_frame=1
self.anim_counter1=1
self.anim_animating=false
-- directions :
-- 8 1 2
-- \ | /
-- 7 - O - 3
-- / | \
-- 6 5 4
self.anim_direction=5 -- set default direction
self.anim_frames={7,8,8,8,8,8,8,8,9,9,9,9,9,9,9,9,6,6,6,6,6,6,6,6} -- direction animation max frame
self.anim_splash_frames=10 -- splash animation max frame
self.splash1=false
self.splash1Frame=1
self.splashPos1=vec2(0,0)
-- 0=standing, 1=diving, 2=swimming, 4=surfacing
self.diverState=0
self.swimming=false
end
function Diver:draw()
-- Codea does not automatically call this method
-- get new position for diver
self:isDiving(button1:isButtonDown(), button1:getTouchId())
self:changeDirection(joystick:getDirection())
-- update animation and draw to the screen
self:update_animation()
end
function Diver:touched(touch)
-- Codea does not automatically call this method
end
function Diver:isDiving(buttonPress, buttonPressId)
-- switch the character between diving and non diving states
if buttonPress==true and buttonId~=buttonPressId then
buttonId=buttonPressId
if self.diverState==0 then
self.anim_direction = self.anim_direction + 8
self.diverState=1
self.anim_frame=1
self.anim_animating=true
elseif self.diverState==2 then
self.anim_direction=self.anim_direction-16
self.diverState=0
self.anim_frame=1
self.anim_animating=false
end
end
end
function Diver:update_animation()
-- counter for animation, needs a rewrite for better timing
if self.anim_animating==true then
self.anim_counter1=self.anim_counter1+1
if self.anim_counter1==5 then
self.anim_counter1=1
if self.anim_animating==true then
self.anim_frame=self.anim_frame+1
if self.anim_frame==self.anim_frames[self.anim_direction] then
self.anim_frame=1
if self.diverState==1 then
self.splash1=true
self.splash1_frame=1
self.splashPos1.x=self.pos.x
self.splashPos1.y=self.pos.y
self.diverState=2
self.anim_direction = self.anim_direction+8
end
end
end
if self.splash1==true then
self.splash1Frame=self.splash1Frame+1
if self.splash1Frame>self.anim_splash_frames then
self.splash1Frame=1
self.splash1=false
end
end
end
-- move diver on screen
if self.diverState==0 or self.swimming==true then
if self.anim_direction==1 or self.anim_direction==17 then
self.pos.y=self.pos.y+self.speed
elseif self.anim_direction==2 or self.anim_direction==18 then
self.pos.x = self.pos.x + self.speed
self.pos.y = self.pos.y + self.speed
elseif self.anim_direction==3 or self.anim_direction==19 then
self.pos.x = self.pos.x + self.speed
elseif self.anim_direction==4 or self.anim_direction==20 then
self.pos.x = self.pos.x + self.speed
self.pos.y = self.pos.y -self.speed
elseif self.anim_direction==5 or self.anim_direction==21 then
self.pos.y = self.pos.y -self.speed
elseif self.anim_direction==6 or self.anim_direction==22 then
self.pos.y = self.pos.y -self.speed
self.pos.x = self.pos.x -self.speed
elseif self.anim_direction==7 or self.anim_direction==23 then
self.pos.x = self.pos.x -self.speed
elseif self.anim_direction==8 or self.anim_direction==24 then
self.pos.x = self.pos.x -self.speed
self.pos.y = self.pos.y + self.speed
end
end
else
self.anim_frame=0
end
-- draw diver
if self.diverState==0 then -- if standing
sprite("Dropbox:diver_"..self.anim_direction.."_"..self.anim_frame, self.pos.x, self.pos.y)
elseif self.diverState==1 then -- if diving
sprite("Dropbox:diver_jump_"..(self.anim_direction-8).."_"..self.anim_frame,self.pos.x, self.pos.y)
elseif self.diverState==2 then -- if swimming
sprite("Dropbox:diver_swim_"..(self.anim_direction-16).."_"..self.anim_frame, self.pos.x, self.pos.y)
end
-- draw splash
if self.splash1==true then
sprite("Dropbox:splash"..self.splash1Frame,self.splashPos1.x,self.splashPos1.y)
end
end
function Diver:changeDirection(direction)
-- set animation to true and change direction/frame if needed
if self.diverState==0 then
if direction==0 then
self.anim_animating=false
else
self.anim_animating=true
if self.anim_direction~=direction then
self.anim_frame=1
self.anim_direction=direction
end
end
elseif self.diverState==2 then
if direction==0 then
self.swimming=false
else
self.swimming=true
if (self.anim_direction-16)~=direction then
self.anim_frame=1
self.anim_direction=direction+16
end
end
end
end
Joystick = class()
function Joystick:init(args)
-- you can accept and set parameters here
self.x=args.x or 0
self.y=args.y or 0
self.width=args.width or 75
self.height=args.height or 75
self.direction={0,0,0,0,0,0,0,0,0}
self.realDirection={6,5,4,7,0,3,8,1,2}
self.touchId=nil
end
function Joystick:draw()
-- Codea does not automatically call this method
-- draw joystick to screen
local counter=1
for i=0,2 do -- x
for j=0,2 do -- y
if(self.direction[counter]==1) then
-- if touching area, highlight it
fill(232, 76, 95, 255)
else
fill(66, 164, 95, 255)
end
rect(self.x+(j*self.width),self.y+(i*self.height),self.width, self.height)
counter = counter + 1
end
end
end
function Joystick:touched(t)
-- only recognise one touch and track it
if t.state == BEGAN and self.touchId == nil then
self:jDirection(t)
elseif t.id == self.touchId then
if t.state == MOVING then
self.direction={0,0,0,0,0,0,0,0}
self:jDirection(t)
elseif t.state == ENDED or t.state == CANCELLED then
self:reset()
end
end
end
function Joystick:reset()
-- reset touch vsriables
self.direction={0,0,0,0,0,0,0,0}
self.touchId=nil
end
function Joystick:jDirection(t)
-- determine what direction has been pressed
local counter=1
for i=0,2 do -- x
for j=0,2 do -- y
if t.x>=self.x+(j*self.width) and t.x<=self.x+(j*self.width)+self.width then
if t.y>=self.y+(i*self.height) and t.y<=self.y+(i*self.height)+self.height then
self.touchId = t.id
self.direction[counter]=1
end
end
counter = counter + 1
end
end
end
function Joystick:getDirection()
-- return true direction
for i=1,9 do
if self.direction[i]==1 then
return self.realDirection[i]
end
end
return 0
end
-- Lady Killer ripoff
function setup()
displayMode(FULLSCREEN)
-- create player
player1=Diver({x=WIDTH/2, y=HEIGHT/2, speed=2})
-- create play area
-- floor=PlayArea()
-- create joystick
joystick=Joystick({x=20,y=50,width=50,height=50})
button1=Button({x=WIDTH - 120, y=50})
buttonId=nil
end
function draw()
background(110, 164, 203, 255)
-- draw to the screen
-- floor:draw()
player1:draw()
joystick:draw()
button1:draw()
end
function touched(t)
joystick:touched(t)
button1:touched(t)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment