Moved object handling into base MisoSceneModel, created new

SectionedMisoSceneModel for storing info on a section of a larger scene.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@2406 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2003-04-12 02:14:10 +00:00
parent 98e48001dc
commit d933605343
3 changed files with 106 additions and 136 deletions
@@ -1,9 +1,16 @@
//
// $Id: MisoSceneModel.java,v 1.12 2003/02/12 05:39:15 mdb Exp $
// $Id: MisoSceneModel.java,v 1.13 2003/04/12 02:14:10 mdb Exp $
package com.threerings.miso.data;
import java.util.ArrayList;
import com.samskivert.util.ArrayUtil;
import com.samskivert.util.IntListUtil;
import com.samskivert.util.ListUtil;
import com.threerings.io.SimpleStreamableObject;
import com.threerings.media.util.MathUtil;
/**
* Contains basic information for a miso scene model that is shared among
@@ -12,17 +19,29 @@ import com.threerings.io.SimpleStreamableObject;
public class MisoSceneModel extends SimpleStreamableObject
implements Cloneable
{
/** The sector of our containing scene for which we contain model
* data. Simple scenes may contain only one sector, but larger more
* complicated scenes will likely contain multiple sectors to support
* fine grain updates. */
public int sectorId;
/** The width of this scene or section of the scene (depending on the
* model implementation), in tile units. */
public short width;
/** The width of our sector, in tile units. */
public int width;
/** The height of this scene or section of the scene (depending on the
* model implementation), in tile units. */
public short height;
/** The height of our sector, in tile units. */
public int height;
/** The combined tile ids (tile set id and tile id) of the
* "uninteresting" tiles in the object layer. */
public int[] objectTileIds;
/** The x coordinate of the "uninteresting" tiles in the object
* layer. */
public short[] objectXs;
/** The y coordinate of the "uninteresting" tiles in the object
* layer. */
public short[] objectYs;
/** Information records for the "interesting" objects in the object
* layer. */
public ObjectInfo[] objectInfo;
/**
* Creates a completely uninitialized model suitable for little more
@@ -37,8 +56,50 @@ public class MisoSceneModel extends SimpleStreamableObject
*/
public MisoSceneModel (int width, int height)
{
this.width = width;
this.height = height;
this.width = (short)MathUtil.bound(
Short.MIN_VALUE, width, Short.MAX_VALUE);
this.height = (short)MathUtil.bound(
Short.MIN_VALUE, height, Short.MAX_VALUE);
// start with zero-length object arrays
objectTileIds = new int[0];
objectXs = new short[0];
objectYs = new short[0];
objectInfo = new ObjectInfo[0];
}
/**
* Adds an object to this model.
*/
public void addObject (ObjectInfo info)
{
if (info.isInteresting()) {
objectInfo = (ObjectInfo[])ArrayUtil.append(objectInfo, info);
} else {
objectTileIds = ArrayUtil.append(objectTileIds, info.tileId);
objectXs = ArrayUtil.append(objectXs, (short)info.x);
objectYs = ArrayUtil.append(objectYs, (short)info.y);
}
}
/**
* Removes an object from this model.
*/
public void removeObject (ObjectInfo info)
{
// look for it in the interesting info array
int oidx = ListUtil.indexOfEqual(objectInfo, info);
if (oidx != -1) {
objectInfo = (ObjectInfo[])ArrayUtil.splice(objectInfo, oidx, 1);
}
// look for it in the uninteresting arrays
oidx = IntListUtil.indexOf(objectTileIds, info.tileId);
if (oidx != -1) {
objectTileIds = ArrayUtil.splice(objectTileIds, oidx, 1);
objectXs = ArrayUtil.splice(objectXs, oidx, 1);
objectYs = ArrayUtil.splice(objectYs, oidx, 1);
}
}
/**
@@ -47,10 +108,39 @@ public class MisoSceneModel extends SimpleStreamableObject
public Object clone ()
{
try {
return (MisoSceneModel)super.clone();
MisoSceneModel model = (MisoSceneModel)super.clone();
model.objectTileIds = (int[])objectTileIds.clone();
model.objectXs = (short[])objectXs.clone();
model.objectYs = (short[])objectYs.clone();
model.objectInfo = (ObjectInfo[])objectInfo.clone();
return model;
} catch (CloneNotSupportedException cnse) {
throw new RuntimeException(
"MisoSceneModel.clone() booched " + cnse);
throw new RuntimeException("MisoSceneModel.clone: " + cnse);
}
}
/**
* Populates the interesting and uninteresting parts of a miso scene
* model given lists of {@link ObjectInfo} records for each.
*/
public static void populateObjects (MisoSceneModel model,
ArrayList ilist, ArrayList ulist)
{
// set up the uninteresting arrays
int ucount = ulist.size();
model.objectTileIds = new int[ucount];
model.objectXs = new short[ucount];
model.objectYs = new short[ucount];
for (int ii = 0; ii < ucount; ii++) {
ObjectInfo info = (ObjectInfo)ulist.get(ii);
model.objectTileIds[ii] = info.tileId;
model.objectXs[ii] = (short)info.x;
model.objectYs[ii] = (short)info.y;
}
// set up the interesting array
int icount = ilist.size();
model.objectInfo = new ObjectInfo[icount];
ilist.toArray(model.objectInfo);
}
}
@@ -1,35 +0,0 @@
//
// $Id: MisoSceneRepository.java,v 1.1 2003/02/12 05:39:15 mdb Exp $
package com.threerings.miso.data;
import com.samskivert.io.PersistenceException;
/**
* A generic interface for an entity that provides access to {@link
* MisoSceneModel} data.
*/
public interface MisoSceneRepository
{
/**
* Loads the specified scene model from the repository.
*
* @return the requested scene or null if no scene exists with the
* specified scene and sector id.
*
* @exception PersistenceException thrown if an error occurs
* communicating with the underlying storage mechanism.
*/
public MisoSceneModel loadSceneModel (int sceneId, int sectorId)
throws PersistenceException;
/**
* Stores the specified model in the repository, inserting it if it
* does not already exist, updating it otherwise.
*
* @exception PersistenceException thrown if an error occurs
* communicating with the underlying storage mechanism.
*/
public void storeSceneModel (MisoSceneModel model)
throws PersistenceException;
}
@@ -1,5 +1,5 @@
//
// $Id: SimpleMisoSceneModel.java,v 1.1 2003/02/12 05:39:15 mdb Exp $
// $Id: SimpleMisoSceneModel.java,v 1.2 2003/04/12 02:14:10 mdb Exp $
package com.threerings.miso.data;
@@ -33,22 +33,6 @@ public class SimpleMisoSceneModel extends MisoSceneModel
* methods. */
public int[] baseTileIds;
/** The combined tile ids (tile set id and tile id) of the
* "uninteresting" tiles in the object layer. */
public int[] objectTileIds;
/** The x coordinate of the "uninteresting" tiles in the object
* layer. */
public short[] objectXs;
/** The y coordinate of the "uninteresting" tiles in the object
* layer. */
public short[] objectYs;
/** Information records for the "interesting" objects in the object
* layer. */
public ObjectInfo[] objectInfo;
/**
* Creates a completely uninitialized model suitable for little more
* than unserialization.
@@ -66,12 +50,6 @@ public class SimpleMisoSceneModel extends MisoSceneModel
this.vwidth = vwidth;
this.vheight = vheight;
allocateBaseTileArray();
// start with zero-length object arrays
objectTileIds = new int[0];
objectXs = new short[0];
objectYs = new short[0];
objectInfo = new ObjectInfo[0];
}
/**
@@ -100,40 +78,6 @@ public class SimpleMisoSceneModel extends MisoSceneModel
return true;
}
/**
* Adds an object to this model.
*/
public void addObject (ObjectInfo info)
{
if (info.isInteresting()) {
objectInfo = (ObjectInfo[])ArrayUtil.append(objectInfo, info);
} else {
objectTileIds = ArrayUtil.append(objectTileIds, info.tileId);
objectXs = ArrayUtil.append(objectXs, (short)info.x);
objectYs = ArrayUtil.append(objectYs, (short)info.y);
}
}
/**
* Removes an object from this model.
*/
public void removeObject (ObjectInfo info)
{
// look for it in the interesting info array
int oidx = ListUtil.indexOfEqual(objectInfo, info);
if (oidx != -1) {
objectInfo = (ObjectInfo[])ArrayUtil.splice(objectInfo, oidx, 1);
}
// look for it in the uninteresting arrays
oidx = IntListUtil.indexOf(objectTileIds, info.tileId);
if (oidx != -1) {
objectTileIds = ArrayUtil.splice(objectTileIds, oidx, 1);
objectXs = ArrayUtil.splice(objectXs, oidx, 1);
objectYs = ArrayUtil.splice(objectYs, oidx, 1);
}
}
/**
* Get the index into the baseTileIds[] for the specified
* x and y coordinates, or return -1 if the specified coordinates
@@ -196,35 +140,6 @@ public class SimpleMisoSceneModel extends MisoSceneModel
{
SimpleMisoSceneModel model = (SimpleMisoSceneModel)super.clone();
model.baseTileIds = (int[])baseTileIds.clone();
model.objectTileIds = (int[])objectTileIds.clone();
model.objectXs = (short[])objectXs.clone();
model.objectYs = (short[])objectYs.clone();
model.objectInfo = (ObjectInfo[])objectInfo.clone();
return model;
}
/**
* Populates the interesting and uninteresting parts of a miso scene
* model given lists of {@link ObjectInfo} records for each.
*/
public static void populateObjects (SimpleMisoSceneModel model,
ArrayList ilist, ArrayList ulist)
{
// set up the uninteresting arrays
int ucount = ulist.size();
model.objectTileIds = new int[ucount];
model.objectXs = new short[ucount];
model.objectYs = new short[ucount];
for (int ii = 0; ii < ucount; ii++) {
ObjectInfo info = (ObjectInfo)ulist.get(ii);
model.objectTileIds[ii] = info.tileId;
model.objectXs[ii] = (short)info.x;
model.objectYs[ii] = (short)info.y;
}
// set up the interesting array
int icount = ilist.size();
model.objectInfo = new ObjectInfo[icount];
ilist.toArray(model.objectInfo);
}
}