richtaur (owner)

Revisions

gist: 213044 Download_button fork
public
Public Clone URL: git://gist.github.com/213044.git
Embed All Files: show embed
left4defense.js #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
(function() {
 
  var SCREEN_WIDTH = 480,
    SCREEN_HEIGHT = 360,
    SPRITE_SIZE = 24,
    TILE_SIZE = 24,
    TILES_WIDTH = 20,
    TILES_HEIGHT = 15;
 
  var game,
    map,
    screens;
 
  var init = function() {
 
    // Setup DGE
    DGE.set('stage', 'l4d');
    DGE.set('default-dims', TILE_SIZE, TILE_SIZE);
    DGE.set('tile-dims', TILE_SIZE);
 
    DGE.init({
      base : 'img/',
      stage : 'l4d'
    });
 
/*
screens = new DGE.Screens({},
'battle'
);
*/
 
// screens.battle.init(function() {
 
      // Initialize the map Sprite
      map.sprite = new DGE.Sprite({
// addTo : this,
        width : (TILES_WIDTH * TILE_SIZE),
        height : (TILES_HEIGHT * TILE_SIZE)
      });
 
// });
 
// screens.battle.init();
 
    // Begin the DGE main game loop
    DGE.main.start();
 
    // TODO - get rid of this, find a more elegant way
// screens.battle.show();
    map.init();
    newGame();
    // /TODO
 
  };
 
  var map = {
    init : function() {
 
      // Make the new tile map
      this.tiles = DGE.sprites.tileMap(DGE.Sprite, TILES_WIDTH, TILES_HEIGHT, {
        //addTo : this.sprite,
        group : 'tiles',
        image : 'concrete.png',
        width : SPRITE_SIZE,
        height : SPRITE_SIZE
      });
 
    },
    load : function(num) {
 
      var freeCoords = [],
        i,
        m = L4D.maps[num],
        tile, x, y;
 
      for (y = 0; y < TILES_HEIGHT; y++) {
        for (x = 0; x < TILES_WIDTH; x++) {
          tile = m.data[y][x];
          this.tiles[y][x].image(L4D.tiles[tile]);
          if (tile == 'o') freeCoords.push({
            tx : x,
            ty : y,
          });
        }
      }
 
      this.survivors = [];
 
      for (i = 0; i < m.survivors; i++) {
        this.survivors.push(new Survivor({
          txy : freeCoords.shift()
        }));
      };
 
      this.numSurvivors = this.survivors.length;
      this.zombies = [];
 
      for (i = 0; i < m.zombies; i++) {
        this.zombies.push(new Zombie({
          tx : DGE.rand(m.spawn.tx1, m.spawn.tx2),
          ty : DGE.rand(m.spawn.ty1, m.spawn.ty2)
        }));
      }
 
      this.numZombies = this.zombies.length;
 
// TODO clean this up, get it into main()
setInterval(function() {
 
  map.zombies.sort(function(a, b) {
    return (a.y - b.y);
  });
 
  for (var i = 0; i < map.numZombies; i++) {
    map.zombies[i].front();
  }
 
}, 100);
 
    }
  };
 
  var newGame = function() {
    map.load(0);
  };
 
  var Survivor = DGE.extend(DGE.Sprite, function(conf) {
 
    this.init(conf, {
      group : 'survivors',
      image : 'zoey_d.gif'
    });
 
  });
 
  var Zombie = DGE.extend(DGE.Sprite, function(conf) {
 
    this.init(conf, {
      group : 'zombies',
      image : 'zombie_u.gif',
      move : DGE.move.angle,
      ping : function() {
 
        if (DGE.rand(0, 50) == 1) {
 
          var survivor = map.survivors[DGE.rand(0, (map.survivors.length - 1))];
 
          this.speed(DGE.rand(1, 3) / 8);
          this.angleTo(survivor, true);
 
        }
 
      }
    });
 
  });
 
  var zombies = {
    getSpeed : function() {
 
      var p = DGE.rand(1, 100),
        speed;
 
      if (p < 50) {
        speed = DGE.rand(1, 5) / DGE.rand(2, 10);
      } else if (p < 80) {
        speed = 1;
      } else if (p < 90) {
        speed = DGE.rand(1, 5) / DGE.rand(1, 2);
      } else {
        speed = DGE.rand(1, 5) / DGE.rand(8, 10);
      }
 
      return speed;
 
    }
  };
 
  init();
 
})();