noope.laws
Class Law
java.lang.Object
|
+--noope.laws.Law
- Direct Known Subclasses:
- OutputProjection, OutputText, ParticlePairLaw
- public abstract class Law
- extends java.lang.Object
This class describes a force-law in the modelled universe.
Below is a skeleten example of how Law could be subclassed to create a useful law, for a full
implementation see the Gravity
class.
class GravityPropertyRecord extends LawPropertyRecord
{
GravityPropertyRecord(BlockReader, Entity) // get gravitation mass from BlockReader
protected double gravitationalMass;
public double getGravitationalMass();
}
class Gravity extends Law
{
Gravity(BlockReader br, int number, Physics phys)
{
super(br, number);
// do rest of initialization from br
}
LawPropertyRecord constructNewPropertyRecord(BlockReader r, Entity e)
{
return new GravityPropertyRecord(r, e);
}
public void act()
{ // for each entity in Entities
// work out force on it and call entity.addForce, by using entity.getPropertyRecord(lawnum)
}
...
}
- Version:
- 19-Apr-2001 Added dispose() [Miklos Reiter]
- Author:
- Miklos Reiter
Field Summary |
protected Physics |
physics
A reference to the physics world so getActiveForce can learn about its
environment. |
Constructor Summary |
Law(BlockReader br,
int lawnumber,
Physics phys)
Constructs a new Law object from the supplied BlockReader, with lawnumber as its number. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
physics
protected Physics physics
- A reference to the physics world so getActiveForce can learn about its
environment.
Law
public Law(BlockReader br,
int lawnumber,
Physics phys)
throws BRLoadingException
- Constructs a new Law object from the supplied BlockReader, with lawnumber as its number.
These numbers are allocated by the Laws constructor. Everything apart from the number is loaded from br.
The constructor of every derived law should follow this interface.
Subclasses should call the inherited constructor first to make sure the name is assigned.
Also, subclasses, should have a constructor taking a BlockReader and an int, so it can be called by Physics,
even though it does not know about the class.
The entries in br have to be as follows:
[name "lawName"]
(if name not given as first entry, will assume classname, i.e. parameter as the name of the law)
... (custom entries for derived laws)
...
- Parameters:
br
- BlockReader used to initialise objectlawnumber
- Number of this lawphys
- Reference to the Physics world- See Also:
Laws.Laws(BlockReader, Physics)
getName
public java.lang.String getName()
- Returns the name of the law object. This is as specified in the BlockReader object from which this Law object has
been constructed.
getNumber
public int getNumber()
- Returns the number of the law object. The number is the index of this Law in Laws and also the index of the
corresponding LawPropertyRecord in Entity.properties
act
public abstract void act()
- Make the law act upon the entities. This is called by Physics once in every time interval.
This method should calculate the net force due to this Law acting on each Entity and then call
Entity.addForce(force)
on every Entity to add this law's constribution to the total force acting on the Entity.
- Parameters:
e
- the list of entities that the force should act on- See Also:
Entity.addForce(Vector3D)
constructNewPropertyRecord
public LawPropertyRecord constructNewPropertyRecord(BlockReader br,
Entity ent)
throws BRLoadingException
- Create a new LawPropertyRecord from the BlockReader object. This is done
by calling the corresponding LawPropertyRecord's constructor with the BlockReader object.
Most subclasses will want to override this method to construct an instance of their own
LawPropertyRecord class, but this is not a requirement. If a law does not need to store
any data, it can use the default method that just returns an instance of LawPropertyRecord.
If this is what you want, pass it a null BlockReader object, or it will issue a warning.
- Parameters:
br
- BlockReader used to read properties froment
- is the instance of Entity, this LawPropertyRecord will be constructed for
toString
public java.lang.String toString()
- Provides for debug-quality printing. Print the name and number of this Law.
- Overrides:
toString
in class java.lang.Object
dispose
public void dispose()
- Dispose of system resources used by this law. This can also be used for output laws to
close any output devices like files, windows, etc. used. Added by Miklos Reiter on
19-Apr-2001.