package com.game.roguelikeengine; /* Copyright (C) 2013 Ferran Fabregas (ferri.fc@gmail.com) This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.Random; import javax.imageio.ImageIO; public class Hero { private int agility; private int force; private int relative_x_tile; private int relative_y_tile; private String name; private int life; private int level; private int exp; private int resist; private int current_sprite_position; // sprite that will be shown private BufferedImage sprite; private Object head; private Object lefthand; private Object righthand; private Object body; private Object foot; public Hero(String name,String file) { // initial set-up this.agility=4; this.force=5; // offense this.resist=3; // defense this.life=100; // hp this.exp=1; // experience this.relative_y_tile=1; this.relative_x_tile=1; this.name=name; init_sprite_pos(); // initial sprite position, every number corresponds to a sprite. Each set of sprites has different configuration this.head = new Object(); // empty object this.body = new Object(); // empty object this.lefthand = new Object(); // empty object this.righthand = new Object(); // empty object this.foot = new Object(); // empty object try { // hero this.sprite=ImageIO.read(new File(file)); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // hero gets public String getname() { return this.name; } public int getexperience() { return this.exp; } public int getresist() { return this.resist+this.head.getdefense()+this.lefthand.getdefense()+this.righthand.getdefense()+this.body.getdefense()+this.foot.getdefense()+((int)(this.exp/GameEngine.EXPERIENCE_NEXT_LEVEL_LIMIT)); } public int getforce() { return this.force+this.head.getattack()+this.lefthand.getattack()+this.righthand.getattack()+this.body.getattack()+this.foot.getattack()+((int)(this.exp/GameEngine.EXPERIENCE_NEXT_LEVEL_LIMIT)); } public int getagility() { return this.agility+((int)(this.exp/GameEngine.EXPERIENCE_NEXT_LEVEL_LIMIT)); } public int gethp() { return this.life; } public int getlevel() { return this.level; } public int getrelativextile() { return this.relative_x_tile; } public int getrelativeytile() { return this.relative_y_tile; } public Object gethead() { return this.head; } public Object getlefthand() { return this.lefthand; } public Object getrighthand() { return this.righthand; } public Object getbody() { return this.body; } public Object getfoot() { return this.foot; } public BufferedImage getimage() { return this.sprite; } // hero sets / updates public void sethead(Object obj) { this.head=obj; } public void setlefthand(Object obj) { this.lefthand=obj; } public void setrighthand(Object obj) { this.righthand=obj; } public void setbody(Object obj) { this.body=obj; } public void setfoot(Object obj) { this.foot=obj; } public void decayhead() { if (gethead().getname()!=null) { // if object exist gethead().reducedurability(1); } } public void decaylefthand() { if (getlefthand().getname()!=null) { // if object exist getlefthand().reducedurability(1); } } public void decayrighthand() { if (getrighthand().getname()!=null) { // if object exist getrighthand().reducedurability(1); } } public void decaybody() { if (getbody().getname()!=null) { // if object exist getbody().reducedurability(1); } } public void decayfoot() { if (getfoot().getname()!=null) { // if object exist getfoot().reducedurability(1); } } public void randomdecay() { Random randomGenerator = new Random(); int object_2_decay = randomGenerator.nextInt(5); switch (object_2_decay) { case 0: decayfoot(); break; case 1: decayhead(); break; case 2: decaylefthand(); break; case 3: decayrighthand(); break; case 4: decaybody(); break; } } public void setrelativextile(int value) { this.relative_x_tile=value; } public void setrelativeytile(int value) { this.relative_y_tile=value; } public void updateagility(int value) { this.agility=this.agility+value; } public void updateexperience(int value) { this.exp=this.exp+value; } public void updatehp(int value) { this.life=this.life+value; } public void updatelevel() { this.level++; } public void updateforce(int value) { this.force=this.force+value; } public void updateresist(int value) { this.resist=this.resist+value; } // hero position updates public void up() { if (this.relative_y_tile>0) { this.relative_y_tile -= 1; sprite_goup(); } } public void down() { if (this.relative_y_tile<GameEngine.ON_SCREEN_TILES_Y-1) { this.relative_y_tile += 1; sprite_godown(); } } public void left() { if (this.relative_x_tile>0) { this.relative_x_tile -= 1; sprite_goleft(); } } public void right() { if (this.relative_x_tile<GameEngine.ON_SCREEN_TILES_X-1) { this.relative_x_tile += 1; sprite_goright(); } } public void scrollup() { this.relative_y_tile=GameEngine.ON_SCREEN_TILES_Y-1; } public void scrolldown() { this.relative_y_tile=0; } public void scrollleft() { this.relative_x_tile=GameEngine.ON_SCREEN_TILES_X-1; } public void scrollrigth() { this.relative_x_tile=0; } // fight // hero hit enemy public String hit(Enemy enemy) { //System.out.println("enemy force:"+enemy.getforce()); //System.out.println("enemy agility:"+enemy.getagility()); //System.out.println("enemy agility ceil:"+(int)(Math.ceil(((double)enemy.getagility()/(double)3)))); //System.out.println("hero resist:"+this.resist); Random randomGenerator = new Random(); int herohit= (((int)(Math.ceil((double)((double)this.agility/(double)3)))*this.force)-enemy.getresist()); int enemyhit=(((int)(Math.ceil((double)((double)enemy.getagility()/(double)3)))*enemy.getforce())-this.resist); int heromodifier = randomGenerator.nextInt(this.agility); // random component based on agility int enemymodifier = randomGenerator.nextInt(enemy.getagility()); // random component based on agility herohit=herohit+heromodifier; enemyhit=enemyhit+enemymodifier; if (herohit<0) { herohit=0; } enemy.updatehp(herohit); // hero hits enemy randomdecay(); // durability decreases if (enemy.gethp()>0) { // if enemy is alive if (enemyhit<0) { enemyhit=0; } this.life=this.life-enemyhit; // enemy hits hero randomdecay(); // durability decreases } // return result control if (enemy.gethp()<=0) { return "ENEMYDEAD"; } if (this.life<=0) { return "HERODEAD"; } return this.name+" deal "+herohit+" damage points to "+enemy.getname()+"\nand "+enemy.getname()+" deal "+enemyhit+" damage points to "+this.name; } // *** BEGIN hero sprite management. you MUST modify it with your own sprite behavior private void init_sprite_pos() { current_sprite_position=7; } private void sprite_goup() { switch(current_sprite_position) { case 10: current_sprite_position=11; break; case 11: current_sprite_position=12; break; case 12: current_sprite_position=10; break; default: current_sprite_position=10; break; } } private void sprite_godown() { switch(current_sprite_position) { case 1: current_sprite_position=2; break; case 2: current_sprite_position=3; break; case 3: current_sprite_position=1; break; default: current_sprite_position=1; break; } } private void sprite_goleft() { switch(current_sprite_position) { case 4: current_sprite_position=5; break; case 5: current_sprite_position=6; break; case 6: current_sprite_position=4; break; default: current_sprite_position=4; break; } } private void sprite_goright() { switch(current_sprite_position) { case 7: current_sprite_position=8; break; case 8: current_sprite_position=9; break; case 9: current_sprite_position=7; break; default: current_sprite_position=7; break; } } public int getyspriteposition() { if (current_sprite_position==1 || current_sprite_position==2 || current_sprite_position==3) { return GameEngine.TILE_Y_SIZE*0; } if (current_sprite_position==4 || current_sprite_position==5 || current_sprite_position==6) { return GameEngine.TILE_Y_SIZE*1; } if (current_sprite_position==7 || current_sprite_position==8 || current_sprite_position==9) { return GameEngine.TILE_Y_SIZE*2; } if (current_sprite_position==10 || current_sprite_position==11 || current_sprite_position==12) { return GameEngine.TILE_Y_SIZE*3; } return 0; } public int getxspriteposition() { if (current_sprite_position==1 || current_sprite_position==4 || current_sprite_position==7 || current_sprite_position==10 ) { return GameEngine.TILE_X_SIZE*0; } if (current_sprite_position==2 || current_sprite_position==5 || current_sprite_position==8 || current_sprite_position==11) { return GameEngine.TILE_X_SIZE*1; } if (current_sprite_position==3 || current_sprite_position==6 || current_sprite_position==9 || current_sprite_position==12) { return GameEngine.TILE_X_SIZE*2; } return 0; } // *** END hero sprite management. you MUST modify it with your own sprite behavior }