Created
November 22, 2011 03:22
-
-
Save anonymous/1384803 to your computer and use it in GitHub Desktop.
Wizard's Fire Shot Class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Shot = class() | |
-- constants | |
SHOT_READY = -1 | |
SHOT_IN_FLIGHT = 0 | |
SHOT_DONE = 1 | |
function Shot:init(sx, sy, tx, ty, stat) | |
-- you can accept and set parameters here | |
self.x = sx | |
self.y = sy | |
self.tx = tx | |
self.ty = ty | |
self.status = stat | |
self.size = 1 | |
end | |
function Shot:update() | |
if self.status == SHOT_IN_FLIGHT then | |
if self.x < self.tx then | |
self.x = self.x + 4 | |
elseif self.x > self.tx then | |
self.x = self.x - 4 | |
end | |
if self.y < self.ty then | |
self.y = self.y + 4 | |
elseif self.y > self.tx then | |
self.y = self.y - 4 | |
end | |
-- check if done | |
if math.abs(self.x - self.tx) < 6 and | |
math.abs(self.y - self.ty) < 6 then | |
self.status = SHOT_DONE | |
sound(SOUND_SHOOT,222) | |
end | |
end | |
return nil | |
end | |
function Shot:collision(x, y) | |
if math.abs(self.tx - x) < self.size + 2 and | |
math.abs(self.ty - y) < self.size + 2 then | |
return true | |
end | |
return false | |
end | |
function Shot:draw() | |
self:update() | |
if self.status == SHOT_IN_FLIGHT then | |
stroke(182, 179, 99, 255) | |
ellipse(self.x, self.y, 11, 11) | |
stroke(128, 130, 153, 255) | |
ellipse(self.tx, self.ty, 11, 11) | |
return nil | |
end | |
if self.status == SHOT_DONE then | |
self.size = self.size + 1 | |
stroke(0, 0, 0, 255) | |
fill(0, 0, 0, 255) | |
rect(self.tx-10, self.ty-10, 20, 20) | |
fill(255, 177, 0, 255) | |
stroke(255, 179, 0, 255) | |
ellipse(self.tx, self.ty, self.size * 3, self.size * 2) | |
if self.size > 20 then | |
self.x = -999 | |
self.status = SHOT_READY | |
end | |
end | |
end | |
function Shot:touched(touch) | |
-- Codify does not automatically call this method | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment