Skip to content

Commit

Permalink
implement character events
Browse files Browse the repository at this point in the history
  • Loading branch information
aidiary committed May 7, 2011
1 parent f996e6d commit ad5512c
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 17 deletions.
5 changes: 3 additions & 2 deletions js/character.js
Expand Up @@ -2,7 +2,7 @@ STOP = 0;
MOVE = 1;
PROB_MOVE = 0.1;

function Character(name, x, y, dir, movetype) {
function Character(name, x, y, dir, movetype, message) {
this.name = name;
this.x = x;
this.y = y;
Expand All @@ -16,11 +16,12 @@ function Character(name, x, y, dir, movetype) {
this.movetype = movetype;
this.animcycle = 12;
this.frame = 0;
this.message = message;

// images are class property
Character.images = new Object();
var names = ["player", "king", "minister", "soldier"];
for (i = 0; i < names.length; i++) {
for (var i = 0; i < names.length; i++) {
Character.images[names[i]] = new Image();
Character.images[names[i]].src = "images/" + names[i] + ".png";
}
Expand Down
7 changes: 0 additions & 7 deletions js/main.js
Expand Up @@ -3,14 +3,7 @@ window.onload = function() {
activeKey = null;
map = new Map("test");
player = new Player("player", 1, 1, DOWN);
king = new Character("king", 2, 1, DOWN, STOP);
minister = new Character("minister", 3, 1, DOWN, MOVE);
soldier = new Character("soldier", 4, 1, DOWN, MOVE);
// add characters to map
map.addChara(player);
map.addChara(king);
map.addChara(minister);
map.addChara(soldier);

// start mainloop
setInterval('mainLoop()', 16);
Expand Down
47 changes: 39 additions & 8 deletions js/map.js
Expand Up @@ -8,7 +8,7 @@ function Map(name) {

// images are class property
Map.images = new Array(256);
for (i = 0; i < 256; i++) {
for (var i = 0; i < 256; i++) {
Map.images[i] = new Image();
}
Map.images[0].src = "images/grass.png";
Expand All @@ -19,6 +19,9 @@ function Map(name) {

// load map data
this.load("map/" + name + ".map");

// load event data
this.loadEvent("map/" + name + ".evt");
}

Map.prototype.load = function(filename) {
Expand All @@ -36,18 +39,35 @@ Map.prototype.load = function(filename) {
this.defaultTile = parseInt(lines[1]);
// map data
this.data = new Array(this.row);
for (i = 0; i < this.row; i++) {
for (var i = 0; i < this.row; i++) {
this.data[i] = new Array(this.col);
for (j = 0; j < this.col; j++) {
for (var j = 0; j < this.col; j++) {
// map data starts from lines[2]
this.data[i][j] = parseInt(lines[i+2][j]);
}
}
}

Map.prototype.loadEvent = function(filename) {
// load event file
var httpObj = $.ajax({
url: filename,
async: false
});
var lines = httpObj.responseText.split("\n");
for (var i = 0; i < lines.length - 1; i++) {
if (lines[i][0] == "#") continue; // comment line
var data = lines[i].split("\t");
var eventType = data[0];
if (eventType == "CHARA") {
this.createChara(data);
}
}
}

Map.prototype.update = function() {
// update characters on this map
for (i = 0; i < this.charas.length; i++) {
for (var i = 0; i < this.charas.length; i++) {
this.charas[i].update(this);
}
}
Expand All @@ -61,8 +81,8 @@ Map.prototype.draw = function(ctx, offset) {
endx = startx + div(WIDTH, GS) + 1;
starty = div(offsety, GS);
endy = starty + div(HEIGHT, GS) + 1;
for (y = starty; y < endy; y++) {
for (x = startx; x < endx; x++) {
for (var y = starty; y < endy; y++) {
for (var x = startx; x < endx; x++) {
// draw default image at the outside of map
if (x < 0 || y < 0 || x > this.col-1 || y > this.row-1) {
ctx.drawImage(Map.images[this.defaultTile], x*GS-offsetx, y*GS-offsety);
Expand All @@ -73,7 +93,7 @@ Map.prototype.draw = function(ctx, offset) {
}

// draw characters on this map
for (i = 0; i < this.charas.length; i++) {
for (var i = 0; i < this.charas.length; i++) {
this.charas[i].draw(ctx, offset);
}
}
Expand All @@ -88,7 +108,7 @@ Map.prototype.isMovable = function(x, y) {
}

// cannot move to character cell
for (i = 0; i < this.charas.length; i++) {
for (var i = 0; i < this.charas.length; i++) {
if (this.charas[i].x == x && this.charas[i].y == y) {
return false;
}
Expand All @@ -100,3 +120,14 @@ Map.prototype.isMovable = function(x, y) {
Map.prototype.addChara = function(chara) {
this.charas.push(chara);
}

Map.prototype.createChara = function(data) {
var name = data[1];
var x = parseInt(data[2]);
var y = parseInt(data[3]);
var direction = parseInt(data[4]);
var moveType = parseInt(data[5]);
var message = data[6];
var chara = new Character(name, x, y, direction, moveType, message);
this.addChara(chara);
}
5 changes: 5 additions & 0 deletions map/test.evt
@@ -0,0 +1,5 @@
# test2.evt
# event file
CHARA minister 3 1 0 1 I am a minister.
CHARA king 2 1 0 0 I am a king!
CHARA soldier 4 1 0 1 I am a soldier.

0 comments on commit ad5512c

Please sign in to comment.