Permalink
Browse files

Waypoint-based enemy movement system

  • Loading branch information...
1 parent b23491d commit 09bc1d38640c7ecc140226a4c3d979f0b860564a @lessandro committed Dec 18, 2012
Showing with 47 additions and 40 deletions.
  1. +28 −3 src/enemy.ls
  2. +19 −37 src/level.ls
View
@@ -1,14 +1,39 @@
class Enemy extends Entity
- (@game, @x, @y, @width = 32, @height = 32) ->
+ (@game, enemy) ->
+ @width = 32
+ @height = 32
@dead = false
@direction = 1
+ @sprite = enemy.sprite
+ @setup-waypoints enemy.waypoints
+
+ setup-waypoints: (waypoints) ->
+ @segment-length = 15 # frame ticks
+ @waypoint-length = waypoints.length * @segment-length
+
+ @waypoints = [[x * Tile.size, y * Tile.size] for [x, y] in waypoints]
+ @waypoints.push @waypoints[0]
+
+ # 0....1....2....0
+
+ interpolate: ([x0, y0], [x1, y1], t) ->
+ [dx, dy] = [x1 - x0, y1 - y0]
+ return [x0 + dx * t, y0 + dy * t]
tick: ->
+ waypoint-position = (@game.frame % @waypoint-length) / @segment-length
+ # ^ number from 0 to waypoint.length
+ wp0 = Math.floor waypoint-position
+ wp1 = Math.ceil waypoint-position
+ t = waypoint-position - wp0
+ [@x, @y] = @interpolate @waypoints[wp0], @waypoints[wp1], t
+
@direction = if @game.level.player.x < @x then -1 else 1
- @shoot 6
+ if @on-screen! && (@game.frame % 40 == 0)
+ @shoot 9
draw: ->
- sprite = \player0
+ sprite = @sprite
sprite += (if @direction == 1 then \r else \l)
@game.canvas.draw-sprite @x, @y, sprite
View
@@ -55,19 +55,15 @@ class Level
]
enemies: [
- {
- x: 37
- y: 4
- }
- {
- x: 55
- y: 4
- }
+ * sprite: 'player1'
+ waypoints: [[37 4] [38 5] [38 3]]
+ * sprite: 'player1'
+ waypoints: [[55 4] [56 5] [56 3]]
]
},
{
player:
- x: 2
+ x: 1
y: 5
tiles: [
@@ -84,14 +80,8 @@ class Level
]
enemies: [
- {
- x: 36
- y: 4
- }
- {
- x: 29
- y: 2
- }
+ * sprite: 'player1'
+ waypoints: [[35 4] [35 3] [34 2] [32 2] [30 2] [28 2] [27 3] [28 4] [29 6] [31 7] [33 7] [35 7] [35 6]]
]
},
{
@@ -101,30 +91,24 @@ class Level
tiles: [
'G *%%% * * * * * * * * *%%%%% * * * GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG GGGG'
- 'G %%%%% * * * * * * * %%%%% * * GGGGG D D D P|P P|P R RGGG'
- 'G %%%%% * * * * %%%%% T R RGGG %%% G G GG'
- 'G %%%%% %%%%% G GGG G %%% = G'
- 'G* P|P P J P|P GG GGGG %%% G R G'
+ 'G %%%%% * * * * * * * %%%%% * * R GGGGG D D D P|P P|P R RGGG'
+ 'G %%%%% * * * * %%%%% T G RGGG %%% G G ZGG'
+ 'G %%%%% %%%%% GGG G %%% =JG'
+ 'G* P|P P P|P GG GGGG %%% G R G'
'G | G G P G | G G GG D FGGGG | G'
- 'G | GGG Z G G G | GGD G GGGGD | G G G'
+ 'G | GGG G G G | GGD G GGGGD | G G G'
'G | GGG GGGGG G GGG G G GG F | G G G G'
'G+ | GGGGG GGGGG GGG G RGGWWWGGF F | GGG'
'GGGGGGGGGGGGGGGGGGGGGGGGGWWWWWWWWWWWWWWWWWGGGGGGGGGGGGGFFFFFFFFFFGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG'
]
enemies: [
- {
- x: 19
- y: 5
- }
- {
- x: 87
- y: 8
- }
- {
- x: 98
- y: 5
- }
+ * sprite: 'player1'
+ waypoints: [[19 5] [17 4] [15 5] [16 7] [18 8] [19 7]]
+ * sprite: 'player0'
+ waypoints: [[87 8]]
+ * sprite: 'player0'
+ waypoints: [[98 4]]
]
}
]
@@ -140,9 +124,7 @@ class Level
if Level.maps[n].enemies
for enemy in that
- x = enemy.x * Tile.size
- y = enemy.y * Tile.size
- @entities.push new Enemy(@game, x, y)
+ @entities.push new Enemy(@game, enemy)
get-coords: (x, y) ->
i = Math.floor x / Tile.size

0 comments on commit 09bc1d3

Please sign in to comment.