group1.model
Class Environment

java.lang.Object
  extended by group1.model.Environment

public class Environment
extends java.lang.Object

An environment, a.k.a. a level, that contains a list of game objects. (Terrain, enemies, Jeff, etc.) In other words, the various areas in which the action will take place.

This class no longer uses a discrete grid to store the objects. Instead, it handles everything using double values. (The plan is, it *is* going to use a grid of rectangles for faster hit detection computation in larger environments. That's not quite finished yet, though.)

Note that this class and the GameObj classes technically store the data upside-down. Java treats the y-axis as top-to-bottom, which is too confusing for actual physics calculations. So, we make all calculations as though it's represented from bottom-to-top, then reverse the values later.

Also, a note on intersection: Objects actually need to sit within each other ever so slightly. See, Rectangle2D.intersects() doesn't work unless they intersect. So, if an object is moved completely outside of an object it hits, those objects are no longer touching. This can cause glitchy effects with walls and ceilings, and completely breaks any concept of standing on the floor.

Finally, note that this is a work in progress. It still has a lot of efficiency problems. In particular, the grid's set up, but not being used yet.


Constructor Summary
Environment()
          Default constructor.
Environment(Jeff jef, Controller controller, java.io.BufferedReader input)
          Constructor.
 
Method Summary
static GameObj createGameObj(char typeCode, double x, double y)
          Given an alphabetic type code, construct the corresponding type of game object.
 Controller getController()
          Get the controller object from the environment.
 double getGravity()
          Get the gravity from the environment.
 double getHeight()
          Return the "true" height of the Environment.
 XYPair getJeffsPosition()
          Get Jeff's position.
 GameObj getObject(int i)
          Return the specified object in the environment.
 java.util.List<GameObj> getObjectList()
          Return the full list of objects in the Environment.
 int getObjectListSize()
          Return the size of the list of all objects in the environment.
 double getWidth()
          Return the "true" width of the Environment.
 boolean isObjectNearLadder(GameObj obj)
          Check whether or not an object is close enough to a ladder to grab it.
 boolean isObjectOnGround(GameObj obj)
          Check whether or not an object is on ground that it can walk on.
 void setGravity(double grav)
          Set the gravity.
 int timeClick()
          Calculates and sets the positions and velocities of all objects in the environment, based upon the objects' own movements and hit detection.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Environment

public Environment()
Default constructor.


Environment

public Environment(Jeff jef,
                   Controller controller,
                   java.io.BufferedReader input)
            throws java.io.EOFException,
                   java.io.IOException
Constructor. Constructs a new Environment and initializes it based on the data provided in an environment description file, which contains the length and height of the Environment as well as all initial game objects and their locations. See "example.env" for explanation and an example of the proper format of this text file.

Parameters:
jef - The hero of the game. (The only object that carries over between environments.)
controller - The game controller. (Useful for objects that need to move.)
input - the stream that reads the environment description file, currently at the beginning of the file.
Throws:
java.io.EOFException - in case of file format error when more data was expected than given in the file.
java.io.IOException - if other input error occurs.
Method Detail

createGameObj

public static final GameObj createGameObj(char typeCode,
                                          double x,
                                          double y)
Given an alphabetic type code, construct the corresponding type of game object. Used when reading a text file for the initialization of the environment.

Parameters:
typeCode - the alphabetic type code, or '.' to specify no object.
x - The x-coordinate position.
y - The y-coordinate position.
Returns:
the newly constructed GameObj subobject using the default constructor for the subobject, or null if no object or if an invalid type code is specified.

setGravity

public void setGravity(double grav)
Set the gravity. (Otherwise, uses a default value.)

Parameters:
grav - The gravity value. (Should be negative. Unless we're falling up or something...?)

timeClick

public int timeClick()
Calculates and sets the positions and velocities of all objects in the environment, based upon the objects' own movements and hit detection. Returns a value to indicate if Jeff has died or completed the level.

This should be called by the Game class at each time click.

Returns:
-1 if Jeff dies. (Remake the Environment and start the level over.) 0 if nothing special. 1 if Jeff completes the level. (Move to the next Environment.)

isObjectOnGround

public boolean isObjectOnGround(GameObj obj)
Check whether or not an object is on ground that it can walk on. Useful for objects that can stand on the ground.

Note, like hit detection, this operation can be slightly expensive if we call it often. But, the grid rectangles should make this better later on.

For now, though, this is a fairly decent solution.

Parameters:
obj - The object in question.
Returns:
Whether or not that object is on the ground.

isObjectNearLadder

public boolean isObjectNearLadder(GameObj obj)
Check whether or not an object is close enough to a ladder to grab it. (Currently not implemented, and returns false. To be implemented later.)

Parameters:
obj - The object in question.
Returns:
Whether or not the object is near a ladder.

getGravity

public double getGravity()
Get the gravity from the environment. (Generally assumed to be negative.) Useful for any object that is affected by gravity.

Returns:
The gravity value.

getJeffsPosition

public XYPair getJeffsPosition()
Get Jeff's position. Useful for enemy AI.


getController

public Controller getController()
Get the controller object from the environment. Useful for objects that respond to controller commands, particularly Jeff.

Returns:
The controller.

getObjectList

public java.util.List<GameObj> getObjectList()
Return the full list of objects in the Environment.

Returns:
All the objects.

getObject

public GameObj getObject(int i)
Return the specified object in the environment. (Use with getObjectListSize().) You might pass this directly to the view.

Returns:
The i'th game object in the list.

getObjectListSize

public int getObjectListSize()
Return the size of the list of all objects in the environment. You might pass this directly to the view.

Returns:
The size of the list of game objects.

getHeight

public double getHeight()
Return the "true" height of the Environment.

Returns:
The height as a double value.

getWidth

public double getWidth()
Return the "true" width of the Environment.

Returns:
The width as a double value.