Created
November 22, 2011 03:20
-
-
Save anonymous/1384801 to your computer and use it in GitHub Desktop.
Wizard's Fire Missile 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
Missile = class() | |
--constants | |
MISSILE_READY = -1 | |
MISSILE_IN_FLIGHT = 0 | |
MISSILE_DONE = 1 | |
function Missile:init(x, y, speed) | |
-- current position | |
self.x = x | |
self.y = y | |
-- origin position | |
self.ox = self.x | |
self.oy = self.y | |
--pick a target | |
self.speed = speed | |
self.target = math.random(7) | |
-- set change in x per cycle | |
self.dx = (self.x - (self.target * 100 - 30)) / ((self.y - 100) / speed) | |
self.status= MISSILE_IN_FLIGHT | |
end | |
function Missile:clear() | |
stroke(0, 0, 0, 255) | |
strokeWidth(5) | |
line(self.ox, self.oy, self.x, self.y) | |
end | |
function Missile:draw() | |
-- erase previous line | |
self:clear() | |
if self.status == MISSILE_IN_FLIGHT then | |
if self.y < 120 then | |
self.status = MISSILE_DONE | |
sprite("Small World:Explosion", self.x, self.y) | |
else | |
-- update position | |
self.x = self.x - self.dx | |
self.y = self.y - self.speed | |
stroke(222, 31, 60, 255) | |
strokeWidth(3) | |
line(self.ox, self.oy, self.x, self.y) | |
end | |
end | |
end | |
function Missile: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