Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Wheel enemy graphic and rotating enemy animations

  • Loading branch information...
commit c4cbb5b1674df1813de2277f3d46b39debc403aa 1 parent dd2b00a
Daniel Mark Schwartz authored
Showing with 42 additions and 9 deletions.
  1. +42 −9 game.coffee
  2. BIN  wheel.png
View
51 game.coffee
@@ -40,6 +40,7 @@ enemyImage.src = 'enemy.gif'
#Photo credit: Christopher Chan / Foter / CC BY-NC-ND
imgs =
+ wheel: 'wheel.png'
eye: 'eye.png'
title_monk: 'title_monk.png'
tm_light: 'tm_light.png'
@@ -169,7 +170,7 @@ class Scene
@bullets.push new Bullet x, y, bullet.move, bullet.image
spawnEnemy: (x, y, e) ->
- @enemies.push new Enemy x, y, e.move, e.fire, e.bullet, e.image, e.health
+ @enemies.push new Enemy x, y, e.move, e.fire, e.bullet, e.animate, e.health
spawnExplosion: (x, y) ->
color = new Rainbow
@@ -235,6 +236,33 @@ class Bullet
do ctx.closePath
do ctx.fill
+class Animation
+ constructor: (@image) ->
+ @width = @image.width
+ @height = @image.height
+
+ update: (dt) ->
+
+ draw: (ctx, x, y) ->
+ ctx.drawImage @image, x - @width/2, y - @height/2
+
+class AnimationRotate extends Animation
+ constructor: (@image, @rotationSpeed = 2) ->
+ @rotation = 0
+ @width = @image.width
+ @height = @image.height
+
+ update: (dt) ->
+ @rotation += @rotationSpeed*dt
+ if @rotation >= 2*Math.PI
+ @rotation = 0
+
+ draw: (ctx, x, y) ->
+ do ctx.save
+ ctx.translate x, y
+ ctx.rotate @rotation
+ ctx.drawImage @image, -@width/2, -@height/2
+ do ctx.restore
moveStraight = (dx=0, dy=50) ->
(x, y, dt) ->
@@ -268,7 +296,7 @@ enemy =
move: moveStraight dx, dy
fire: fireEvery fireRate
bullet: [() -> bulletStraight()]
- image: atom.images.eye
+ animate: new AnimationRotate atom.images.eye, 2
health: 500
circle: (n=6, speed=100)->
move: moveStraight()
@@ -280,7 +308,7 @@ enemy =
bulletStraight speed * Math.cos(theta), speed * Math.sin(theta)
)
)(i, 1)
- image: enemyImage
+ animate: new AnimationRotate atom.images.wheel
health: 1000
fan: (dx, dy, n=6, speed=100)->
odd = 1
@@ -294,25 +322,27 @@ enemy =
theta = 2 * Math.PI * (i + .5*odd) / n
bulletStraight speed * Math.cos(theta), speed * Math.sin(theta)
)
- image: enemyImage
+ animate: new Animation enemyImage
health: 1000
direct: (dx, dy, g) ->
move: moveStraightGravity dx, dy, g
fire: fireEvery 1
bullet: [(dt, s, e) -> bulletDirect s, e]
- image: enemyImage
+ animate: new Animation enemyImage
health: 1250
class Enemy
- constructor: (@x, @y, @move, @fire, @bullet, @image, health, @die) ->
- @r = @image.width/2
+ constructor: (@x, @y, @move, @fire, @bullet, @animate, health, @die) ->
@alive = true
@collision = false
@health = 1
@health_coeff = 1/health
update: (dt, s) ->
+ @animate.update dt
+ @r = @animate.width/2
+
[@x, @y] = @move @x, @y, dt
if @fire dt
@@ -320,10 +350,11 @@ class Enemy
if @x < 0 or @x > atom.width or @y < 0 or @y > atom.height
@alive = false
+
draw: (ctx) ->
ctx.globalAlpha = @health * .5 + .5
- ctx.drawImage @image, @x - @image.width/2, @y - @image.height/2
+ @animate.draw ctx, @x, @y
ctx.globalAlpha = 1
ctx.fillStyle = "orange"
@@ -602,6 +633,7 @@ class Game extends atom.Game
@musicPlay @music_source
level1 = [
+ {waitUntil: (s) -> game.loaded},
{do: (s) -> s.fadeIn .25},
{do: (s) -> game.musicPlay 'level1'},
{setBackground: new BackgroundStars},
@@ -671,7 +703,8 @@ title = [
{changeScene: new Scene level1}
]
-game = new Game(new Scene title)
+#game = new Game(new Scene title)
+game = new Game(new Scene level1)
window.onblur = ->
game.stop()
View
BIN  wheel.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit c4cbb5b

Please sign in to comment.
Something went wrong with that request. Please try again.