Ship = class() -- Free game by juaxix -- http://www.xixgames.com -- Copyright LGPL - 11/2011 function Ship:init() -- you can accept and set parameters here self.position = vec2(0,0) self.size = 66 self.health = 50 self.attackPower = 1 self.attacks = {} self.invulnDuration = 0 self.knockVel = vec2(0,0) self.maxattacks = 3 self.nattacks = 0 watch("ShipHealth") self.acolor = color(255, 253, 0, 255) end function Ship:move(dir) newPos = self.position + dir * 20 newPos = newPos + self.knockVel if cosmos:isCollidingWithAsteroids(self,45,false) or cosmos:isCollidingWithAliens(self,66,false) then -- hit self:applyDamageFromPoint(self.position,math.random(3,6)) else if newPos.x > WIDTH then newPos.x = 0 elseif newPos.x < 0 then newPos.x = WIDTH end if newPos.y > HEIGHT then newPos.y = 0 elseif newPos.y < 0 then newPos.y = HEIGHT end self.position = newPos end -- check loots for i,l in ipairs(cosmos.loots) do if l:collect(self.position) then if l.type == "powerup" then self.acolor = l.acolor self.maxattacks = math.min(self.maxattacks + 1, 21) else self.health = math.min(l.life+self.health,50) end end if l.position.y>HEIGHT or l.position.x<0 then table.remove(cosmos.loots, i) cosmos.nloots = cosmos.nloots - 1 end end end function Ship:applyDamageFromPoint(point, damage) if self.health == 0 then return end if self.invulnDuration == 0 then sound(SOUND_HIT) self.health = math.max(self.health - damage, 0) if self.health == 0 then table.insert(cosmos.explosions, Explosion:init(self.position,color(255,255,255,255),"Died") ) end local l = self.position - point l = l:normalize() self.invulnDuration = 0.5 end end function Ship:fire() if self.nattacks 0 then local flashAmount = (math.sin(inv*20)+1)*0.5 tint(255,255,255,255 * flashAmount) else --tint(255,255,255,255) noTint() end end