function Object() { this.name = null; this.x = 0; this.y = 0; this.width = 1; this.height = 1; this.map = 0; this.animations = {}; this.currentAnimation = null; this.takeable = false; // by default, objects cannot be taken this.decodeObjectFromXML = function() { this.name = this.xml.attributes.getNamedItem("name").nodeValue; this.time = 0; // animations: this.animations = {}; var animationElements = this.xml.getElementsByTagName("animation"); for(var i = 0;i0) { var otherCharacter = null; var retVal = executeRuleEffect(this.actionsToExecute[0], character, map, game, otherCharacter); if (retVal!=undefined) this.actionsToExecute.splice(0,1); } if (this.currentAnimation!=null && this.animations[this.currentAnimation]!=null) { this.animations[this.currentAnimation].update(); this.width = this.animations[this.currentAnimation].getWidth(); this.height = this.animations[this.currentAnimation].getHeight(); } return true; } this.walkable = function() { return true; } this.generateObjectListForSaveData = function(objects) { var idx = objects.indexOf(this); if (idx==-1) { objects.push(this); if (this.itemInside!=undefined) objects = this.itemInside.generateObjectListForSaveData(objects); if (this.inventory!=undefined) { for(var i = 0;i " + str); data.animations[key] = str; } } } if (this.currentAnimation!=undefined) data.currentAnimation = this.currentAnimation; return data; } } /* * ------------------------ ITEMS ------------------------ */ Item.prototype = new Object(); function Item(name, animation) { this.name = name; this.currentAnimation = ANIM_IDLE; this.animations = {}; this.animations[ANIM_IDLE] = (animation==null ? null:animation.clone()); this.takeable = true; this.useable = false; this.equipable = false; this.useUponTake = false; this.attackBonus = 0; this.defenseBonus = 0; this.magicMultiplier = 1; this.value = 1; // value in gold coins of the object initAIRules(this); this.effect = function(character, map, game) { fireEventRule(this, "use"); } this.getClassName = function() { return "Item"; } } Food.prototype = new Item("Food",null); function Food(name, animation, value) { this.name = name; this.currentAnimation = ANIM_IDLE; this.animations = {}; this.animations[ANIM_IDLE] = (animation==null ? null:animation.clone()); this.takeable = true; this.useable = true; this.equipable = false; this.burrowed = false; this.value = value; initAIRules(this); this.effect = function(character, map, game) { if (character.player_character) game.pushMessage("Nah, I am not very hungry..."); } this.getClassName = function() { return "Food"; } } Spade.prototype = new Item("Spade",null); function Spade(animation, value) { this.value = value; this.currentAnimation = ANIM_IDLE; this.animations = {}; this.animations[ANIM_IDLE] = (animation==null ? null:animation.clone()); this.takeable = true; this.useable = true; this.equipable = false; this.burrowed = false; initAIRules(this); this.effect = function(character, map, game) { if (map.canDig(character.x,character.y)) { var object = map.getBurrowed(character.x,character.y); if (object!=null) { object.burrowed = false; game.pushMessage("You unearthed a " + object.name + "!"); } else { game.pushMessage("You dig a hole, but find nothing..."); } } else { game.pushMessage("You cannot dig a hole here!"); } } this.getClassName = function() { return "Spade"; } } Key.prototype = new Item("Key",null); function Key(name, ID, animation) { this.currentAnimation = ANIM_IDLE; this.animations = {}; this.animations[ANIM_IDLE] = (animation==null ? null:animation.clone()); this.name = name; this.keyID = ID; this.takeable = true; this.useable = false; this.equipable = false; this.value = 0; // items with value = 0 cannot be sold initAIRules(this); this.getClassName = function() { return "Key"; } } Chest.prototype = new Item("Chest",null); function Chest(name, itemInside, animation) { this.currentAnimation = ANIM_IDLE; this.animations = {}; this.animations[ANIM_IDLE] = (animation==null ? null:animation.clone()); this.name = name; this.takeable = true; this.useable = true; this.equipable = false; this.itemInside = itemInside; this.value = 0; // items with value = 0 cannot be sold initAIRules(this); this.effect = function(character, map, game) { fireEventRule(this, "open"); character.inventory.splice(character.inventory.indexOf(this),1); character.inventory.push(this.itemInside); game.pushMessage("You open the chest and find a... " + this.itemInside.name); if (this.itemInside.useUponTake) itemInside.effect(character,map,game); } this.getClassName = function() { return "Chest"; } } ItemHPotion.prototype = new Item(null, null); function ItemHPotion(bonus, animation) { this.currentAnimation = ANIM_IDLE; this.animations = {}; this.animations[ANIM_IDLE] = (animation==null ? null:animation.clone()); this.name = "H.Potion+" + bonus; this.useable = true; this.bonus = bonus; this.value = 5 + bonus*2; initAIRules(this); this.effect = function(character, map, game) { if (character.max_hp > character.hp) { character.hp += this.bonus; if (character.hp>character.max_hp) character.hp = character.max_hp; character.inventory.splice(character.inventory.indexOf(this),1); game.pushMessage("You feel healthier..."); } else { game.pushMessage("Nah... It would be a waste"); } }; this.getClassName = function() { return "ItemHPotion"; } } ItemMPotion.prototype = new Item(null, null); function ItemMPotion(bonus, animation) { this.currentAnimation = ANIM_IDLE; this.animations = {}; this.animations[ANIM_IDLE] = (animation==null ? null:animation.clone()); this.name = "M.Potion+" + bonus; this.useable = true; this.bonus = bonus; this.value = 5 + bonus*2; initAIRules(this); this.effect = function(character, map, game) { if (character.max_mp > character.mp) { character.mp += this.bonus; if (character.mp>character.max_mp) character.mp = character.max_mp; character.inventory.splice(character.inventory.indexOf(this),1); game.pushMessage("Magic is flowing through your veins..."); } else { game.pushMessage("Nah... It would be a waste"); } }; this.getClassName = function() { return "ItemMPotion"; } } ItemXPPotion.prototype = new Item(null, null); function ItemXPPotion(bonus, animation) { this.currentAnimation = ANIM_IDLE; this.animations = {}; this.animations[ANIM_IDLE] = (animation==null ? null:animation.clone()); this.name = "XP.Potion+" + bonus; this.useable = true; this.bonus = bonus; this.value = 10 + bonus*4; initAIRules(this); this.effect = function(character, map, game) { character.experienceGain(this.bonus, map); character.inventory.splice(character.inventory.indexOf(this),1); game.pushMessage("You feel wiser..."); }; this.getClassName = function() { return "ItemXPPotion"; } } ItemCoinPurse.prototype = new Item("", null); function ItemCoinPurse(gold, animation) { this.currentAnimation = ANIM_IDLE; this.animations = {}; this.animations[ANIM_IDLE] = (animation==null ? null:animation.clone()); this.name = gold + " gold purse"; this.gold = gold; this.useUponTake = true; this.value = gold; this.animations[this.currentAnimation].reset(); initAIRules(this); this.effect = function(character, map, game) { character.gold += this.gold; character.inventory.splice(character.inventory.indexOf(this),1); game.pushMessage("You got " + this.gold + " gold coins."); }; this.getClassName = function() { return "ItemCoinPurse"; } } EquipableItem.prototype = new Item(null, null); function EquipableItem(name, animation, itemType, attackBonus, defenseBonus, magicMultiplier, canChop, value) { this.name = name; this.currentAnimation = ANIM_IDLE; this.animations = {}; this.animations[ANIM_IDLE] = (animation==null ? null:animation.clone()); this.itemType = itemType; this.equipable = true; this.attackBonus = attackBonus; this.defenseBonus = defenseBonus; this.magicMultiplier = magicMultiplier; this.canChop = canChop; this.value = value; initAIRules(this); this.getClassName = function() { return "EquipableItem"; } } Scroll.prototype = new Item("Scroll"); function Scroll(name, spellInside, value, animation) { this.currentAnimation = ANIM_IDLE; this.animations = {}; this.animations[ANIM_IDLE] = (animation==null ? null:animation.clone()); this.name = name; this.takeable = true; this.useable = true; this.equipable = false; this.spellInside = spellInside; this.value = value; initAIRules(this); this.effect = function(character, map, game) { if (character.spells.indexOf(this.spellInside)!=-1) { game.pushMessage("You already know that spell..."); } else { character.inventory.splice(character.inventory.indexOf(this),1); character.spells.push(this.spellInside); game.pushMessage("You have learned the spell " + this.spellInside); } } this.getClassName = function() { return "Scroll"; } } /* * ------------------------ DOORS AND SWITCHES ------------------------ */ Lever.prototype = new Object(); function Lever(name, state, animation1, animation2) { this.name = name; this.currentAnimation = state; this.animations = {}; this.animations[true] = (animation1==null ? null:animation1.clone()); this.animations[false] = (animation2==null ? null:animation2.clone()); this.state = state; initAIRules(this); this.effect = function(map) { // console.log("Running lever effect!!"); fireEventRule(this, "use"); if (this.state) { this.state = false; fireEventRule(this, "deactivate"); } else { this.state = true; fireEventRule(this, "activate"); } // look for doors to operate: var doors = map.getDoorsWithID(this.name); // console.log("Found " + doors.length + " doors to operate with ID " + this.name); for(var i = 0;i=0); x1 = this.x + 1; do { door = map.getDoor(x1,this.y); if (door!=null && door.name == this.name) { door.setStateNonPropagating(state,map); } else break; x1++; }while(x1=0); y1 = this.y + 1; do { door = map.getDoor(this.x,y1); if (door!=null && door.name == this.name) { door.setStateNonPropagating(state,map); } else break; y1++; }while(y1=this.walk_speed) { this.state = STATE_READY; if (this.duration<=0) return false; } } return true; } this.draw = function(alpha,offsetx,offsety,game) { switch(this.state) { case STATE_MOVING: var offs = Math.floor(TILE_HEIGHT*(this.walk_speed-this.state_timer)/this.walk_speed); switch(this.direction) { case DIRECTION_UP: this.animations[this.currentAnimation].draw(this.x*TILE_WIDTH + offsetx, this.y*TILE_HEIGHT + offs + offsety,alpha,game); break; case DIRECTION_RIGHT: this.animations[this.currentAnimation].draw(this.x*TILE_WIDTH - offs + offsetx,this.y*TILE_HEIGHT + offsety,alpha,game); break; case DIRECTION_DOWN: this.animations[this.currentAnimation].draw(this.x*TILE_WIDTH + offsetx,this.y*TILE_HEIGHT - offs + offsety,alpha,game); break; case DIRECTION_LEFT: this.animations[this.currentAnimation].draw(this.x*TILE_WIDTH + offs + offsetx,this.y*TILE_HEIGHT + offsety,alpha,game); break; } break; default: this.animations[this.currentAnimation].draw(this.x*TILE_WIDTH + offsetx, this.y*TILE_HEIGHT + offsety,alpha,game); break; } } this.getClassName = function() { return "Spell"; } } /* * ------------------------ VEHICLES ------------------------ */ Vehicle.prototype = new Object(); function Vehicle(name) { this.name = name; this.canSwim = false; this.state_timer = 0; this.state = STATE_READY; this.direction = DIRECTION_RIGHT; this.animations = {}; this.currentAnimation = null; this.animation_speed = 16; this.walk_speed = 16; initAIRules(this); this.effect = function(map) { } this.update = function(character, map, game) { if (this.currentAnimation!=null) { this.animations[this.currentAnimation].update(); this.width = this.animations[this.currentAnimation].getWidth(); this.height = this.animations[this.currentAnimation].getHeight(); } switch(this.state) { case STATE_READY: if (this.state_timer==0) this.currentAnimation = ANIM_IDLE + ANIM_DIRECTIONS[this.direction]; this.state_timer++; break; case STATE_MOVING: if (this.state_timer==0) { this.currentAnimation = ANIM_MOVING + ANIM_DIRECTIONS[this.direction]; if (this.animations[this.currentAnimation]==null) this.currentAnimation = ANIM_IDLE + ANIM_DIRECTIONS[this.direction]; } this.state_timer++; if (this.state_timer>=this.walk_speed) { this.state_timer=0; this.state = STATE_READY; } break; default: break; } return Vehicle.prototype.update.call(this, character, map, game); } this.draw = function(alpha,offsetx,offsety,game) { if (this.currentAnimation!=null) { switch(this.state) { case STATE_MOVING: var offs = Math.floor(TILE_HEIGHT*(this.walk_speed-this.state_timer)/this.walk_speed); switch(this.direction) { case DIRECTION_UP: this.animations[this.currentAnimation].draw(this.x*TILE_WIDTH + offsetx, this.y*TILE_HEIGHT + offs + offsety,alpha,game); break; case DIRECTION_RIGHT: this.animations[this.currentAnimation].draw(this.x*TILE_WIDTH - offs + offsetx,this.y*TILE_HEIGHT + offsety,alpha,game); break; case DIRECTION_DOWN: this.animations[this.currentAnimation].draw(this.x*TILE_WIDTH + offsetx,this.y*TILE_HEIGHT - offs + offsety,alpha,game); break; case DIRECTION_LEFT: this.animations[this.currentAnimation].draw(this.x*TILE_WIDTH + offs + offsetx,this.y*TILE_HEIGHT + offsety,alpha,game); break; } break; default: this.animations[this.currentAnimation].draw(this.x*TILE_WIDTH + offsetx, this.y*TILE_HEIGHT + offsety,alpha,game); break; } } } this.getClassName = function() { return "Vehicle"; } }