Player erbt nun von Static Object, alle von Static Object erbenen Klassen nutzen nun den Classloader

This commit is contained in:
andreas
2014-11-25 23:20:27 +01:00
parent dce25e690b
commit 4f9579166b
4 changed files with 44 additions and 102 deletions
@@ -41,6 +41,9 @@ public class ImageLoader {
case 'r': case 'r':
imageFileName="de/heatup/resources/images/little_green_guy_verysmall.png"; imageFileName="de/heatup/resources/images/little_green_guy_verysmall.png";
break; break;
case 'p':
imageFileName="de/heatup/resources/images/little_blue_guy_verysmall.png";
break;
default: default:
imageFileName="de/heatup/resources/images/dummy_image.png"; imageFileName="de/heatup/resources/images/dummy_image.png";
break; break;
@@ -1,16 +1,11 @@
package de.heatup.objects; package de.heatup.objects;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.Random; import java.util.Random;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import de.heatup.cfg.Config;
import de.heatup.logging.Logger;
import de.heatup.mapengine.CollisionHandler; import de.heatup.mapengine.CollisionHandler;
import de.heatup.mapengine.MapGrid;
import de.heatup.mapengine.MapGridCell;
import de.heatup.ui.Main; import de.heatup.ui.Main;
public class Opponent extends Player { public class Opponent extends Player {
+8 -46
View File
@@ -1,27 +1,14 @@
package de.heatup.objects; package de.heatup.objects;
import java.awt.BasicStroke;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point; import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JComponent;
import de.heatup.cfg.Config; import de.heatup.cfg.Config;
import de.heatup.logging.Logger;
import de.heatup.mapengine.CollisionHandler; import de.heatup.mapengine.CollisionHandler;
public class Player extends JComponent {
// TODO: Magische Nummern entfernen
public class Player extends StaticObject {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
protected BufferedImage bf1;
protected Graphics2D g2d;
private double rescaleFactorX = 0.0;
private double rescaleFactorY = 0.0;
public boolean startMove = false; public boolean startMove = false;
public boolean animate = false; public boolean animate = false;
protected int speedMultiplicator = Config.initialSpeedMultiplicator; protected int speedMultiplicator = Config.initialSpeedMultiplicator;
@@ -35,8 +22,9 @@ public class Player extends JComponent {
private Point currentGridPosition; private Point currentGridPosition;
public Player() { public Player() {
super('p', new Point());
CollisionHandler.addMovingObject(this); CollisionHandler.addMovingObject(this);
if (bf1 == null) { if (super.bf1 == null) {
loadBufferedImage(); loadBufferedImage();
} }
} }
@@ -61,37 +49,13 @@ public class Player extends JComponent {
public Point getGridLocation() { public Point getGridLocation() {
return currentGridPosition; return currentGridPosition;
} }
public void paintComponent(Graphics g) {
g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.drawImage(bf1, 0, 0, this);
g2d.setStroke(new BasicStroke(8.0f));
g2d.finalize();
}
public void spawnAt(Point spawnPoint) { public void spawnAt(Point spawnPoint) {
this.currentGridPosition = spawnPoint; this.currentGridPosition = spawnPoint;
this.setAutoBounds(spawnPoint.x*25, spawnPoint.y*25); super.setAutoBounds(spawnPoint.x*25, spawnPoint.y*25);
this.repaint(); this.repaint();
} }
protected void loadBufferedImage() {
try {
this.bf1 = ImageIO.read(this.getClass().getClassLoader().getResourceAsStream("de/heatup/resources/images/little_blue_guy_verysmall.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
public void setImage(BufferedImage image) {
this.bf1 = image;
}
public void setAutoBounds(int x, int y) {
this.setBounds(x, y, this.bf1.getWidth(), this.bf1.getHeight());
}
public void step(char d, int i) { public void step(char d, int i) {
this.direction = d; this.direction = d;
this.i = i; this.i = i;
@@ -120,10 +84,8 @@ public class Player extends JComponent {
} }
if (stepCounter <= 0) { if (stepCounter <= 0) {
stepCounter = 25; stepCounter = 25;
//debug
Logger.write("on grid - x: "+getGridLocation().x + " y: "+getGridLocation().y);
} }
} }
startMove = false; startMove = false;
} }
} }
+33 -51
View File
@@ -1,104 +1,86 @@
package de.heatup.objects; package de.heatup.objects;
import java.awt.BasicStroke;
import java.awt.Graphics; import java.awt.Graphics;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.Point; import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JComponent; import javax.swing.JComponent;
import de.heatup.cfg.Config; import de.heatup.cfg.Config;
import de.heatup.ui.PanelBuilder; import de.heatup.ui.PanelBuilder;
/*
* Draft for static objects
*/
public class StaticObject extends JComponent { public class StaticObject extends JComponent {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
//private Image image; protected BufferedImage bf1;
private BufferedImage bf1; protected Graphics2D g2d;
private Graphics2D g2d;
private boolean doRescale = false; private boolean doRescale = false;
private double rescaleFactorX = 0.0; private double rescaleFactorX = 0.0;
private double rescaleFactorY = 0.0; private double rescaleFactorY = 0.0;
private boolean scale = false;
private Point location; protected Point currentGridPosition;
private char staticObjectType; private char staticObjectType;
private int staticObjectWidth; private int staticObjectWidth;
private int staticObjectHeight; private int staticObjectHeight;
public StaticObject(char staticObjectType, Point location, int staticObjectWidth, int staticObjectHeight) { public StaticObject(char staticObjectType, Point location, int staticObjectWidth, int staticObjectHeight) {
this.staticObjectType = staticObjectType; this.staticObjectType = staticObjectType;
this.location = location;
this.staticObjectWidth = staticObjectWidth; this.staticObjectWidth = staticObjectWidth;
this.staticObjectHeight = staticObjectHeight; this.staticObjectHeight = staticObjectHeight;
loadBufferedImage(); loadBufferedImage();
setAutoBounds(location.x, location.y); setAutoBounds(location.x, location.y, staticObjectWidth, staticObjectHeight);
} }
public StaticObject(char staticObjectType, Point location) { public StaticObject(char staticObjectType, Point location) {
this(staticObjectType, location, Config.singleTilePixels, Config.singleTilePixels); this(staticObjectType, location, Config.singleTilePixels, Config.singleTilePixels);
} }
public void paintComponent(Graphics g) {
public void paintComponent(Graphics g) {
this.g2d = (Graphics2D)g; this.g2d = (Graphics2D)g;
//this.image = Toolkit.getDefaultToolkit().getImage("src/de/heatup/resources/images/little_green_guy.png"); this.g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
if ( bf1 == null ) { //this.g2d.setStroke(new BasicStroke(8.0f));
if ( this.bf1 == null ) {
loadBufferedImage(); loadBufferedImage();
} }
//g2d.rotate(0.2, 10, 10);
/*
// TODO: Check if we can store a rescaled image in bf1??? * Skalieren der Bilder wenn größer als Bounds
// if ( doRescale ) { */
// System.out.println("Hit: " + rescaleFactorX + " " + rescaleFactorY); if ( scale ) {
g2d.scale(2, 2); this.g2d.scale(((double)this.getSize().width/(double)bf1.getWidth()) , ((double)this.getSize().height/(double)bf1.getHeight()));
// } }
g2d.drawImage(bf1, 0, 0, this); this.g2d.drawImage(bf1, 0, 0, this);
g2d.finalize(); this.g2d.finalize();
} }
private void loadBufferedImage() {
protected void loadBufferedImage() {
this.bf1 = PanelBuilder.getImageLoader().getBufferedImage(this.staticObjectType); this.bf1 = PanelBuilder.getImageLoader().getBufferedImage(this.staticObjectType);
} }
public void setImage(BufferedImage image) { public void setImage(BufferedImage image) {
this.bf1 = image; this.bf1 = image;
} }
private void autoRescale() {
// TODO: get original width and hight of bf1
// Calculate factor new/original
// Rescale image using factor
// Save new bf in bf1 ???
this.rescaleFactorX = (double)this.staticObjectWidth / (double)this.bf1.getWidth();
this.rescaleFactorY = (double)this.staticObjectHeight / (double)this.bf1.getHeight();
//System.out.println("Object: " + this.staticObjectType + "\tBuffered Image: Height: " + this.bf1.getHeight() + "\t Width: " + this.bf1.getWidth() + "\tStatic Object: Height: " + this.staticObjectHeight + "\t Width: " + this.staticObjectWidth);
if (this.rescaleFactorX != 1 || this.rescaleFactorY != 1 ) {
this.doRescale = true;
}
}
// TODO: getter für höhe breite position
public void setAutoBounds(int x, int y) { public void setAutoBounds(int x, int y) {
//this.setBounds(x, y, this.bf1.getWidth(), this.bf1.getHeight());
this.setSize(this.bf1.getWidth(), this.bf1.getHeight()); this.setSize(this.bf1.getWidth(), this.bf1.getHeight());
this.setLocation(x, y); this.setLocation(x, y);
} }
public void setAutoBounds(int x, int y, int width, int height) {
this.setSize(width, height);
this.setLocation(x, y);
if ( ((bf1.getWidth() != this.getSize().width ) || (bf1.getHeight() != this.getSize().height)) ) {
scale = true;
}
}
public String getDebugInfo() { public String getDebugInfo() {
String info = "Debug: " + "Rescale? " + this.doRescale + "\tScale X:" + this.rescaleFactorX + "\tScale Y: " + this.rescaleFactorY + "\tsH: " + this.staticObjectHeight + "\tsW: " + this.staticObjectWidth + "\tStaticObjectType: " + this.staticObjectType + "\tbW : " + this.bf1.getWidth() + "\tbH :" + this.bf1.getHeight(); String info = "Debug: " + "Rescale? " + this.doRescale + "\tScale X:" + this.rescaleFactorX + "\tScale Y: " + this.rescaleFactorY + "\tsH: " + this.staticObjectHeight + "\tsW: " + this.staticObjectWidth + "\tStaticObjectType: " + this.staticObjectType + "\tbW : " + this.bf1.getWidth() + "\tbH :" + this.bf1.getHeight();
return info; return info;
} }
} }