Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**######
- * Game.Java
- *@author Robert
- *@version 0.0.1
- *Main Game class, handles the entire game.
- */
- package game;
- //Local Imports
- import game.core.GameCore;
- import game.input.GameAction;
- import game.input.InputManager;
- import game.world.World;
- //Java Imports
- import java.awt.Graphics2D;
- import java.awt.event.KeyEvent;
- import java.awt.Point;
- public class Game extends GameCore {
- private Point cache = new Point();
- private final static int SIZE_H = 256;
- private final static int SIZE_W = 256;
- private World world;
- private WorldMapRenderer renderer;
- private InputManager inputManager;
- private WorldMap map;
- private GameAction exit;
- private GameAction scrollMapLeft;
- private GameAction scrollMapRight;
- private GameAction scrollMapUp;
- private GameAction scrollMapDown;
- public static void main(String args[]){
- new Game().run();
- }
- public void init(){
- super.init();
- initInput();
- //create and start a new Resource Manager....Why? Who Cares?!
- ResourceManager rM = new ResourceManager(wm.getFullScreenWindow().getGraphicsConfiguration());
- world = generateGameWorld(SIZE_H, SIZE_W);
- //load our resources!
- renderer = new WorldMapRenderer();
- map = new WorldMap(world, SIZE_H, SIZE_W);
- }
- public void stop(){
- super.stop();
- }
- private World generateGameWorld(int x, int y){
- World gameWorld = new World(x, y);
- return gameWorld;
- }
- @Override
- public void draw(Graphics2D g) {
- renderer.draw(g, map, wm.getWidth(), wm.getHeight());
- }
- public void initInput(){
- exit = new GameAction("exit");
- scrollMapLeft = new GameAction("Scroll Map Left");
- scrollMapRight = new GameAction("Scroll Map Right");
- scrollMapUp = new GameAction("Scroll Map UP");
- scrollMapDown = new GameAction("Scroll Map Down");
- inputManager = new InputManager(wm.getFullScreenWindow());
- inputManager.mapToKey(scrollMapLeft, KeyEvent.VK_LEFT);
- inputManager.mapToKey(scrollMapRight, KeyEvent.VK_RIGHT);
- inputManager.mapToKey(scrollMapUp, KeyEvent.VK_UP);
- inputManager.mapToKey(scrollMapDown, KeyEvent.VK_DOWN);
- inputManager.mapToKey(exit, KeyEvent.VK_ESCAPE);
- }
- public void checkInput(long elapsedTime){
- if (exit.isPressed()){
- stop();
- }
- if(scrollMapLeft.isPressed()){
- if(map.getCenterX() < WorldMapRenderer.tilesToPixels(1)){
- map.setCenterX(0);
- }
- else{
- map.setCenterX(-1);
- }
- }
- if(scrollMapRight.isPressed()){
- if(WorldMapRenderer.pixelsToTiles(map.getCenterX()) >= WorldMapRenderer.tilesToPixels(map.getWidth())){
- map.setCenterX(0);
- }
- else{
- map.setCenterX(1);
- }
- }
- if(scrollMapUp.isPressed()){
- map.setCenterY(-1);
- }
- if(scrollMapDown.isPressed()){
- map.setCenterY(1);
- }
- }
- public void update(long elapsedTime){
- checkInput(elapsedTime);
- }
- }
- //###END GAME.JAVA
- /**
- * WorldMap.Java
- * @author Robert
- * Creates A World Map From the Generated World.
- */
- package game;
- import game.graphicsEngine.Sprite;
- import game.world.World;
- import java.awt.Image;
- import java.awt.Point;
- import java.util.LinkedList;
- import java.util.Iterator;
- import java.util.TreeMap;
- import javax.swing.ImageIcon;
- public class WorldMap {
- private Image[][] tiles;
- private LinkedList<Sprite> sprites;
- private Point centerPoint;
- public WorldMap(World world, int height, int width){
- centerPoint = new Point();
- centerPoint.x = WorldMapRenderer.tilesToPixels(width) / 2;
- centerPoint.y = WorldMapRenderer.tilesToPixels(height) / 2;
- tiles = new Image[height][width];
- for(int y = 0; y < height; y++){
- for (int x = 0; x < width; x++){
- setTile(y, x, world.getTile(y, x).getImageIcon());
- }
- }
- }
- public void setTile(int y, int x, ImageIcon icon){
- tiles[y][x] = icon.getImage();
- }
- public Image getTile(int y, int x){
- if (x < 0 || x >= getWidth()
- || y < 0 || y>= getHeight()){
- return null;
- }
- else{
- return tiles[y][x];
- }
- }
- public int getWidth(){
- return tiles[0].length;
- }
- public int getHeight(){
- return tiles.length;
- }
- public void addSprite(Sprite sprite){
- sprites.add(sprite);
- }
- public void removeSprite(Sprite sprite){
- sprites.remove(sprite);
- }
- public Iterator<Sprite> getSprites() {
- // TODO Auto-generated method stub
- return sprites.iterator();
- }
- /**
- * @param width the width to set
- */
- public void setWidth(int width) {
- }
- /**
- * @param height the height to set
- */
- public void setHeight(int height) {
- }
- public int getCenterX(){
- return centerPoint.x;
- }
- public int getCenterY(){
- return centerPoint.y;
- }
- public void setCenterX(int delta){
- centerPoint.x += WorldMapRenderer.tilesToPixels(delta);
- }
- public void setCenterY(int delta){
- centerPoint.y += WorldMapRenderer.tilesToPixels(delta);
- }
- }
- //###End WorldMap.java###
- /**
- * WorldMapRenderer.java
- *@author Robert
- * renders a worldMap to a displayable graphic
- */
- package game;
- import java.awt.Graphics2D;
- import java.awt.Image;
- public class WorldMapRenderer {
- private static final int TILE_SIZE = 64;
- /**
- * The Size in Bits of Tile.
- * Math.pow(2, TILE_SIZE_BITS) == TILE_SIZE;
- */
- private static final int TILE_SIZE_BITS = 6;
- public static int pixelsToTiles(float pixels){
- return pixelsToTiles(Math.round(pixels));
- }
- public static int pixelsToTiles(int pixels){
- return pixels >> TILE_SIZE_BITS;
- }
- public static int tilesToPixels(int numOfTiles){
- return numOfTiles << TILE_SIZE_BITS;
- }
- public void draw(Graphics2D g, WorldMap map, int screenWidth, int screenHeight){
- int mapWidth = tilesToPixels(map.getWidth());
- int mapHeight = tilesToPixels(map.getHeight());
- //Not quite sure how to implement this yet.
- int offsetX = ((screenWidth / 2) - (map.getCenterX() / 2));
- //offsetX = Math.min(offsetX, 0);
- //offsetX = Math.max(offsetX, screenWidth - mapWidth);
- //get offsetY for drawing Sprites.
- int offsetY = ((screenHeight / 2) - (map.getCenterY() / 2));
- //offsetY = Math.min(offsetY, 0);
- //offsetY = Math.max(offsetY, screenHeight - mapHeight);
- //draw the visible map.
- int firstTileX = pixelsToTiles(- offsetX);
- int firstTileY = pixelsToTiles(-offsetY);
- if (firstTileX < 0){
- firstTileX = 0;
- }
- if (firstTileY < 0){
- firstTileY = 0;
- }
- int lastTileX = firstTileX + pixelsToTiles(screenWidth) + 1;
- int lastTileY = firstTileY + pixelsToTiles(screenHeight) + 1;
- if (lastTileX >= map.getWidth()){
- lastTileX = map.getWidth() - 1;
- }
- if (lastTileY >= map.getHeight()){
- lastTileY = map.getHeight() - 1;
- }
- for (int y = firstTileY; y <= lastTileY; y++){
- for(int x = firstTileX; x <= lastTileX; x++){
- Image image = map.getTile(y, x);
- if (image != null){
- g.drawImage(image,
- tilesToPixels(x) + offsetX,
- tilesToPixels(y) + offsetY,
- null);
- }
- }
- }
- /*
- //draw Sprites --(really do nothing for now, uncomment when you have unit animations and shit.
- * right now we're just trying to draw the map.)
- Iterator i = map.getSprites();
- while(i.hasNext()){
- Sprite sprite = (Sprite)i.next();
- int x = Math.round(sprite.getX() + offsetX);
- int y = Math.round(sprite.getY() + offsetY);
- g.draw(sprite.getImage(), x, y, null);
- //wake up the unit if its on screen.
- if (sprite instanceof Unit && x >= 0 && x < screenWidth && y >= 0 && y < screenHeight){
- ((Unit)sprite).wakeup();
- }
- }
- */
- }
- public static int getTileSize() {
- return TILE_SIZE;
- }
- }
- //###End WorldMapRenderer.java###
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement