Initial working version of many miso classes. The beginnings of some
of our tile- and scene-related machinations. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@44 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
//
|
||||
// $Id: CompiledSceneManager.java,v 1.1 2001/07/12 22:38:03 shaper Exp $
|
||||
|
||||
package com.threerings.cocktail.miso.scene;
|
||||
|
||||
public class CompiledSceneManager implements SceneManager
|
||||
{
|
||||
// context, rsrc mgr, loads compiled scene files via rsrc mgr
|
||||
public Scene getScene (String name)
|
||||
{
|
||||
// TBD
|
||||
return null;
|
||||
}
|
||||
|
||||
public Scene getScene (int sid)
|
||||
{
|
||||
// TBD
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,229 @@
|
||||
//
|
||||
// $Id: DisplayMisoSceneImpl.java,v 1.1 2001/07/12 22:38:03 shaper Exp $
|
||||
|
||||
package com.threerings.cocktail.miso.scene;
|
||||
|
||||
import com.threerings.cocktail.miso.tile.Tile;
|
||||
import com.threerings.cocktail.miso.tile.TileManager;
|
||||
|
||||
import java.awt.Point;
|
||||
import java.io.*;
|
||||
|
||||
/**
|
||||
* A scene represents the data model corresponding to a single screen
|
||||
* for game play. For instance, one scene might display a portion of
|
||||
* a street with several buildings scattered about on the periphery.
|
||||
*/
|
||||
public class Scene
|
||||
{
|
||||
public Tile tiles[][][]; // the tiles comprising the scene
|
||||
|
||||
/**
|
||||
* Construct a new Scene object initialized to a default state.
|
||||
*/
|
||||
public Scene (TileManager tmgr)
|
||||
{
|
||||
_tmgr = tmgr;
|
||||
|
||||
_name = DEF_SCENE_NAME;
|
||||
_sid = 0;
|
||||
_version = VERSION;
|
||||
|
||||
tiles = new Tile[TILE_WIDTH][TILE_HEIGHT][NUM_LAYERS];
|
||||
|
||||
Tile tile = _tmgr.getTile(DEF_TILESET_NAME, DEF_TILE_NAME);
|
||||
for (int xx = 0; xx < TILE_WIDTH; xx++) {
|
||||
for (int yy = 0; yy < TILE_HEIGHT; yy++) {
|
||||
for (int ii = 0; ii < NUM_LAYERS; ii++) {
|
||||
if (ii == LAYER_BASE) {
|
||||
tiles[xx][yy][ii] = tile;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_file = null;
|
||||
}
|
||||
|
||||
public File getFile ()
|
||||
{
|
||||
return _file;
|
||||
}
|
||||
|
||||
public void setFile (File file)
|
||||
{
|
||||
_file = file;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the number of actual (non-null) tiles present in the
|
||||
* specified tile layer for this scene.
|
||||
*/
|
||||
public int getNumLayerTiles (int lnum)
|
||||
{
|
||||
if (lnum == LAYER_BASE) return TILE_WIDTH * TILE_HEIGHT;
|
||||
|
||||
int numTiles = 0;
|
||||
|
||||
for (int xx = 0; xx < TILE_WIDTH; xx++) {
|
||||
for (int yy = 0; yy < TILE_HEIGHT; yy++) {
|
||||
if (tiles[xx][yy] != null) numTiles++;
|
||||
}
|
||||
}
|
||||
|
||||
return numTiles;
|
||||
}
|
||||
|
||||
/**
|
||||
* Populate the scene object by reading the contents from the given
|
||||
* input stream.
|
||||
*/
|
||||
public void readFrom (InputStream in) throws IOException
|
||||
{
|
||||
DataInputStream dis = new DataInputStream(in);
|
||||
|
||||
// read scene header information
|
||||
_name = dis.readUTF();
|
||||
_sid = dis.readShort();
|
||||
_version = dis.readShort();
|
||||
|
||||
// make sure we can understand the file format
|
||||
if (_version < 0 || _version > VERSION) {
|
||||
throw new IOException("Can't understand scene file format " +
|
||||
" [version=" + _version + "]");
|
||||
}
|
||||
|
||||
// allocate full tile array. null tiles denote tiles in absentia.
|
||||
tiles = new Tile[TILE_WIDTH][TILE_HEIGHT][NUM_LAYERS];
|
||||
|
||||
// read all tiles for the base layer
|
||||
for (int xx = 0; xx < TILE_WIDTH; xx++) {
|
||||
for (int yy = 0; yy < TILE_HEIGHT; yy++) {
|
||||
// read tile details
|
||||
short tsid = dis.readShort();
|
||||
short tid = dis.readShort();
|
||||
|
||||
// retrieve tile from tile manager
|
||||
tiles[xx][yy][LAYER_BASE] = _tmgr.getTile(tsid, tid);
|
||||
}
|
||||
}
|
||||
|
||||
// read tiles for each of the subsequent layers
|
||||
for (int lnum = 1; lnum < NUM_LAYERS; lnum++) {
|
||||
// read the number of tiles in this layer
|
||||
int numTiles = dis.readShort();
|
||||
|
||||
for (int ii = 0; ii < numTiles; ii++) {
|
||||
// read tile details
|
||||
short tsid = dis.readShort();
|
||||
short tid = dis.readShort();
|
||||
byte tx = dis.readByte();
|
||||
byte ty = dis.readByte();
|
||||
|
||||
// retrieve tile from tile manager
|
||||
tiles[tx][ty][lnum] = _tmgr.getTile(tsid, tid);
|
||||
}
|
||||
}
|
||||
|
||||
// read hotspot points
|
||||
short numSpots = dis.readShort();
|
||||
_hotspots = new Point[numSpots];
|
||||
for (int ii = 0; ii < numSpots; ii++) {
|
||||
_hotspots[ii] = new Point();
|
||||
_hotspots[ii].x = dis.readByte();
|
||||
_hotspots[ii].y = dis.readByte();
|
||||
}
|
||||
|
||||
// read exit points
|
||||
short numExits = dis.readShort();
|
||||
_exits = new ExitPoint[numExits];
|
||||
for (int ii = 0; ii < numExits; ii++) {
|
||||
_exits[ii] = new ExitPoint();
|
||||
_exits[ii].x = dis.readByte();
|
||||
_exits[ii].y = dis.readByte();
|
||||
_exits[ii].sid = dis.readShort();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Write this scene object to the specified output stream.
|
||||
*/
|
||||
public void writeTo (OutputStream out) throws IOException
|
||||
{
|
||||
DataOutputStream dos = new DataOutputStream(out);
|
||||
|
||||
// write scene header information
|
||||
dos.writeUTF(_name);
|
||||
dos.writeShort(_sid);
|
||||
dos.writeShort(_version);
|
||||
|
||||
// write tiles for the base layer
|
||||
for (int xx = 0; xx < TILE_WIDTH; xx++) {
|
||||
for (int yy = 0; yy < TILE_HEIGHT; yy++) {
|
||||
Tile tile = tiles[xx][yy][LAYER_BASE];
|
||||
dos.writeShort(tile.tsid);
|
||||
dos.writeShort(tile.tid);
|
||||
}
|
||||
}
|
||||
|
||||
// write tiles for each of the subsequent layers
|
||||
for (int lnum = 1; lnum < NUM_LAYERS; lnum++) {
|
||||
// write the number of tiles in this layer
|
||||
dos.writeShort(getNumLayerTiles(lnum));
|
||||
|
||||
for (int xx = 0; xx < TILE_WIDTH; xx++) {
|
||||
for (int yy = 0; yy < TILE_HEIGHT; yy++) {
|
||||
Tile tile = tiles[xx][yy][lnum];
|
||||
if (tile != null) {
|
||||
dos.writeShort(tile.tsid);
|
||||
dos.writeShort(tile.tid);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// write hotspot points
|
||||
int numSpots = (_hotspots == null) ? 0 : _hotspots.length;
|
||||
dos.writeShort(numSpots);
|
||||
for (int ii = 0; ii < numSpots; ii++) {
|
||||
dos.writeByte(_hotspots[ii].x);
|
||||
dos.writeByte(_hotspots[ii].y);
|
||||
}
|
||||
|
||||
// write exit points
|
||||
int numExits = (_exits == null) ? 0 : _exits.length;
|
||||
dos.writeShort(numExits);
|
||||
for (int ii = 0; ii < numExits; ii++) {
|
||||
dos.writeByte(_exits[ii].x);
|
||||
dos.writeByte(_exits[ii].y);
|
||||
dos.writeByte(_exits[ii].sid);
|
||||
}
|
||||
}
|
||||
|
||||
// the latest scene file format version number
|
||||
protected static final short VERSION = 1;
|
||||
|
||||
// scene width/height in tiles
|
||||
protected static final int TILE_WIDTH = 20;
|
||||
protected static final int TILE_HEIGHT = 40;
|
||||
|
||||
// layer identifiers and total number of layers
|
||||
protected static final int LAYER_BASE = 0;
|
||||
protected static final int LAYER_OBJECT = 1;
|
||||
protected static final int NUM_LAYERS = 2;
|
||||
|
||||
protected static final String DEF_SCENE_NAME = "Untitled Scene";
|
||||
|
||||
protected static final String DEF_TILESET_NAME = "ground";
|
||||
protected static final String DEF_TILE_NAME = "dirt";
|
||||
|
||||
protected String _name; // the scene name
|
||||
protected short _sid; // the unique scene id
|
||||
protected short _version; // file format version
|
||||
protected Point _hotspots[]; // hot spot zone points
|
||||
protected ExitPoint _exits[]; // exit points to different scenes
|
||||
|
||||
protected File _file; // the file last associated with this scene
|
||||
|
||||
protected TileManager _tmgr;
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
//
|
||||
// $Id: EditableSceneManager.java,v 1.1 2001/07/12 22:38:03 shaper Exp $
|
||||
|
||||
package com.threerings.cocktail.miso.scene;
|
||||
|
||||
public class EditableSceneManager implements SceneManager
|
||||
{
|
||||
public Scene getScene (String name)
|
||||
{
|
||||
// TBD
|
||||
return null;
|
||||
}
|
||||
|
||||
public Scene getScene (int sid)
|
||||
{
|
||||
// TBD
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
//
|
||||
// $Id: ExitPoint.java,v 1.1 2001/07/12 22:38:03 shaper Exp $
|
||||
|
||||
package com.threerings.cocktail.miso.scene;
|
||||
|
||||
/**
|
||||
* Represents a point in a scene that leads to a different scene.
|
||||
*/
|
||||
public class ExitPoint
|
||||
{
|
||||
byte x, y; // coordinates for this exit point
|
||||
short sid; // scene id this exit transitions to
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
//
|
||||
// $Id: IsoSceneView.java,v 1.1 2001/07/12 22:38:03 shaper Exp $
|
||||
|
||||
package com.threerings.cocktail.miso.scene;
|
||||
|
||||
import com.threerings.cocktail.miso.tile.Tile;
|
||||
import com.threerings.cocktail.miso.tile.TileManager;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
/**
|
||||
* The IsoSceneView provides an isometric graphics view of a
|
||||
* particular scene.
|
||||
*/
|
||||
public class IsoSceneView implements SceneView
|
||||
{
|
||||
public IsoSceneView (TileManager tmgr)
|
||||
{
|
||||
_tmgr = tmgr;
|
||||
|
||||
_viewX = 0;
|
||||
_viewY = 0;
|
||||
|
||||
_bounds = new Rectangle(0, 0, DEF_BOUNDS_WIDTH, DEF_BOUNDS_HEIGHT);
|
||||
}
|
||||
|
||||
public void paint (Graphics g)
|
||||
{
|
||||
|
||||
_offGraphics.setColor(Color.white);
|
||||
_offGraphics.fillRect(0, 0, _bounds.width, _bounds.height);
|
||||
|
||||
renderScene(_offGraphics, _viewX, _viewY);
|
||||
|
||||
g.drawImage(_offImage, 0, 0, null);
|
||||
}
|
||||
|
||||
protected void renderScene (Graphics g, int x, int y)
|
||||
{
|
||||
int mapX = x / Tile.HALF_WIDTH;
|
||||
int xOff = x & (Tile.HALF_WIDTH - 1);
|
||||
|
||||
int mapY = y / Tile.HEIGHT;
|
||||
int yOff = y & Tile.HEIGHT - 1;
|
||||
|
||||
int xa = xOff - yOff;
|
||||
int ya = (xOff >> 1) + (yOff >> 1);
|
||||
|
||||
int mx = mapX;
|
||||
int my = mapY;
|
||||
|
||||
int screenY = OFF_Y + (Tile.HALF_HEIGHT - ya);
|
||||
|
||||
for (int ii = 0; ii < Scene.TILE_HEIGHT; ii++) {
|
||||
int tx = mx;
|
||||
int ty = my;
|
||||
|
||||
int screenX = OFF_X + (Tile.HALF_WIDTH - xa);
|
||||
|
||||
int length = Scene.TILE_WIDTH;
|
||||
if ((ii & 1) == 1) {
|
||||
length++;
|
||||
screenX -= Tile.HALF_WIDTH;
|
||||
}
|
||||
|
||||
for (int j = 0; j < length; j++) {
|
||||
// TODO: draw layers L1+.
|
||||
Tile tile = _scene.tiles[tx][ty][Scene.LAYER_BASE];
|
||||
|
||||
g.drawImage(tile.img, screenX, screenY, null);
|
||||
|
||||
screenX += Tile.WIDTH;
|
||||
|
||||
if ((tx += 1) > Scene.TILE_WIDTH - 1) tx = 0;
|
||||
if ((ty -= 1) < 0) ty = Scene.TILE_HEIGHT - 1;
|
||||
}
|
||||
|
||||
screenY += Tile.HALF_HEIGHT;
|
||||
|
||||
if ((ii & 1) == 1) {
|
||||
if ((mx += 1) > Scene.TILE_WIDTH - 1) mx = 0;
|
||||
} else {
|
||||
if ((my += 1) > Scene.TILE_HEIGHT - 1) my = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void setScene (Scene scene)
|
||||
{
|
||||
_scene = scene;
|
||||
}
|
||||
|
||||
public void setTarget (Component target)
|
||||
{
|
||||
_offImage = target.createImage(_bounds.width, _bounds.height);
|
||||
_offGraphics = _offImage.getGraphics();
|
||||
}
|
||||
|
||||
protected static final int OFF_X = -Tile.WIDTH;
|
||||
protected static final int OFF_Y = 0;
|
||||
|
||||
protected static final int DEF_BOUNDS_WIDTH = 600;
|
||||
protected static final int DEF_BOUNDS_HEIGHT = 600;
|
||||
|
||||
protected Rectangle _bounds;
|
||||
|
||||
protected int _viewX, _viewY;
|
||||
|
||||
protected Graphics _offGraphics;
|
||||
protected Image _offImage;
|
||||
|
||||
protected Scene _scene;
|
||||
|
||||
protected TileManager _tmgr;
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
//
|
||||
// $Id: SceneManager.java,v 1.1 2001/07/12 22:38:03 shaper Exp $
|
||||
|
||||
package com.threerings.cocktail.miso.scene;
|
||||
|
||||
/**
|
||||
* Manages the various scenes that are displayed during the game and
|
||||
* provides simplified retrieval and caching facilities.
|
||||
*/
|
||||
public interface SceneManager
|
||||
{
|
||||
public Scene getScene (String name);
|
||||
|
||||
public Scene getScene (int sid);
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
//
|
||||
// $Id: SceneView.java,v 1.1 2001/07/12 22:38:03 shaper Exp $
|
||||
|
||||
package com.threerings.cocktail.miso.scene;
|
||||
|
||||
import java.awt.Component;
|
||||
import java.awt.Graphics;
|
||||
|
||||
/**
|
||||
* An interface to be implemented by classes that provide a view of a
|
||||
* given scene by drawing the scene contents onto a particular GUI
|
||||
* component.
|
||||
*/
|
||||
public interface SceneView
|
||||
{
|
||||
/**
|
||||
* Render the scene to the given graphics context.
|
||||
*/
|
||||
public void paint (Graphics g);
|
||||
|
||||
/**
|
||||
* Set the scene that we're rendering.
|
||||
*/
|
||||
public void setScene (Scene scene);
|
||||
|
||||
/**
|
||||
* Set the target component to which we're rendering.
|
||||
*/
|
||||
public void setTarget (Component target);
|
||||
}
|
||||
Reference in New Issue
Block a user