Skip to content
Browse files

Monster Movement

  • Loading branch information...
1 parent 0edd2a2 commit 1a76351d7bba17b95b838c302fd8a25f16224100 @crteal crteal committed Oct 28, 2012
Showing with 66 additions and 5 deletions.
  1. +1 −0 index.html
  2. +10 −4 script/game.js
  3. +1 −1 script/map.js
  4. +54 −0 script/monster.js
View
1 index.html
@@ -14,6 +14,7 @@
<script src="script/keyboard.js"></script>
<script src="script/map.js"></script>
<script src="script/player.js"></script>
+ <script src="script/monster.js"></script>
<script src="script/game.js"></script>
</body>
</html>
View
14 script/game.js
@@ -26,15 +26,21 @@
}
var st,
- f = 0,
- el = elem(".fps"),
+ monsters = [],
m = map(8, ["empty", "grass", "grass2", "block", "monster"], [3.25, 1.25, 1, 2, 0.5], function (t, r, c) {
- return (initPosition[1] == r && initPosition[0] == c) ? "" : t;
+ if(initPosition[1] == r && initPosition[0] == c) {
+ return "";
+ } else {
+ if (t == "monster") {
+ monsters.push(spawn(1, [c, r]));
+ }
+ return t;
+ }
});
drawMap(elem(".game"), m);
tile(initPosition[0], initPosition[1]).innerHTML = '<div class="player"></div>';
- st = game([player]);
+ st = game([player].concat(monsters));
} ());
View
2 script/map.js
@@ -73,5 +73,5 @@ function tile(x, y) {
function occupied(x, y) {
var t = tile(x, y);
- return t ? t.classList.contains("block") || t.classList.contains("monster") : false;
+ return t ? t.classList.contains("block") || t.classList.contains("monster") || t.childNodes.length : false;
}
View
54 script/monster.js
@@ -0,0 +1,54 @@
+function spawn(s, p) {
+ return (function (s, p) {
+ var l, speed = s, position = initPosition = p;
+
+ function update(dt) {
+ var last = position, orientation, velocity;
+
+ orientation = [(function () {
+ var seed = random();
+ if (seed < 0.25) {
+ if (seed < 0.1) {
+ return -1;
+ } else if (seed < 0.2) {
+ return 1;
+ }
+ }
+ return 0;
+ } ()), (function () {
+ var seed = random();
+ if (seed < 0.25) {
+ if (seed < 0.1) {
+ return -1;
+ } else if (seed < 0.2) {
+ return 1;
+ }
+ }
+ return 0;
+ } ())];
+
+ velocity = orientation.map(function (o) {
+ return o * speed;
+ });
+
+ l = position.slice(0);
+ position = vect2dAdd(velocity, position);
+
+ if (!(inbounds(position[0], position[1]) && !occupied(position[0], position[1]))) {
+ position = last;
+ }
+ }
+
+ function draw() {
+ var el = tile(position[0], position[1]);
+ tile(l[0], l[1]).classList.remove("monster");
+ el.classList.add("monster");
+ }
+
+ return {
+ update: update,
+ draw: draw
+ };
+
+ } (s, p));
+}

0 comments on commit 1a76351

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