Created
August 5, 2016 20:42
-
-
Save xDaevax/47acc89ade649dfadf7b809cfe9cddc7 to your computer and use it in GitHub Desktop.
Java Game Example (Code-Review Question: 137930)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Translated by CS2J (http://www.cs2j.com): 8/5/2016 4:24:38 PM | |
// | |
package Game.Engine; | |
import CS2JNet.JavaSupport.util.ListSupport; | |
import java.util.ArrayList; | |
import java.util.LinkedList; | |
import java.util.List; | |
public class Environment | |
{ | |
private Collection<GameObject> _objects; | |
private boolean _environmentLock; | |
public Environment() { | |
this._objects = new Collection<GameObject>(); | |
this._environmentLock = false; | |
} | |
protected static void onObjectPositionChanged(Object sender, ChangePositionEventArgs args) { | |
args.Dump(); | |
} | |
public void addObstacle(GameObject obstacle) { | |
this._objects.Add(obstacle); | |
} | |
public void initialize() throws InvalidOperationException { | |
if (!this._environmentLock) | |
{ | |
for (GameObject item : this._objects) | |
{ | |
if ((item instanceof IChangesPosition ? (IChangesPosition)item : (IChangesPosition)null) != null) | |
{ | |
((IChangesPosition)item).Changed += OnObjectPositionChanged; | |
} | |
} | |
} | |
else | |
{ | |
throw new InvalidOperationException("Environment has already been locked for playing and cannot be modified."); | |
} | |
} | |
public void prepare() { | |
this._environmentLock = true; | |
} | |
public Collection<GameObject> getObjects() { | |
return this._objects; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Translated by CS2J (http://www.cs2j.com): 8/5/2016 4:24:38 PM | |
// | |
package Game.Engine; | |
import CS2JNet.JavaSupport.util.ListSupport; | |
import Game.Engine.Environment; | |
import java.util.ArrayList; | |
import java.util.LinkedList; | |
import java.util.List; | |
public class GameEngine | |
{ | |
private Environment _gameEnvironment; | |
public GameEngine() { | |
this._gameEnvironment = new Environment(); | |
} | |
public void generateObstacle() { | |
PositionalObject block = new Block(); | |
block.Dimensions.Add(Dimensions.Length(60, SpatialUnit.Pixels)); | |
block.Dimensions.Add(Dimensions.Width(20, SpatialUnit.Pixels)); | |
this._gameEnvironment.AddObstacle(block); | |
} | |
public void setup() throws Exception { | |
this.getEnvironment().initialize(); | |
this.getEnvironment().prepare(); | |
} | |
public Environment getEnvironment() { | |
return this._gameEnvironment; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Translated by CS2J (http://www.cs2j.com): 8/5/2016 4:24:38 PM | |
// | |
package Game.Interactions; | |
import CS2JNet.JavaSupport.util.ListSupport; | |
import Game.Interactions.__MultiPositionChangedEventHandler; | |
import Game.Interactions.ChangePositionEventArgs; | |
import Game.Interactions.PositionChangedEventHandler; | |
import java.util.ArrayList; | |
import java.util.LinkedList; | |
import java.util.List; | |
public class __MultiPositionChangedEventHandler implements PositionChangedEventHandler | |
{ | |
public void invoke(Object sender, ChangePositionEventArgs args) throws Exception { | |
IList<PositionChangedEventHandler> copy = new IList<PositionChangedEventHandler>(), members = this.getInvocationList(); | |
synchronized (members) | |
{ | |
copy = new LinkedList<PositionChangedEventHandler>(members); | |
} | |
for (Object __dummyForeachVar0 : copy) | |
{ | |
PositionChangedEventHandler d = (PositionChangedEventHandler)__dummyForeachVar0; | |
d.invoke(sender, args); | |
} | |
} | |
private List<PositionChangedEventHandler> _invocationList = new ArrayList<PositionChangedEventHandler>(); | |
public static PositionChangedEventHandler combine(PositionChangedEventHandler a, PositionChangedEventHandler b) throws Exception { | |
if (a == null) | |
return b; | |
if (b == null) | |
return a; | |
__MultiPositionChangedEventHandler ret = new __MultiPositionChangedEventHandler(); | |
ret._invocationList = a.getInvocationList(); | |
ret._invocationList.addAll(b.getInvocationList()); | |
return ret; | |
} | |
public static PositionChangedEventHandler remove(PositionChangedEventHandler a, PositionChangedEventHandler b) throws Exception { | |
if (a == null || b == null) | |
return a; | |
List<PositionChangedEventHandler> aInvList = a.getInvocationList(); | |
List<PositionChangedEventHandler> newInvList = ListSupport.removeFinalStretch(aInvList, b.getInvocationList()); | |
if (aInvList == newInvList) | |
{ | |
return a; | |
} | |
else | |
{ | |
__MultiPositionChangedEventHandler ret = new __MultiPositionChangedEventHandler(); | |
ret._invocationList = newInvList; | |
return ret; | |
} | |
} | |
public List<PositionChangedEventHandler> getInvocationList() { | |
return _invocationList; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Translated by CS2J (http://www.cs2j.com): 8/5/2016 4:24:38 PM | |
// | |
package Game.Interactions; | |
import CS2JNet.JavaSupport.util.ListSupport; | |
import Game.Interactions.Position; | |
import Game.Interactions.Vector; | |
import java.util.ArrayList; | |
import java.util.LinkedList; | |
import java.util.List; | |
public class ChangePositionEventArgs | |
{ | |
private Position _from; | |
private Vector _to; | |
public ChangePositionEventArgs(Position from, Vector to) { | |
this._from = from; | |
this._to = to; | |
} | |
public Position getFrom() { | |
return this._from; | |
} | |
public Vector getTo() { | |
return this._to; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Translated by CS2J (http://www.cs2j.com): 8/5/2016 4:24:38 PM | |
// | |
package Game.Interactions; | |
import CS2JNet.JavaSupport.language.RefSupport; | |
import CS2JNet.JavaSupport.util.ListSupport; | |
import Game.Interactions.LinearMovement; | |
import Game.Interactions.Position; | |
import java.util.ArrayList; | |
import java.util.LinkedList; | |
import java.util.List; | |
public class HorizontalMovement extends LinearMovement | |
{ | |
private String _name; | |
private String _description; | |
public HorizontalMovement(Position to, String name) { | |
super(to); | |
this._name = name; | |
} | |
public void perform(RefSupport<Position> source) throws Exception { | |
if (source.getValue().getY() == this.getTo().getY()) | |
{ | |
RefSupport<Position> refVar___0 = new RefSupport<Position>(source.getValue()); | |
super.perform(refVar___0); | |
source.setValue(refVar___0.getValue()); | |
} | |
else | |
{ | |
source.getValue().setX(this.getTo().getX()); | |
} | |
} | |
public String getName(){ | |
return this._name; | |
} | |
public String getDescription() { | |
return this._description; | |
} | |
public void setDescription(String value){ | |
this._description = value; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Translated by CS2J (http://www.cs2j.com): 8/5/2016 4:24:38 PM | |
// | |
package Game.Interactions; | |
import CS2JNet.JavaSupport.language.IEventCollection; | |
import CS2JNet.JavaSupport.util.ListSupport; | |
import Game.Interactions.PositionChangedEventHandler; | |
import java.util.ArrayList; | |
import java.util.LinkedList; | |
import java.util.List; | |
public interface IChangesPosition | |
{ | |
IEventCollection<PositionChangedEventHandler> Changed(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Translated by CS2J (http://www.cs2j.com): 8/5/2016 4:24:38 PM | |
// | |
package Game.Interactions; | |
import CS2JNet.JavaSupport.util.ListSupport; | |
import Game.Interactions.Movement; | |
import java.util.ArrayList; | |
import java.util.LinkedList; | |
import java.util.List; | |
public interface IMovable | |
{ | |
void move(Movement movement); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Translated by CS2J (http://www.cs2j.com): 8/5/2016 4:24:38 PM | |
// | |
package Game.Interactions; | |
import CS2JNet.JavaSupport.language.RefSupport; | |
import CS2JNet.JavaSupport.util.ListSupport; | |
import Game.Interactions.Movement; | |
import Game.Interactions.Position; | |
import java.util.ArrayList; | |
import java.util.LinkedList; | |
import java.util.List; | |
public abstract class LinearMovement extends Movement | |
{ | |
private Position _to; | |
protected LinearMovement(Position to) { | |
this._to = to; | |
} | |
public void perform(RefSupport<Position> source) { | |
source.getValue().setX(this.getTo().getX()); | |
source.getValue().setY(this.getTo().getY()); | |
} | |
protected Position getTo() { | |
return this._to; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Translated by CS2J (http://www.cs2j.com): 8/5/2016 4:24:38 PM | |
// | |
package Game.Interactions; | |
import CS2JNet.JavaSupport.language.RefSupport; | |
import CS2JNet.JavaSupport.util.ListSupport; | |
import Game.Interactions.Position; | |
import java.util.ArrayList; | |
import java.util.LinkedList; | |
import java.util.List; | |
public abstract class Movement | |
{ | |
private int _speed; | |
protected Movement() { | |
this._speed = 1; | |
} | |
public abstract void perform(RefSupport<Position> source); | |
public Integer getSpeed() { | |
return this._speed; | |
} | |
public void setSpeed(int value) { | |
this._speed = value; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Translated by CS2J (http://www.cs2j.com): 8/5/2016 4:24:38 PM | |
// | |
package Game.Interactions; | |
import CS2JNet.JavaSupport.util.ListSupport; | |
import java.util.ArrayList; | |
import java.util.LinkedList; | |
import java.util.List; | |
public class Position | |
{ | |
private int _x; | |
private int _y; | |
public Position(int x, int y) { | |
this._x = x; | |
this._y = y; | |
} | |
public int getX() { | |
return this._x; | |
} | |
public void setX(int value) { | |
this._x = value; | |
} | |
public int getY() { | |
return this._y; | |
} | |
public void setY(int value) { | |
this._y = value; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Translated by CS2J (http://www.cs2j.com): 8/5/2016 4:24:38 PM | |
// | |
package Game.Interactions; | |
import CS2JNet.JavaSupport.util.ListSupport; | |
import Game.Interactions.ChangePositionEventArgs; | |
import Game.Interactions.PositionChangedEventHandler; | |
import java.util.ArrayList; | |
import java.util.LinkedList; | |
import java.util.List; | |
public interface PositionChangedEventHandler | |
{ | |
void invoke(Object sender, ChangePositionEventArgs args); | |
List<PositionChangedEventHandler> getInvocationList(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Translated by CS2J (http://www.cs2j.com): 8/5/2016 4:24:38 PM | |
// | |
package Game.Interactions; | |
import CS2JNet.JavaSupport.util.ListSupport; | |
import Game.Interactions.Position; | |
import java.util.ArrayList; | |
import java.util.LinkedList; | |
import java.util.List; | |
public class Vector | |
{ | |
private int _speed; | |
private Position _targetPosition; | |
public Vector(int x, int y, int speed) { | |
this._targetPosition = new Position(x,y); | |
this._speed = speed; | |
} | |
public int getSpeed() { | |
return this._speed; | |
} | |
public void setSpeed(int value) { | |
this._speed = value; | |
} | |
public Position getTargetPosition() { | |
return this._targetPosition; | |
} | |
public void setTargetPosition(Position value) { | |
this._targetPosition = value; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Translated by CS2J (http://www.cs2j.com): 8/5/2016 4:24:38 PM | |
// | |
package Game.Mechanics; | |
import CS2JNet.JavaSupport.util.ListSupport; | |
import Game.Mechanics.SpatialUnit; | |
import java.util.ArrayList; | |
import java.util.LinkedList; | |
import java.util.List; | |
public class Dimension | |
{ | |
private String _label; | |
private SpatialUnit _unit; | |
private double _value; | |
public Dimension(String label) { | |
this._label = label; | |
this._unit = SpatialUnit.Pixels; | |
this._value = 0.0; | |
} | |
public String getLabel() { | |
return this._label; | |
} | |
public SpatialUnit getUnit() { | |
return this._unit; | |
} | |
public void setUnit(SpatialUnit value) { | |
this._unit = value; | |
} | |
public double getValue() { | |
return this._value; | |
} | |
public void setValue(double value) { | |
this._value = value; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Translated by CS2J (http://www.cs2j.com): 8/5/2016 4:24:38 PM | |
// | |
package Game.Mechanics; | |
import CS2JNet.JavaSupport.language.RefSupport; | |
import CS2JNet.JavaSupport.util.ListSupport; | |
import Game.Mechanics.Dimension; | |
import Game.Mechanics.SpatialUnit; | |
import java.util.ArrayList; | |
import java.util.LinkedList; | |
import java.util.List; | |
public class Dimensions | |
{ | |
private static void populateDimension(RefSupport<Dimension> dimension, double value, SpatialUnit unit) { | |
dimension.getValue().setValue(value); | |
dimension.getValue().setUnit(unit); | |
} | |
public static Dimension height(double value, SpatialUnit unit) { | |
Dimension returnValue = new Dimension("Height"); | |
RefSupport<Dimension> refVar___0 = new RefSupport<Dimension>(returnValue); | |
populateDimension(refVar___0,value,unit); | |
returnValue = refVar___0.getValue(); | |
return returnValue; | |
} | |
public static Dimension width(double value, SpatialUnit unit) { | |
Dimension returnValue = new Dimension("Width"); | |
RefSupport<Dimension> refVar___1 = new RefSupport<Dimension>(returnValue); | |
populateDimension(refVar___1,value,unit); | |
returnValue = refVar___1.getValue(); | |
return returnValue; | |
} | |
public static Dimension length(double value, SpatialUnit unit) { | |
Dimension returnValue = new Dimension("Length"); | |
RefSupport<Dimension> refVar___2 = new RefSupport<Dimension>(returnValue); | |
populateDimension(refVar___2,value,unit); | |
returnValue = refVar___2.getValue(); | |
return returnValue; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Translated by CS2J (http://www.cs2j.com): 8/5/2016 4:24:38 PM | |
// | |
package Game.Mechanics; | |
import CS2JNet.JavaSupport.util.ListSupport; | |
import Game.Mechanics.Dimension; | |
import java.util.ArrayList; | |
import java.util.LinkedList; | |
import java.util.List; | |
public interface IHasDimensions | |
{ | |
Collection<Dimension> getDimensions(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Translated by CS2J (http://www.cs2j.com): 8/5/2016 4:24:38 PM | |
// | |
package Game.Mechanics; | |
import CS2JNet.JavaSupport.util.ListSupport; | |
import java.util.ArrayList; | |
import java.util.LinkedList; | |
import java.util.List; | |
public enum SpatialUnit | |
{ | |
Pixels, | |
Centimeters, | |
Inches, | |
Millimeters | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Translated by CS2J (http://www.cs2j.com): 8/5/2016 4:24:38 PM | |
// | |
package Game.Objects; | |
import CS2JNet.JavaSupport.util.ListSupport; | |
import Game.Objects.ImpassableObject; | |
import java.util.ArrayList; | |
import java.util.LinkedList; | |
import java.util.List; | |
public class Block extends ImpassableObject | |
{ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Translated by CS2J (http://www.cs2j.com): 8/5/2016 4:24:38 PM | |
// | |
package Game.Objects; | |
import CS2JNet.JavaSupport.util.ListSupport; | |
import java.util.ArrayList; | |
import java.util.LinkedList; | |
import java.util.List; | |
public abstract class GameObject extends IHasDimensions | |
{ | |
private Collection<Dimension> _dimensions; | |
private String _name; | |
protected GameObject() { | |
this._dimensions = new Collection<Dimension>(); | |
this._name = ""; | |
} | |
public String getName() { | |
return this._name; | |
} | |
public void setName(String value) { | |
this._name = value; | |
} | |
public Collection<Dimension> getDimensions() { | |
return this._dimensions; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Translated by CS2J (http://www.cs2j.com): 8/5/2016 4:24:38 PM | |
// | |
package Game.Objects; | |
import CS2JNet.JavaSupport.util.ListSupport; | |
import Game.Objects.PositionalObject; | |
import java.util.ArrayList; | |
import java.util.LinkedList; | |
import java.util.List; | |
public abstract class ImpassableObject extends PositionalObject | |
{ | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Translated by CS2J (http://www.cs2j.com): 8/5/2016 4:24:38 PM | |
// | |
package Game.Objects; | |
import CS2JNet.JavaSupport.language.RefSupport; | |
import CS2JNet.JavaSupport.util.ListSupport; | |
import Game.Objects.GameObject; | |
import java.util.ArrayList; | |
import java.util.LinkedList; | |
import java.util.List; | |
public abstract class PositionalObject extends GameObject implements IChangesPosition, IMovable | |
{ | |
private Position _currentPosition; | |
protected PositionalObject() { | |
super(); | |
this._currentPosition = new Position(0, 0); | |
} | |
// Default initialization | |
public void move(Movement movement) throws Exception { | |
Position previous = new Position(this.getCurrentPosition().X, this.getCurrentPosition().Y); | |
Position current = this.getCurrentPosition(); | |
RefSupport<Position> refVar___0 = new RefSupport<Position>(current); | |
movement.Perform(refVar___0); | |
current = refVar___0.getValue(); | |
// Only invoke the event if there are attached listeners. | |
if (this.Changed != null) | |
{ | |
this.Changed.Invoke(this, new ChangePositionEventArgs(previous, new Vector(current.X, current.Y, movement.Speed))); | |
} | |
} | |
public PositionChangedEventHandler Changed = new PositionChangedEventHandler(); | |
public Position getCurrentPosition() { | |
return this._currentPosition; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment