Skip to content

Instantly share code, notes, and snippets.

Created November 22, 2011 03:20
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 anonymous/1384801 to your computer and use it in GitHub Desktop.
Save anonymous/1384801 to your computer and use it in GitHub Desktop.
Wizard's Fire Missile Class
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