Turn Based RPG Battle
This is a code mockup of what an RPG turn-based system might be able to achieve.
by Sam Fereday
HTML
<div id="stage"></div>
CSS
.red {
color: #EE0000;
}
JavaScript
/*
This example takes in to account a scoring system per head. This is taken from a couple of stack overflow examples that judged the enemies reaction to what each player state was at.
You might have some enemies search for the person with the lowest health. Or, those with the highest def. And perhaps randomly select between each. Either way, depending on the enemy, the score for each target will change. Higher targets yielding more enemy focus. A simple example of this is 'hate' in mmos.
So for now we'll use a simple example, using a timed iteration in that the player and the enemy will respond in turn to various situations. For now we'll base it on a simple state of who's done the most damage overall.
Things to add:
- The actual AI
- A simple level up system (auto assigned stats)
- Some fun little graphics just for shits and giggles
*/
var alldead = false;
var stage = document.getElementById("stage");
// So we can keep a record of what's on screen
var entities = [];
// A simple class (object) to represent a living entity
var Entity = function(){
entities.push(this);
return this;
};
Entity.prototype = {
el: "", // Or 2d / 3d obj when in actual game.
name: "",
maxHP: 0,
hp: 0,
str: 0,
def: 0,
flagKO: false,
friendly: false,
create: function(cont) {
this.el = document.createElement('div');
this.el.className += "entity " + this.name;
cont.appendChild(this.el);
this.updateStats();
return this;
},
setStats: function(obj){
_.each(obj, function(data, key, obj){
if(typeof this[key] !== "undefined") this[key] = data;
}, this);
if(this.hp > this.maxHP) this.maxHP = hp;
if(this.hp === 0) this.death();
return this;
},
updateStats: function() {
this.el.innerHTML = this.name + " HP: " + Math.round(this.hp) + "/" + Math.round(this.maxHP);
},
getStats: function(){
return this;
},
attack: function(t) {
//...