
/* ------------------------------
   gMe is the character class.  Derives from gTile;
  
   ------------------------------ */
function gMe() {
  this.image='man.jpg';   //default image for character
  this.zIndex=100;
  this.object_type="gMe";
  this.title="You";
  this.descrip = "This is you.  An ugly mug.";
  this.__proto__=gMe.prototype;
  
  //act function for stuff the hero can't control, such as healing, mutations etc
  this.act=function() {
    //heal wounds
    this.heal();
  }
  
  this.die=function() {
    msg("You die...");
    end_game();
  }

  /* override to save pack and armour */
  this.save=function() {
    var o=this.save_default("gMe");
    var pids=new Array();
    for(i in this.pack) { pids.push(this.pack[i].id); }
    o.push(pids);

    var wids=new Object();
    for(i in this.worn) { 
      wids[i]=this.worn[i].id;
    }
    o.push(wids);

    this.clean_events();
    var eids=new Array();
    for(i in this.events) { 
      eids.push(this.events[i].id);
    }
    o.push(eids);
    return o;
  }

  /* override to load pack  and armour*/
  this.load=function(o) {
    this.load_default(o,"gMe");
    this.pack_ids=o.shift();
    this.worn_ids=o.shift();
    this.events_ids=o.shift();
    if (this.pack_ids==null) this.pack_ids=new Array();
    if (this.worn_ids==null) this.worn_ids=new Object();
    if (this.events_ids==null) this.events_ids=new Array();
  }

  this.do_attack=function(o,dir) {
    if (o==null) {
      if (dir==null) return;
      //find the object to attack
      if (Math.abs(dir.delta.x) > 1 || Math.abs(dir.delta.y) > 1) {
	msg("You can't reach that far.");
	return false;
      }
      
      var o_list=g.gm.tilesAt(dir.x,dir.y);
      
      for (var i in o_list) {
	if (o_list[i].has_func("attack")) {
	  o=o_list[i];
	  break;
	}
      }
    }

    if (o==null) {
      msg("You swing at thin air.");
      return true;
    }

    if (o.hostile==false) {
      msg("You turn on the poor " + o.title + "!");
    }
    
    var res=gMe.prototype.do_attack.call(this,o);
    if (res=="kill") {
      //gain experience
      this.xp += o.xp;
      if (this.xp >= this.next_level_xp) {
	this.gain_level();
      }
    }
  }

  this.elapse=function(ticks) {
    g.elapse(ticks);
  }

  this.move=function(xoffset,yoffset) {
    if(!this.onMap) return false;
    
    var new_x=this.x + parseInt(xoffset);
    var new_y=this.y + parseInt(yoffset);

    if (new_x >= g.gm.ar_width || new_x < 0) return false;
    if (new_y >= g.gm.ar_height || new_y < 0) return false;

    var o_list=g.gm.tilesAt(this.x+parseInt(xoffset), this.y+parseInt(yoffset));
    for (var i in o_list) {
      var o=o_list[i];
      if (!o.walk(this,xoffset,yoffset)) {
	//add message here about bumping
	return false;
      }
    }

    if (!g.gm.moveTile(this,xoffset,yoffset))
    return false;

    g.elapse(this.speed);
    return true;
  }

  this.action_pickup=function() {
    var o_list=g.gm.tilesAt(this.x,this.y);
    for (var i in o_list) {
      var o=o_list[i];
      if (o.has_func("pickup")) { 
	this.do_pickup(o);
	return;

      }
    }
    msg("Nothing here to pick up.");
  }

  this.action_drop=function() {
    if(this.pack.length==0) {
      msg("You have nothing to drop.");
      return;
    }
    this.do_drop(this.pack[0]);
  }

  this.action_enter=function() {
    var o_list=g.gm.tilesAt(this.x,this.y);

    for (var i in o_list) {
      var o=o_list[i];
      if (o.has_func("enter")) {
	this.do_enter(o);
	return true;
      }
    }
    msg("There's nothing here to enter.");
    return false;
  }

  this.action_talk=function(xoffset,yoffset) {
    
  }
}

gMe.prototype=new gBeing;
gMe.prototype.savedVars = ["save_x","save_y","mod"];

gMe_protos = {
  male: {  image: 'man.jpg', strength: 16 },
  wizard: {  image: 'wizard.jpg', strength: 13 },
  warrior: {  image: 'warrior.jpg', strength: 18 },
  thief: {  image: 'thief.jpg', strength: 14 },
  elf: {  image: 'elf.jpg', strength: 14 },
  guy: {  image: 'guy.jpg', strength: 15 }
};
