Skip to content

Instantly share code, notes, and snippets.

@NerfedWar
Created May 28, 2013 18:50
Show Gist options
  • Save NerfedWar/5665140 to your computer and use it in GitHub Desktop.
Save NerfedWar/5665140 to your computer and use it in GitHub Desktop.
AutoGist your Codea projects!
-- Animation class
Animation = class()
function Animation:init(sheet, numFrames, columns, width, height, delay)
-- load each animation frame in the sheet into a seperate image.
frames = {}
row = 1
column = 1
for i = 1, 32 do
frameImage = sheet:copy(width*(column-1), height*(row-1), width, height)
table.insert(frames, frameImage)
column=column+1
if(column>columns) then
row = row + 1
column = 1
end
end
self.frames = frames -- table of sprite names
self.currentFrame = 1 -- the current frame
self.frameDelay = delay -- how many iterations of main draw loop before changing frame
self.count = 1 -- keeps track of iterations passed
end
function Animation:draw(x, y, w, h)
-- Increment the counter
self.count = self.count + 1
-- Check if frame should be updated
if self.count > self.frameDelay then
-- Reset count and increment current frame
self.count = 1
self.currentFrame = self.currentFrame + 1
-- Check if we've reached the last frame
if self.currentFrame > table.maxn(self.frames) then
-- Back to first frame
self.currentFrame = 1
end
end
-- Draw frame
if w and h then
sprite(self.frames[self.currentFrame], x, y, w, h)
else
sprite(self.frames[self.currentFrame], x, y)
end
end
-- For the parameter
function Animation:setFrameDelay(newDelay)
self.frameDelay = newDelay
end
Game = class()
function Game:init()
parameter.integer("Current element", 1, 4, 1,function(s)
self.activePlayerElement=s
end)
-- element constants
self.ELEMENT_NONE = 1
self.ELEMENT_FIRE = 2
self.ELEMENT_WATER = 3
self.ELEMENT_AIR = 4
self.ELEMENT_EARTH = 5
-- current active elements
self.activePlayerElement = self.ELEMENT_AIR
self.activeEnemyElement = self.ELEMENT_NONE
-- load spritesheets
self.castingCircleSpriteSheet = readImage("Dropbox:CastingCircle_SpriteSheet")
self.backgroundSpriteSheets = {}
self.backgroundSpriteSheets[self.ELEMENT_FIRE] = readImage("Dropbox:FireBackground")
self.backgroundSpriteSheets[self.ELEMENT_AIR] = readImage("Dropbox:AirBackground")
-- create animations
self.castingCircleAnimation = Animation(self.castingCircleSpriteSheet, 32, 1, 500, 400, 2)
self.backgroundAnimations = {}
self.backgroundAnimations[self.ELEMENT_FIRE] = Animation(self.backgroundSpriteSheets[self.ELEMENT_FIRE], 32, 8, 512, 512, 2)
self.backgroundAnimations[self.ELEMENT_AIR] = Animation(self.backgroundSpriteSheets[self.ELEMENT_AIR], 32, 8, 512, 512, 2)
end
function Game:draw()
-- set background color
background(0, 0, 0, 255)
-- elemental backgrounds
self.backgroundAnimations[self.activePlayerElement]:draw(WIDTH / 2, HEIGHT / 2, 756, 756)
-- casting circles
self.castingCircleAnimation:draw(WIDTH / 2, HEIGHT / 2 - 200)
-- spell drawing
end
function Game:touched(touch)
if self.activePlayerElement==self.ELEMENT_AIR then
self.activePlayerElement=self.ELEMENT_FIRE
else
self.activePlayerElement=self.ELEMENT_AIR
end
end
function Game:exit()
end
-- Magic Duels
-- This example shows off the functionality of the viewer sidebar
-- The sidebar can be filled with interactive widgets, called parameters, that
-- can be adjusted live, while your code is running to affect your result.
-- The below example simply draws a circle and some text, and uses parameters to
-- control the various styles and positions of elements
function setup()
parameter.integer("Current scene",1,3,1,function(s)
if s == 1 then
changeScene("start")
elseif s == 2 then
changeScene("game")
elseif s == 3 then
changeScene("gameover")
end
end)
end
function draw()
background(40, 40, 50)
scene:draw()
end
function touched(touch)
scene:touched(touch)
end
function changeScene(s)
if scene ~= nil then
scene:exit()
end
if s == "start" then
scene = Start()
sID = s
elseif s == "game" then
scene = Game()
sID = s
elseif s == "gameover" then
scene = GameOver()
sID = s
else
print("Invalid scene name")
end
end
Start = class()
function Start:init()
self.imgSplash = readImage("Dropbox:Magic Duels Logo")
end
function Start:draw()
background(0, 0, 0, 255)
sprite(self.imgSplash, WIDTH/2, HEIGHT/2)
end
function Start:touched(touch)
changeScene("game")
end
function Start:exit()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment