Created
March 17, 2012 16:16
-
-
Save Sephi-Chan/2061616 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
ig | |
.module('game.entities.shield') | |
.requires('impact.entity') | |
.defines -> | |
window.EntityShield = ig.Entity.extend | |
collides: ig.Entity.COLLIDES.NEVER | |
type: ig.Entity.TYPE.B | |
checkAgainst: ig.Entity.TYPE.B | |
size: | |
x: 110 | |
y: 110 | |
center: | |
x: 0 | |
y: 0 | |
absorbSound: new ig.Sound('media/sounds/shield-absorb.*') | |
init: (x, y, settings)-> | |
@radius = @size.x/2 | |
@ship = settings.ship | |
x = x - @size.x/2 | |
y = y - @size.y/2 | |
@absorbSound.volume = 0.6 | |
@parent(x, y, settings) | |
update: -> | |
# Follow the ship. | |
@pos.x = @ship.center.x - @size.x/2 | |
@pos.y = @ship.center.y - @size.y/2 | |
@center.x = @pos.x + @size.x/2 | |
@center.y = @pos.y + @size.y/2 | |
@parent() | |
check: (other)-> | |
# The ship doesn't absorb its own bullets. | |
if other.shipId and other.shipId isnt @ship.id | |
# Since the collision box is a square, we check | |
# if the bullet is in the area of the shield. | |
if @areaContains(other.pos.x, other.pos.y) | |
@receiveDamage(other) | |
receiveDamage: (bullet)-> | |
@impactTimer ||= new ig.Timer(0.3) | |
@impactTimer.reset() | |
@absorbSound.play() | |
bullet.kill() | |
for i in [1..5] | |
shift = | |
x: Math.random().map(0, 1, -10, 10) | |
y: Math.random().map(0, 1, -10, 10) | |
ig.game.spawnEntity(EntityExplosion, bullet.pos.x + shift.x, bullet.pos.y + shift.y) | |
draw: -> | |
context.save() | |
context.globalAlpha = 0.8 | |
context.lineWidth = 1 | |
displayAnimation = @impactTimer && @impactTimer.delta() < 0 | |
if displayAnimation | |
# The more time passes since the hit, the less red in the color | |
# and the more visible is the shield. Radius 0 is the center of ship. | |
remainingTime = Math.abs(@impactTimer.delta()) | |
redQuantity = ~~remainingTime.map(0.0, 0.3, 150, 255) | |
endRadius = ~~remainingTime.map(0.0, 0.3, @radius/2, @radius/3) | |
context.strokeStyle = "rgb(#{redQuantity}, 255, 0)" | |
gradient = context.createRadialGradient( | |
@center.x | |
@center.y | |
@radius | |
@center.x | |
@center.y | |
endRadius | |
) | |
gradient.addColorStop(0, "rgba(#{redQuantity}, 255, 0, 0.6)") | |
gradient.addColorStop(0.4, "rgba(#{redQuantity}, 255, 0, 0)") | |
else | |
context.strokeStyle = "rgb(0, 255, 0)" | |
gradient = context.createRadialGradient( | |
@center.x | |
@center.y | |
@radius | |
@center.x | |
@center.y | |
@radius/2 | |
) | |
gradient.addColorStop(0, "rgba(0, 255, 0, 0.6)") | |
gradient.addColorStop(0.4, "rgba(0, 255, 0, 0)") | |
context.fillStyle = gradient | |
context.beginPath() | |
context.arc( | |
@center.x | |
@center.y | |
@radius | |
0 | |
2 * Math.PI | |
false | |
) | |
context.fill() | |
context.stroke() | |
context.restore() | |
areaContains: (x, y)-> | |
# Point is in area if (x - center.x) ^ 2 + (y - center.y) ^ 2 is < to radius ^ 2. | |
sum = Math.pow((x - @center.x), 2) + Math.pow((y - @center.y), 2) | |
sum <= Math.pow(@radius, 2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment