Show Posts
|
|
Pages: [1]
|
|
1
|
Game Development / Newbie & Debugging Questions / Given a collection of one class, get the objects of its child class...
|
on: 2012-10-25 06:32:34
|
|
First, I have a Fleet, which contains a collection of Ships (ArrayList<Ship>) of all different types (classes that extend Ship). Next, I have a Celestial object, which could be one of several different types (classes that extend Celestial).
Some types of Ships can harvest resources from one and only one type of Celestial each. For example, an OreShip (extends Ship) and only an OreShip can harvest from and only from an AsteroidBelt (extends Celestial). Furthermore, not all types of Ships can harvest (those that can do so implement the interface Harvests) and not all types of Celestials are harvestable (those that are implement the interface Harvestable).
Now, here is the problem. If the Fleet has access to a Celestial body that is an AsteroidBelt and contains at least one OreShip, I want to be able to order the Fleet to make its OreShips, and only the OreShips, harvest from the AsteroidBelt. How can I do this, preferably without a massive if/else switch, given the ArrayList<Ship>?
|
|
|
|
|
2
|
Game Development / Newbie & Debugging Questions / Text wrapping with AngelCodeFont: how do I do it?
|
on: 2012-03-04 20:19:08
|
UPDATE: Never mind, I figured this one out on my own. Thanks to advice I received in my thread on UnicodeFont, I decided to switch to AngelCodeFont and so far its working great. My problem now is how to do "text wrapping" (without it, my long Strings end up going off the screen). The Strings are pulled from an external .txt file and stored in an ArrayList<String>, from which they are pulled one line at a time and rendered to the screen. Also, I know the point at which text wrapping needs to be applied (when the width of the String is greater than 770 pixels). My question is how to implement text wrapping under these conditions. The code in question is shown below (this is not the entire class...), 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| public class VisualNovelState extends BasicGameState { private static final String FONTDEF = "data/font/32ptArial.fnt"; private static final String FONTIMG = "data/font/32ptArial_0.tga"; private static final Color FONTCOLOR = Color.white; private static final int DIALOGUEX = 15; private static final int DIALOGUEY = 430; private static final int TEXTWRAP = 770; private static final String TEXTFILE = "data/gamedata/visualnovelscript.txt"; private AngelCodeFont font; private String dialogueText; private ArrayList<String> script;
public void setFont(AngelCodeFont font) { this.font = font; }
public AngelCodeFont getFont() { return font; }
public String getDialogueText() { return dialogueText; } public void setScript(ArrayList<String> script) { this.script = script; }
public ArrayList<String> getScript() { return script; }
@Override public void init(GameContainer container, StateBasedGame game) throws SlickException { this.setFont(new AngelCodeFont(FONTDEF, FONTIMG)); }
@Override public void enter(GameContainer container, StateBasedGame game) throws SlickException { TextFileReader parser = new TextFileReader(); this.setScript(parser.read(TEXTFILE)); }
@Override public void render(GameContainer container, StateBasedGame game, Graphics g) throws SlickException { this.getFont().drawString(DIALOGUEX, DIALOGUEY, this.getDialogueText(), FONTCOLOR); } } |
|
|
|
|
|
5
|
Game Development / Newbie & Debugging Questions / Slick2D Trying to display text in UnicodeFont
|
on: 2012-03-03 22:54:35
|
Im trying to display Strings of text in Slick2D using the UnicodeFont (because TrueTypeFont is deprecated). The class I am working on is shown below. Im guessing that lines 106 and 117 are where the problem lies, but I don't know how to fix it (Ive never displayed text in Slick2D before). Also, everything else works fine. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
| public class VisualNovelState extends BasicGameState{ public static final String BACKGROUND = [FILEPATH]; public static final String PORTRAIT = [FILEPATH]; public static final String NAMEBOX = [FILEPATH]; public static final String DIALOGUEBOX = [FILEPATH]; public static final String TESTSTRING = "Text display test. "; public static final String FONT = "Arial"; public static final int STYLE = java.awt.Font.PLAIN; public static final int SIZE = 60;
private int stateId; private Image background; private Image portrait; private Image nameBox; private Image dialogueBox; private String nameText; private String dialogueText; private UnicodeFont font;
public void setID(int stateId){ this.stateId = stateId; }
@Override public int getID() { return stateId; }
public void setBackground(Image background){ this.background = background; }
public Image getBackground(){ return background; }
public void setPortrait(Image portrait){ this.portrait = portrait; }
public Image getPortrait(){ return portrait; }
public void setNameBox(Image nameBox){ this.nameBox = nameBox; }
public Image getNameBox(){ return nameBox; }
public void setDialogueBox(Image dialogueBox){ this.dialogueBox = dialogueBox; }
public Image getDialogueBox(){ return dialogueBox; } public void setNameText(String nameText){ this.nameText = nameText; }
public String getNameText(){ return nameText; }
public void setDialogueText(String dialogueText){ this.dialogueText = dialogueText; }
public String getDialogueText(){ return dialogueText; }
public void setFont(UnicodeFont font){ this.font = font; }
public UnicodeFont getFont(){ return font; }
@Override public void init(GameContainer container, StateBasedGame game) throws SlickException { this.setBackground(new Image(BACKGROUND)); this.setPortrait(new Image(PORTRAIT)); this.setNameBox(new Image(NAMEBOX)); this.setDialogueBox(new Image(DIALOGUEBOX)); this.setFont(new UnicodeFont(new java.awt.Font(FONT, STYLE, SIZE))); this.setDialogueText(TESTSTRING); } @Override public void render(GameContainer container, StateBasedGame game, Graphics g) throws SlickException { this.getBackground().draw(0,0); this.getPortrait().draw(0,0); this.getNameBox().draw(0,0); this.getDialogueBox().draw(0,0); this.getFont().drawString(1, 450, this.getDialogueText()); }
@Override public void update(GameContainer container, StateBasedGame game, int delta) throws SlickException { Input input = container.getInput();
if(input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)){ } }
public VisualNovelState(int stateId) throws SlickException{ this.setID(stateId); } } |
|
|
|
|
|
8
|
Game Development / Game Play & Game Design / Need help designing a Visual Novel/Tactical RPG (kinda like Fire Emblem)
|
on: 2011-08-06 03:09:58
|
So Im making a game that's a mix of Visual Novel and Tactical RPG. Essentially the game flows from one event to another based on a script. Decisions along the way can alter the path the player takes through the story. There are two general types of events: Story events which tell the story, and Battle events in which the player must fight enemies in a Tactical RPG fashion. Basically its like Fire Emblem but with branching paths. So what i need help with is: 1) Getting the game to go along with the story as well as player decisions (namely the branching paths). 2) Getting the game to switch from "Story mode" to "Battle mode" 3) Getting all of the above to display on the screen. 3.5) To be more specific on number 3, theres an awful lot of data related to graphics (like monster sprites) that are all the way down in Battle that need to be brought up to the Main class to be displayed, while Battle is running a loop and without interfering with said loop. 4) Making sure i dont get something like the screen not updating until the battle loop is finished (I have a funny feeling that my current main class will end up doing this). 5) How would I go about getting, say, PNG files to display using the opengl Display? Right now the problem is more of overall design rather than actual programming, so for now i just need general suggestions like what kinds of classes to make and how to have them interact with each other and ultimately how to get the main class shown below to work with them. Also I pretty much already have the battle system designed, so im good on everything that takes place WITHIN a "Battle Event"; its just everything outside that i need help with. Thanks to anyone who can help. The work ive done so far: For numbers 1 and 2, my idea is to have a Script class, which contains an Array of Scenes (Scene is an interface with one method play()) and has a method to read them called read(). Two classes, Story and Battle, implement Scene. When play() is called on a Story, it reads through the dialogue until it runs out of dialog to display, at which point read() moves on to the next Scene. When play() is called on a Battle, the game goes through a battle (it should be noted that in this setup, Battle.play() contains the battle system.). Here's the main class (there is some lwjgl and opengl involved, but just a little bit) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| package game;
import org.lwjgl.Sys; import org.lwjgl.input.Keyboard; import org.lwjgl.input.Mouse; import org.lwjgl.opengl.Display;
public class Game {
public static final String GAME_TITLE = "My Game"; private static final int FRAMERATE = 60; private static boolean finished;
public static void main(String[] args) { try { init(); run(); } catch (Exception e) { e.printStackTrace(System.err); Sys.alert(GAME_TITLE, "An error occured and the game will close."); } finally { cleanup(); } }
public static void init() throws Exception { Display.setTitle(GAME_TITLE); Display.setVSyncEnabled(true); Display.create(); }
public static void run() { while (!finished) { Display.update(); if (Display.isCloseRequested()) { finished = true; } else if (Display.isActive()) { Display.sync(FRAMERATE); } else { try { Thread.sleep(100); } catch (InterruptedException e) { } } } }
public static void cleanup() { Display.destroy(); } } |
|
|
|
|
|
9
|
Game Development / Newbie & Debugging Questions / Re: RPG - Changing maps and entering battle
|
on: 2011-04-12 18:37:11
|
Also, do not use the paint(Graphics) method in JPanel. All Swing components that you extend must draw in paintComponent(Graphics). Then there is no need to call "super".
Thanks ill try that. Although to be more specific, the problem im having is that the map doesnt change when the player steps on the specified tile.
|
|
|
|
|
10
|
Game Development / Newbie & Debugging Questions / RPG - going from map screen to battle screen
|
on: 2011-04-12 04:39:51
|
|
OMG ANOTHER EDIT: Ok, ive nearly completed the "map screen" portion of my RPG. The next thing im going to try to do is make the battle system. The challenge im having now is figuring out how to go from the map screen to the battle screen. The layout of the battle screen is drastically different than that of the map screen, and the controls do different things on that screen to. Can i somehow fit this into my current Canvas class, or do i need to make a seperate class for it and somehow have the Main class switch between them?
current version of Main: [Code]package projectrpg;
import java.awt.Color; import java.io.IOException; import javax.swing.JFrame; import javax.xml.parsers.ParserConfigurationException; import javax.xml.xpath.XPathExpressionException; import org.xml.sax.SAXException;
/** * * @author Dennis Jr */ public class Main extends JFrame {
public Main(String map, GameData data) { add(new Canvas("map1", data)); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //xAxis should be 10 pixles shorter than yAxis. For some reason the //xAxis seems to be naturally 10 pixels more than specified. setSize(490, 500); setLocationRelativeTo(null); setTitle("2D Adventure"); setResizable(false); setVisible(true); }
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException { GameData data = new GameData(); String map = "map1"; new Main(map, data); } }[/Code]
Current version of Canvas [Code]package projectrpg;
import java.awt.Rectangle;
import java.awt.Graphics;
import java.util.ArrayList;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JPanel;
/** * * @author Dennis Jr */ public class Canvas extends JPanel implements Runnable {
private GameData data; private Thread animator; private final int DELAY = 25; private Map levelMap; //Enable Test Map, delete or comment out before distribution. //private ArrayList<MapTile> mapTiles = new ArrayList<MapTile>(); //Normal Map, enable before distribution. private ArrayList<MapTile> mapTiles; //Player's sprite and position on screen are the same throughout the game. //When the player moves, in reality the map moves around him. //Player should be positioned so x position = (xAxis / 2) - 15. //and y position = (yAxis / 2) - 30. private ArrayList<LinkTile> linkTiles; //This puts the center of the player in the center of the screen. private Player player; private int xShift; private int yShift; private Rectangle facing; private final int WIDTH = 20; private final int HEIGHT = 20; private final int PLAYERSTARTX = 230; private final int PLAYERSTARTY = 220; private final String PLAYERIMAGE = "player.png"; private ArrayList<Interactable> interactables; private ArrayList<Collidable> collidables;
public Canvas(String mapName, GameData data) { addKeyListener(new TAdapter()); setFocusable(true); setDoubleBuffered(true); this.data = data; this.levelMap = this.data.findMap(mapName); this.player = new Player(PLAYERSTARTX, PLAYERSTARTY, PLAYERIMAGE); setFacing(player.getX(), player.getY() + HEIGHT, WIDTH, HEIGHT); this.xShift = 0; this.yShift = 0; this.interactables = this.levelMap.getInteractables(); this.collidables = this.levelMap.getCollidables(); this.linkTiles = this.levelMap.getLinkTiles(); }
public void drawMap(Graphics g) { int x = 0; int y = 0;
for (Collidable a : getCollidables()) { x = a.getX(); y = a.getY();
g.drawImage(a.getImage(), x, y, this); } g.drawImage(player.getImage(), player.getX(), player.getY(), this); }
@Override public void paint(Graphics g) { super.paint(g); drawMap(g); }
@Override public void addNotify() { super.addNotify(); animator = new Thread(this); animator.start(); }
public void run() { long beforeTime, timeDiff, sleep;
beforeTime = System.currentTimeMillis();
while (true) {
repaint();
timeDiff = System.currentTimeMillis() - beforeTime; sleep = DELAY - timeDiff;
if (sleep < 0) { sleep = 2; } try { Thread.sleep(sleep); } catch (InterruptedException e) { System.out.println("interrupted"); }
beforeTime = System.currentTimeMillis(); } }
public void checkCollision(Mobile mobile) { for (Collidable a : getCollidables()) { if (a.isCollidable() == true) { Rectangle r2 = a.getBounds(); if (r2.intersects(mobile.getLeftBound())) { mobile.setLeftCollision(true); } if (r2.intersects(mobile.getRightBound())) { mobile.setRightCollision(true); } if (r2.intersects(mobile.getUpBound())) { mobile.setTopCollision(true); } if (r2.intersects(mobile.getDownBound())) { mobile.setBottomCollision(true); } } } }
/** * @return the interactables */ public ArrayList<Interactable> getInteractables() { return interactables; }
/** * @param interactables the interactables to set */ public void setInteractables(ArrayList<Interactable> interactables) { this.interactables = interactables; }
/** * @return the collidables */ public ArrayList<Collidable> getCollidables() { return collidables; }
/** * @param collidables the collidables to set */ public void setCollidables(ArrayList<Collidable> collidables) { this.collidables = collidables; }
class TAdapter extends KeyAdapter {
@Override public void keyPressed(KeyEvent e) {
int key = e.getKeyCode(); player.setLeftCollision(false); player.setRightCollision(false); player.setTopCollision(false); player.setBottomCollision(false);
if (key == KeyEvent.VK_S) { setFacing(player.getX() - WIDTH, player.getY(), WIDTH, HEIGHT); checkCollision(player); if (player.isLeftCollision()) { return; } for (Collidable a : collidables) { a.scrollX(WIDTH); } xShift = xShift + WIDTH; } else if (key == KeyEvent.VK_F) { setFacing(player.getX() + WIDTH, player.getY(), WIDTH, HEIGHT); checkCollision(player); if (player.isRightCollision()) { return; } for (Collidable a : collidables) { a.scrollX(-WIDTH); } xShift = xShift - WIDTH; } else if (key == KeyEvent.VK_E) { setFacing(player.getX(), player.getY() - HEIGHT, WIDTH, HEIGHT); checkCollision(player); if (player.isTopCollision()) { return; } for (Collidable a : collidables) { a.scrollY(HEIGHT); } yShift = yShift + HEIGHT; } else if (key == KeyEvent.VK_D) { setFacing(player.getX(), player.getY() + HEIGHT, WIDTH, HEIGHT); checkCollision(player); if (player.isBottomCollision()) { return; } for (Collidable a : collidables) { a.scrollY(-HEIGHT); } yShift = yShift - HEIGHT; } else if (key == KeyEvent.VK_J) { for (Interactable a : getInteractables()) { if (facing.intersects(a.getBounds())) { a.interact(); } else { return; } } } checkLink(player); repaint(); } }
/** * @return the levelMap */ public Map getLevelMap() { return levelMap; }
/** * @param levelMap the levelMap to set */ public void setLevelMap(Map levelMap) { this.levelMap = levelMap; }
public void checkLink(Mobile mobile) { for (LinkTile a : linkTiles) { Rectangle r = a.getBounds(); if (r.intersects(mobile.getBounds())) { String toMap = a.getToMap(); int newxShift = a.getxShift(); int newyShift = a.getyShift(); Map newMap = data.findMap(toMap);
//Reset old map to original position for (MapTile d : mapTiles) { d.setX(d.getX() - xShift); d.setY(d.getY() - yShift); }
for (LinkTile e : linkTiles) { e.setX(e.getX() - xShift); e.setY(e.getY() - yShift); }
//load new map setLevelMap(newMap); setCollidables(levelMap.getCollidables()); setInteractables(levelMap.getInteractables());
//shift new map to starting position for (MapTile b : mapTiles) { b.setX(b.getX() + newxShift); b.setY(b.getY() + newyShift); }
for (LinkTile c : linkTiles) { c.setX(c.getX() + newxShift); c.setY(c.getY() + newyShift); }
//set shift values xShift = newxShift; yShift = newyShift;
} } }
public void setMapTiles(Map levelMap) { mapTiles = levelMap.getMapTiles(); }
public void setLinkTiles(Map levelMap) { linkTiles = levelMap.getLinkTiles(); }
public void setFacing(int x, int y, int width, int height) { facing = new Rectangle(x, y, width, height); }
public Rectangle getFacing() { return facing; } }[/Code]
|
|
|
|
|
11
|
Game Development / Game Play & Game Design / Implementing battle actions in an RPG?
|
on: 2011-02-02 22:37:09
|
Hello, I'm in the process of developing my first Java RPG. So far i have a basic battle system in place in which creatures are created from data stored in XML files (which upon starting the game is parsed into a GameData object which various methods can search). The creatures can act in a certain order, attack other creatures, take damage and die. Now in order for the battles to really take shape i need to implement a variety of special actions. Player characters will share a small number of universal actions (such as Move, Basic Attack, and Defend) and will learn a number of unique actions as the game progresses (Mostly special attacks and utility powers). Monsters will have a predetermined list of actions they can take, which varies from species to species (naturally all will be able to at least Move and Attack). Initially Actions are performed by typing the name of the Action into the console, as I have yet to implement anything graphics-wise, eventually they will be handled through a GUI. My question is how to implement actions like this? Should Actions be their own class or part of the Main class? How much of an Action should be programmed in Java and how much should be stored in XML? Thanks to anyone who can help me out. 
|
|
|
|
|
|
Add your game by posting it in the WIP section,
or publish it in Showcase.
The first screenshot will be displayed as a thumbnail.
|
|