Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Ship = class()
- 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.invulnDuration == 0 then
- sound(SOUND_HIT)
- self.health = math.max(self.health - damage, 0)
- local l = self.position - point
- l = l:normalize()
- self.invulnDuration = 0.5
- end
- end
- function Ship:fire()
- if self.nattacks<self.maxattacks then
- sound(SOUND_HIT,123)
- table.insert(self.attacks, Fire(self.position, self.attackPower,self.acolor) )
- self.nattacks = self.nattacks + 1
- end
- end
- function Ship:damageAtPoint(point)
- -- if self.currentAttack then
- -- dta = self.position:dist(point)
- -- if dta < self.currentAttack.currentSize * 0.4 then
- -- return dta/self.currentAttack.blastSize * 30
- -- end
- -- end
- return 0
- end
- function Ship:isDead()
- return self.health == 0
- end
- function Ship:drawDead()
- -- tint(50,50,50,255)
- sprite("Small World:Bunny Skull", self.position.x + 5, self.position.y + 25, self.size + 20)
- end
- function Ship:computeHeadAngle()
- pushMatrix()
- translate(WIDTH/2, HEIGHT/2)
- grav = vec2(Gravity.x * 300, Gravity.y * 300)
- --print(grav)
- line(0, 0, grav.x, grav.y)
- -- Arrowhead
- down = vec2(0,-1)
- orient = down:angleBetween(grav)
- pushMatrix()
- resetMatrix()
- translate(WIDTH/2,HEIGHT/2)
- translate(grav.x,grav.y)
- rotate(math.deg(orient))
- line(0, 0, -20, 25)
- line(0, 0, 20, 25)
- popMatrix()
- -- End Arrowhead
- popMatrix()
- return math.deg(orient)
- end
- function Ship:draw()
- self.invulnDuration = math.max(self.invulnDuration - 1/60, 0)
- self.angle = self:computeHeadAngle()
- moveVec = vec2(Gravity.x, Gravity.y) + vec2(0,0.6)
- self:move(moveVec)
- pushStyle()
- for i,a in ipairs(self.attacks) do
- a:draw()
- if cosmos:isCollidingWithAsteroids(a, 48, true) or
- cosmos:isCollidingWithAliens(a,45,true)
- or a.life<=0
- then
- table.remove(self.attacks,i)
- self.nattacks = self.nattacks - 1
- end
- end
- stroke(0,0,0,0)
- fill(15, 23, 65, 141)
- ellipse(self.position.x + 5, self.position.y - 5, self.size, self.size)
- self:tintForInvulnDuration(self.invulnDuration)
- pushMatrix()
- translate(self.position.x + 5, self.position.y)
- rotate(self.angle)
- sprite("SpaceCute:Rocketship", 0,0 + 25, self.size + 20)
- popMatrix()
- popStyle()
- noTint()
- ShipHealth = self.health
- end
- function Ship:touched(touch)
- if self.health == 0 then return end
- if touch.state == ENDED then --and self.invulnDuration == 0 then
- self:fire()
- end
- end
- function Ship:tintForInvulnDuration(inv)
- if inv > 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement