Advertisement
Juaxix

Asteroides - Codea game - 9/10 - Ship.lua

Nov 7th, 2011
438
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.89 KB | None | 0 0
  1. Ship = class()
  2.  
  3. function Ship:init()
  4.     -- you can accept and set parameters here
  5.     self.position = vec2(0,0)
  6.     self.size = 66
  7.     self.health = 50
  8.     self.attackPower = 1
  9.     self.attacks = {}
  10.     self.invulnDuration = 0
  11.     self.knockVel = vec2(0,0)
  12.     self.maxattacks = 3
  13.     self.nattacks = 0
  14.     watch("ShipHealth")
  15.     self.acolor   = color(255, 253, 0, 255)
  16. end
  17.  
  18. function Ship:move(dir)
  19.     newPos = self.position + dir * 20
  20.     newPos = newPos + self.knockVel
  21.     
  22.     if cosmos:isCollidingWithAsteroids(self,45,false) or 
  23.        cosmos:isCollidingWithAliens(self,66,false) then
  24.         -- hit 
  25.         self:applyDamageFromPoint(self.position,math.random(3,6))
  26.     else
  27.         if newPos.x > WIDTH then 
  28.             newPos.x = 0
  29.         elseif newPos.x < 0 then
  30.             newPos.x = WIDTH
  31.         end
  32.         if newPos.y > HEIGHT then
  33.             newPos.y = 0
  34.         elseif newPos.y < 0 then
  35.             newPos.y = HEIGHT
  36.         end
  37.         self.position = newPos
  38.     end
  39.     -- check loots
  40.     for i,l in ipairs(cosmos.loots) do
  41.         if l:collect(self.position) then
  42.             if l.type == "powerup" then
  43.                 self.acolor = l.acolor
  44.                 self.maxattacks = math.min(self.maxattacks + 1, 21)
  45.             else
  46.                 self.health = math.min(l.life+self.health,50)
  47.             end
  48.         end
  49.         if l.position.y>HEIGHT or l.position.x<0 then
  50.             table.remove(cosmos.loots, i)
  51.             cosmos.nloots = cosmos.nloots - 1
  52.         end
  53.     end
  54. end
  55.  
  56. function Ship:applyDamageFromPoint(point, damage)
  57.     if self.invulnDuration == 0 then
  58.         sound(SOUND_HIT)
  59.         self.health = math.max(self.health - damage, 0)
  60.         local l = self.position - point
  61.         l = l:normalize()
  62.         self.invulnDuration = 0.5
  63.     end
  64. end
  65.  
  66. function Ship:fire()
  67.     if self.nattacks<self.maxattacks then
  68.         sound(SOUND_HIT,123)
  69.         table.insert(self.attacks, Fire(self.position, self.attackPower,self.acolor) )
  70.         self.nattacks = self.nattacks + 1
  71.     end
  72. end
  73.  
  74. function Ship:damageAtPoint(point)
  75.   --  if self.currentAttack then
  76.    --     dta = self.position:dist(point) 
  77.    --     if dta < self.currentAttack.currentSize * 0.4 then
  78.   --          return dta/self.currentAttack.blastSize * 30
  79.    --     end
  80.   --  end
  81.     
  82.     return 0
  83. end
  84.  
  85. function Ship:isDead()
  86.     return self.health == 0
  87. end
  88.  
  89. function Ship:drawDead()
  90.    -- tint(50,50,50,255)
  91.     sprite("Small World:Bunny Skull", self.position.x + 5, self.position.y + 25, self.size + 20)
  92. end
  93.  
  94. function Ship:computeHeadAngle()
  95.     pushMatrix()
  96.  
  97.     translate(WIDTH/2, HEIGHT/2)
  98.  
  99.     grav = vec2(Gravity.x * 300, Gravity.y * 300)
  100.  
  101.     --print(grav)
  102.  
  103.     line(0, 0, grav.x, grav.y)
  104.  
  105.     -- Arrowhead
  106.     down = vec2(0,-1)
  107.     orient = down:angleBetween(grav)
  108.  
  109.     pushMatrix()
  110.     resetMatrix()
  111.  
  112.     translate(WIDTH/2,HEIGHT/2)
  113.     translate(grav.x,grav.y)
  114.     rotate(math.deg(orient))
  115.     
  116.     line(0, 0, -20, 25)
  117.     line(0, 0,  20, 25)
  118.     popMatrix()
  119.     -- End Arrowhead
  120.  
  121.     popMatrix()
  122.     return math.deg(orient)
  123. end
  124.  
  125. function Ship:draw()
  126.     self.invulnDuration = math.max(self.invulnDuration - 1/60, 0)
  127.     self.angle = self:computeHeadAngle()
  128.     
  129.     moveVec = vec2(Gravity.x, Gravity.y) + vec2(0,0.6)
  130.     self:move(moveVec)
  131.     
  132.     pushStyle()
  133.  
  134.     for i,a in ipairs(self.attacks) do
  135.         a:draw()
  136.         if cosmos:isCollidingWithAsteroids(a, 48, true) or 
  137.            cosmos:isCollidingWithAliens(a,45,true)
  138.             or a.life<=0 
  139.         then
  140.             table.remove(self.attacks,i)
  141.             self.nattacks = self.nattacks - 1
  142.         end    
  143.     end
  144.     
  145.     stroke(0,0,0,0)
  146.     fill(15, 23, 65, 141)
  147.     ellipse(self.position.x + 5, self.position.y - 5, self.size, self.size)
  148.  
  149.     self:tintForInvulnDuration(self.invulnDuration)
  150.     
  151.     pushMatrix()
  152.     translate(self.position.x + 5, self.position.y)
  153.     rotate(self.angle)
  154.     sprite("SpaceCute:Rocketship", 0,0 + 25, self.size + 20)
  155.     popMatrix()
  156.     popStyle()
  157.     noTint()
  158.     ShipHealth = self.health
  159. end
  160.  
  161. function Ship:touched(touch)
  162.     if self.health == 0 then return end
  163.     if touch.state == ENDED then --and self.invulnDuration == 0 then
  164.         self:fire()
  165.     end
  166. end
  167.  
  168. function Ship:tintForInvulnDuration(inv)
  169.     if inv > 0 then
  170.         local flashAmount = (math.sin(inv*20)+1)*0.5
  171.         tint(255,255,255,255 * flashAmount)
  172.     else
  173.         --tint(255,255,255,255)
  174.         noTint()
  175.     end
  176. end
  177.  
  178.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement