Pastebin
API
tools
faq
paste
Login
Sign up
Please fix the following errors:
New Paste
Syntax Highlighting
/**###### * 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###
Optional Paste Settings
Category:
None
Cryptocurrency
Cybersecurity
Fixit
Food
Gaming
Haiku
Help
History
Housing
Jokes
Legal
Money
Movies
Music
Pets
Photo
Science
Software
Source Code
Spirit
Sports
Travel
TV
Writing
Tags:
Syntax Highlighting:
None
Bash
C
C#
C++
CSS
HTML
JSON
Java
JavaScript
Lua
Markdown (PRO members only)
Objective C
PHP
Perl
Python
Ruby
Swift
4CS
6502 ACME Cross Assembler
6502 Kick Assembler
6502 TASM/64TASS
ABAP
AIMMS
ALGOL 68
APT Sources
ARM
ASM (NASM)
ASP
ActionScript
ActionScript 3
Ada
Apache Log
AppleScript
Arduino
Asymptote
AutoIt
Autohotkey
Avisynth
Awk
BASCOM AVR
BNF
BOO
Bash
Basic4GL
Batch
BibTeX
Blitz Basic
Blitz3D
BlitzMax
BrainFuck
C
C (WinAPI)
C Intermediate Language
C for Macs
C#
C++
C++ (WinAPI)
C++ (with Qt extensions)
C: Loadrunner
CAD DCL
CAD Lisp
CFDG
CMake
COBOL
CSS
Ceylon
ChaiScript
Chapel
Clojure
Clone C
Clone C++
CoffeeScript
ColdFusion
Cuesheet
D
DCL
DCPU-16
DCS
DIV
DOT
Dart
Delphi
Delphi Prism (Oxygene)
Diff
E
ECMAScript
EPC
Easytrieve
Eiffel
Email
Erlang
Euphoria
F#
FO Language
Falcon
Filemaker
Formula One
Fortran
FreeBasic
FreeSWITCH
GAMBAS
GDB
GDScript
Game Maker
Genero
Genie
GetText
Go
Godot GLSL
Groovy
GwBasic
HQ9 Plus
HTML
HTML 5
Haskell
Haxe
HicEst
IDL
INI file
INTERCAL
IO
ISPF Panel Definition
Icon
Inno Script
J
JCL
JSON
Java
Java 5
JavaScript
Julia
KSP (Kontakt Script)
KiXtart
Kotlin
LDIF
LLVM
LOL Code
LScript
Latex
Liberty BASIC
Linden Scripting
Lisp
Loco Basic
Logtalk
Lotus Formulas
Lotus Script
Lua
M68000 Assembler
MIX Assembler
MK-61/52
MPASM
MXML
MagikSF
Make
MapBasic
Markdown (PRO members only)
MatLab
Mercury
MetaPost
Modula 2
Modula 3
Motorola 68000 HiSoft Dev
MySQL
Nagios
NetRexx
Nginx
Nim
NullSoft Installer
OCaml
OCaml Brief
Oberon 2
Objeck Programming Langua
Objective C
Octave
Open Object Rexx
OpenBSD PACKET FILTER
OpenGL Shading
Openoffice BASIC
Oracle 11
Oracle 8
Oz
PARI/GP
PCRE
PHP
PHP Brief
PL/I
PL/SQL
POV-Ray
ParaSail
Pascal
Pawn
Per
Perl
Perl 6
Phix
Pic 16
Pike
Pixel Bender
PostScript
PostgreSQL
PowerBuilder
PowerShell
ProFTPd
Progress
Prolog
Properties
ProvideX
Puppet
PureBasic
PyCon
Python
Python for S60
QBasic
QML
R
RBScript
REBOL
REG
RPM Spec
Racket
Rails
Rexx
Robots
Roff Manpage
Ruby
Ruby Gnuplot
Rust
SAS
SCL
SPARK
SPARQL
SQF
SQL
SSH Config
Scala
Scheme
Scilab
SdlBasic
Smalltalk
Smarty
StandardML
StoneScript
SuperCollider
Swift
SystemVerilog
T-SQL
TCL
TeXgraph
Tera Term
TypeScript
TypoScript
UPC
Unicon
UnrealScript
Urbi
VB.NET
VBScript
VHDL
VIM
Vala
Vedit
VeriLog
Visual Pro Log
VisualBasic
VisualFoxPro
WHOIS
WhiteSpace
Winbatch
XBasic
XML
XPP
Xojo
Xorg Config
YAML
YARA
Z80 Assembler
ZXBasic
autoconf
jQuery
mIRC
newLISP
q/kdb+
thinBasic
Paste Expiration:
Never
Burn after read
10 Minutes
1 Hour
1 Day
1 Week
2 Weeks
1 Month
6 Months
1 Year
Paste Exposure:
Public
Unlisted
Private
Folder:
(members only)
Password
NEW
Enabled
Disabled
Burn after read
NEW
Paste Name / Title:
Create New Paste
Hello
Guest
Sign Up
or
Login
Sign in with Facebook
Sign in with Twitter
Sign in with Google
You are currently not logged in, this means you can not edit or delete anything you paste.
Sign Up
or
Login
Public Pastes
memtest 86 logg
1 hour ago | 229.83 KB
TLOZ Windwaker - Windfall Island - Virtual Pi...
5 hours ago | 1.57 KB
squar
11 hours ago | 0.10 KB
my-pus
11 hours ago | 0.09 KB
OoT rando seed 6/18
18 hours ago | 69.75 KB
Peter Thiel Dialog Society
20 hours ago | 23.72 KB
other seps
CSS | 21 hours ago | 0.15 KB
Check socradar.io for your FortiGate
PowerShell | 23 hours ago | 2.33 KB
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the
Cookies Policy
.
OK, I Understand
Not a member of Pastebin yet?
Sign Up
, it unlocks many cool features!