Now all you need is to make a map out of ascii characters.
That way you can give the player an objective to get to and give the shop a physical location.
Hmmm
(http://www.getsome.co.nz/attachment.php?attachmentid=7768&stc=1&d=1361773141)
import java.util.ArrayList;
import java.util.Random;
import java.awt.*;
import javax.swing.*;
import javax.swing.ImageIcon;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Canvas;
import java.awt.Dimension;
import javax.swing.WindowConstants;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import java.awt.image.BufferStrategy;
import java.awt.Color;
import java.awt.Graphics2D;
import javax.swing.ImageIcon;
import java.util.ArrayList;
/**
A test of using a canvas and Key Listener to navigate around the Naruto Map
* @author Tiwa
* @version mon 27 aug 2012
@Wed Oct 17 07:51:47 NZDT 2012 <- moved to RogueLike
@Wed Oct 17 20:30:06 NZDT 2012 <- test complete: Monospace enabled
*/
public class TestMapPanel extends Canvas implements KeyListener{
protected final int WIDTH=500;
protected final int HEIGHT=500;
protected final int OFFSET_WIDTH=200;
// protected final int WIDTH=600;
// protected final int HEIGHT=600;
protected final int barHeight=30;
protected final int tileSize=10;
private BufferStrategy strategy;
protected boolean gameRunning=true;
// protected final Travel travel=new Travel();
// protected final Tile[][]TILES;
// protected Tile myTile=new Tile();
// protected ImageIcon bgimage=new ImageIcon("narutomap.png");
protected final char[][]grid=new char[WIDTH/tileSize][WIDTH/tileSize];
// protected final char[][]grid=new char[5][5];
protected int playerX=0;
protected int playerY=0;
protected final int screenTop=10;
protected final Random rnd=new Random();
// protected String[]stats={"Name: Bob","Strength: 10","Speed: 5","Armour: 5","Weapon: Sword"};
protected boolean mv_blocked;
protected String blocked_message="Your way is blocked!";
public TestMapPanel(){
// WIDTH=bgimage.getIconWidth();
// HEIGHT=bgimage.getIconHeight();
JFrame frame=new JFrame("Tiwas Roguelike Game");
// frame.setUndecorated(true);
JPanel panel=(JPanel)frame.getContentPane();
panel.setPreferredSize(new Dimension(WIDTH+OFFSET_WIDTH,HEIGHT));
panel.setLayout(null);
setBounds(0,0,WIDTH+OFFSET_WIDTH,HEIGHT);
panel.add(this);
setIgnoreRepaint(true);
frame.pack();
frame.setResizable(false);
frame.setVisible(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
addKeyListener(this);
createBufferStrategy(2);
strategy=getBufferStrategy();
// myTile.setSize(tileSize);
// myTile.setX(0);
// myTile.setY(0);
// myTile.setType('L');
// myTile.addItem("Naruto");
// myTile.setImage(new ImageIcon("narutobody.jpg"));
// TILES[0][0]=myTile;
for(int x=0;x<grid.length;x++){
for(int y=0;y<grid[x].length;y++){
grid[x][y]='.';
}//4y
}//4x
grid[playerX][playerY]='@';
requestFocus();
gameLoop();
}//con
/**
Main Game Loop
*/
public void gameLoop(){
String[]stats={"Name: Bob","Strength: 10","Speed: 5","Armour: 5","Weapon: Sword"};
Font font = new Font("Monospaced", Font.BOLD, 14);
long lastLoopTime = System.currentTimeMillis();
while(gameRunning){
long delta = System.currentTimeMillis() - lastLoopTime;
lastLoopTime = System.currentTimeMillis();
Graphics2D g = (Graphics2D)strategy.getDrawGraphics();
g.setColor(Color.WHITE);
g.fillRect(0,0,WIDTH+OFFSET_WIDTH,HEIGHT);
// g.drawImage(bgimage.getImage(),0,0,WIDTH,HEIGHT,null);
g.setColor(Color.BLACK);
g.setFont(font);
int tileWidth=getWidth()/tileSize;
int tileHeight=getHeight()/tileSize;
for(int x=0;x<grid.length;x++){
/*
for(int y=0;y<TILES[x].length;y++){
g.drawRect(x*tileSize,y*tileSize,tileSize,tileSize);
// Tile tile=TILES[x][y];
char tile=grid[x][y];
if(tile!=null){
g.drawImage(ResizeImage.getImage(tile.getImage(),tileSize,tileSize).getImage(),x*tileSize,y*tileSize,null);
g.drawChars(tile,tileSize,tile.length,
drawChars(char[] data, int offset, int length, int x, int y)
Draws the text given by the specified character array, using this
graphics context's current font and color.
char[] c = { 'W', 'a', 't', 'c', 'h', ' ', 'i', 't', ' ', 'D','u',
'k', 'e', '!' };
g.drawChars(c, 0, c.length, 10, 30);
}//fi tile not null
}//y
*/
// g.drawChars(grid[x],0,grid[x].length,0,x*tileSize);
// g.drawChars(grid[x],1,grid[x].length,0,x*tileSize);//bad offset??
g.drawChars(grid[x],0,grid[x].length,0,x*tileSize+screenTop);
if(x<stats.length)g.drawString(stats[x],WIDTH-100,x*tileSize+screenTop);
}//x
if(mv_blocked)g.drawString(blocked_message,WIDTH-100,stats.length*tileSize+screenTop);
g.dispose();
strategy.show();
try {Thread.sleep(10);
}catch(Exception eek) {
//new Help(eek.toString());
// eek.printStackTrace();
JOptionPane.showMessageDialog(this,eek.toString(),"Error in main loop",JOptionPane.INFORMATION_MESSAGE);
}//do nothing
}//while game is running
System.exit(0);
}//method
/**
* The following methods are implemented by the Key Listener
*/
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
// movePlayer(playerX-1,playerY);
// movePlayer(playerX,playerY+1);
movePlayer(playerX,playerY-1);
}//if you press left
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
// movePlayer(playerX+1,playerY);
// movePlayer(playerX,playerY-1);
movePlayer(playerX,playerY+1);
}//if you press right
// if (e.getKeyCode() == KeyEvent.VK_DOWN && !pressD) {
if (e.getKeyCode() == KeyEvent.VK_DOWN){
// movePlayer(playerX,playerY+1);
// movePlayer(playerX-1,playerY);
movePlayer(playerX+1,playerY);
}//if you press right
if (e.getKeyCode() == KeyEvent.VK_UP) {
// movePlayer(playerX,playerY-1);
// movePlayer(playerX+1,playerY);
movePlayer(playerX-1,playerY);
}//if you press right
}//method
public void keyReleased(KeyEvent e) {
}//method
public void keyTyped(KeyEvent e) {
if (e.getKeyChar() == 27) {
// System.exit(0);
gameRunning=false;
}//if you press escape (keyChar 27)
}//method
protected void movePlayer(int x,int y){
grid[playerX][playerY]='.';
blocked_message="";
boolean blockedx=true;
boolean blockedy=true;
if(x>-1 && x<grid.length){
playerX=x;
blockedx=false;
}//fi x
if(y>-1 && y<grid.length){
playerY=y;
blockedy=false;
}//fi x
grid[playerX][playerY]='@';
if(!blockedx && !blockedy)mv_blocked=false;
else {
mv_blocked=true;
if(blocked_message.length()==0)blocked_message="Your way is blocked!";
}
// repaint();
}//me move player
// protected void checkCoordinate(int num,int
} // class TestMap
Tiwa is genius.
Actually: You're the genius here. I would never have thought of using the old school system of hit location in a fighting game like this. That is one of those 'Too Crazy To Work' situations.
My original idea with the body parts thing was to have the Ninjas be able to replace their body parts with stolen parts from other Ninja's, a surprisingly (http://naruto.wikia.com/wiki/Kakashi_Hatake) common (http://naruto.wikia.com/wiki/Ao) occurrence (http://naruto.wikia.com/wiki/Danzō_Shimura)
So: This whole locational damage thing is, not a new idea by any means, but poses a surprisingly structural dilemma (pun not intended)
"How the buggery farts do I display all this??"
The Book/Internet/Programming Answer: Use Objects and Methods
My Answer: One Ninja Body containing HashMap
+ One Array of Strings + Two HashMaps (NinjaBodyParts, JLabel)
This has actually solved all my problems with the whole Object -> Interface -> Display questions I have ever had.
(I've taken out the Accessor/Mutators, aka Getters and Setters, for brevity)
import java.util.HashMap;
public class NarutoNinja {
protected String name;
protected HashMap<String,NinjaBodyPart>body=new HashMap<String,NinjaBodyPart>();
protected HashMap<String,Jutsu>jutsus=new HashMap<String,Jutsu>();//These are a Ninjas Attacks, in this case "Left Punch","Left Kick", etc
public NarutoNinja(String n){
name=n;
}//c
}//class
Ninja Body Parts
import java.util.ArrayList;
public class NinjaBodyPart{
protected String name;
protected int health;
protected int MAX_HEALTH;
protected final ArrayListparts=new ArrayList();
public NinjaBodyPart(String n,int h){
name=n;
health=h;
MAX_HEALTH=h;
}//c
public String toString(){
return name;
}
public String getLabel(){
return String.format("%s: %d",name,health);
}
And the HashMap of JLabels in the Main Program/Display
protected final HashMaplblBody=new HashMap();
Now you iterate through the Ninja's Body Parts and add new JLabels for them
HashMapbody=kakashi.getBody();
Object[]keys=body.keySet().toArray();
for(int i=0;iString key=keys[i].toString();//one key now can access both HashMaps
JLabel lbl=new JLabel(body.get(key).getLabel());//makes text for the JLabel
lbl.setOpaque(true);//otherwise the background colour doesnt come through
lbl.setBackground(Color.green);//a nice healthy limb
lblBody.put(key,lbl);
}//for i
Spoiler contains code for processing attacks which is somewhat irrelevant (you can just use an int damage amount)
protected String processAttack(NarutoNinja ninja1,NarutoNinja ninja2,Jutsu attack){
NinjaBodyPart part=processPart(ninja2,attack);
Color col=Color.yellow;
String line=String.format("%s %ss %s in his %s for %d damage (%d Health)\n",ninja1.getName(),attack.getName(),ninja2.getName(),part.getName(),attack.getDamage(),part.getHealth());
int phealth=part.getHealth();
String damage="";
part.setHealth(phealth-attack.getDamage());
if(part.getHealth()damage="bruised";
}
if(part.getHealth()<0){
damage="dismembered";
col=Color.red;
destroyBodyParts(part,part.getHealth());
String[]dead={"Head","UpperTorso","LowerTorso"};
for(int i=0;iif(part.getName().equals(dead[i])){
sndKick.stop();
sndPunch.stop();
sndWin.play();
JOptionPane.showMessageDialog(
this,
"Congratulations! You Win!",
"Crombat Great Success",
JOptionPane.PLAIN_MESSAGE,
new ImageIcon("gaithumbsup.jpg")
);
}//fi
}//4 i
return line+=String.format("Kakashis %s is %s (%d Health)",part.getName(),damage,part.getHealth());
}//if part.getHealth()<0
if(part.getHealth()==0){
sndCrack.play();
damage="critically injured";
col=Color.WHITE;//gray doesnt show up properly here
}
if(part.getHealth()>=part.getMaxHealth()/2){
damage="damaged";
}
line+=String.format("Kakashis %s is %s (%d Health)",part.getName(),damage,part.getHealth());
lblBody.get(part.getName()).setText(part.getLabel());
if(col!=null){
lblBody.get(part.getName()).setBackground(col);
}
return line;
}//me
So: How do you destroy multiple parts when a limb is liberated from the body? With Recursion!
You probably dont need to use recursion here, but Im lazy.
protected void destroyBodyParts(NinjaBodyPart part,int h){
part.setHealth(h);
lblBody.get(part.getName()).setText(part.getLabel());
lblBody.get(part.getName()).setBackground(Color.red);
if(part.getParts().isEmpty())return;
for(int i=0;idestroyBodyParts(part.getParts().get(i),h);
}
}//me
And the buttons? All run from Reflection:
protected final String[]btnLabels={"Left Punch","Right Punch","Left Kick","Right Kick","Combo","Instructions"};
protected void createButtons(JPanel btnPanel){
for(int i=0;iJButton btn=new JButton(btnLabels[i]);
btn.setMargin(new Insets(0,0,0,0));
btn.setActionCommand(String.format("%s%s","btn",Library.removeSpaces(btnLabels[i])));
btn.addActionListener(this);
btnPanel.add(btn);
}//4
}
public void actionPerformed(ActionEvent ae){
try{
getClass().getDeclaredMethod(ae.getActionCommand()).invoke(this);
}//catch
catch(Exception e){
Library.getHelp(e.toString(),ae.getActionCommand());
}//catch
}//me action performed
And all you need is the methods that are named after the buttons e.g btnLeftPunch(),btnRightKick(),btnInstructions()
And thats it. Put some JPanels in to display the information and Thats all you need. When the Body Parts take damage you can use the name of the Body Part to access the Label displaying the information for the Body Part and update the display
What this whole HashMap display idea does is simplifies the reset (aka 'Healing') of a character down to one ridiculously simple method
In this case: ninja(0) is the Target and ninja(1) is the aggressor. So we reset the target like so
protected void reset(){
HashMapbody=ninjas.get(0).getBody();
for(Map.Entryentry:body.entrySet()){
NinjaBodyPart part=entry.getValue();//get the iterated body part from the Map
part.setHealth(part.getMaxHealth());//MAX_HEALTH is saved in the class from the initial health
JLabel lbl=lblBody.get(part.getName());//get the label associated with the body part
lbl.setText(part.getLabel());//reset the text of the label
lbl.setBackground(Color.GREEN);//nice healthy limb
}//4 iterate map of body parts
}//reset
Which is simplified further in the spoiler
protected void reset(){
for(Map.Entryentry:ninjas.get(0).getBody().entrySet()){
entry.getValue().setHealth(entry.getValue().getMaxHealth());
lblBody.get(entry.getValue().getName()).setText(entry.getValue().getLabel());
lblBody.get(entry.getValue().getName()).setBackground(Color.GREEN);
}
And Finally: If one of the Body Parts is a 'Principal Body Part'
protected final String[]PARTS_BODY={"Head","UpperTorso","LowerTorso"};
Then all you need to do is iterate this incredibly short list to see if the damaged part is one of these. If it is: Enemy is Killed
You could even turn this into a HashSet if you want and use PARTS_BODY.contains(part.getName())
Either Way:
(http://www.getsome.co.nz/attachment.php?attachmentid=7828&stc=1&d=1363947786)