Permalink
Browse files

Monster Mash

  • Loading branch information...
1 parent 499e26b commit 92c4b8e9eb95c4d6a6f3603e7147ecebc352ca3a @crteal crteal committed Oct 30, 2012
Showing with 52 additions and 28 deletions.
  1. +1 −1 script/game.js
  2. +51 −27 script/monster.js
View
@@ -32,7 +32,7 @@
return "";
} else {
if (t == "monster") {
- monsters.push(spawn(1, [c, r]));
+ monsters.push(spawn(3.5, [c, r]));
}
return t;
}
View
@@ -1,48 +1,72 @@
function spawn(s, p) {
return (function (s, p) {
- var l, speed = s, position = initPosition = 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;
- }
+ var velocity,
+ orientation,
+ last = position.slice(0);
+
+ orientation = [(function (s) {
+ if (s < 0.25) {
+ return -1;
+ } else if (s < 0.5) {
+ 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;
- }
+ } (random())), (function (s) {
+ if (s < 0.25) {
+ return -1;
+ } else if (s < 0.5) {
+ return 1;
}
return 0;
- } ())];
+ } (random()))];
velocity = orientation.map(function (o) {
- return o * speed;
+ return o * speed * (dt / 1000);
});
- l = position.slice(0);
position = vect2dAdd(velocity, position);
- if (!(inbounds(position[0], position[1]) && !occupied(position[0], position[1]))) {
- position = last;
+ dx = floor(position[0]);
+ dy = floor(position[1]);
+ sx = floor(last[0]);
+ sy = floor(last[1]);
+
+ if (dx != sx || dy != sy) {
+ if (!(inbounds(dx, dy) && !occupied(dx, dy))) {
+ position = last;
+ } else {
+ l = last;
+ }
}
}
function draw() {
- var el = tile(position[0], position[1]);
- tile(l[0], l[1]).classList.remove("monster");
- el.classList.add("monster");
+ var st,
+ hasMoved,
+ el = elem(".player"),
+ dx = floor(position[0]),
+ dy = floor(position[1]),
+ sx = dx,
+ sy = dy,
+ dt = tile(dx, dy);
+
+ if (dt) {
+ if (l) {
+ sx = floor(l[0]);
+ sy = floor(l[1]);
+ }
+
+ st = tile(sx, sy);
+ if (st && st.classList.contains("monster")) {
+ st.classList.remove("monster");
+ }
+ dt.classList.add("monster");
+ }
}
return {

0 comments on commit 92c4b8e

Please sign in to comment.