Skip to content

Commit

Permalink
Implement Undo feature (#2).
Browse files Browse the repository at this point in the history
Also refactored the direction heading from an int to a enum.
  • Loading branch information
theKidOfArcrania committed May 30, 2017
1 parent ee36268 commit c496c78
Show file tree
Hide file tree
Showing 50 changed files with 1,080 additions and 599 deletions.
45 changes: 45 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions .idea/uiDesigner.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions Convertor/src/turtle/editor/TMXToMTP.java
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,16 @@ public static void main(String[] args) throws Exception
return;
if (!file.contains("."))
file += ".mtp";

try
{
LevelPack prev = new LevelPack(new File(file));
pck.setLevelPackID(prev.getLevelPackID());
}
catch (Exception e)
{
//Does nothing
}
pck.savePack(new File(file));
}

Expand Down Expand Up @@ -357,6 +367,18 @@ private static Object parseValue(String loc, String prop, String val)
"short property: '" + prop + "' --> '" + val + "'");
return null;
}
case "direction":
try
{
int num = Integer.parseInt(parts[1]);
return Direction.values()[num];
}
catch (Exception e)
{
System.err.println("WARNING: " + loc + " has an invalid " +
"direction property: '" + prop + "' --> '" + val + "'");
return null;
}
case "int":
try
{
Expand Down
28 changes: 27 additions & 1 deletion src/turtle/attributes/Attributable.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,47 @@
package turtle.attributes;

import java.io.Serializable;

/**
* This interface encompasses any object that contains a set of attributes that can be set.
*
* @author Henry
*
*/
public interface Attributable {
public interface Attributable extends Serializable
{

/**
* Gets the value of a particular attribute.
*
* @param name the name of the attribute.
* @return the value (or null if attribute does not exist)
* @see AttributeSet#getAttribute(String)
*/
default Object getAttribute(String name) {
return getAttributeSet().getAttribute(name);
}

/**
* Gets the value of a particular attribute.
*
* @param name the name of the attribute.
* @param def the default value to return.
* @return the value.
* @see AttributeSet#getAttribute(String, Object)
*/
default Object getAttribute(String name, Object def) {
return getAttributeSet().getAttribute(name, def);
}

/**
* Sets the value of a particular attribute.
*
* @param name the name of the attribute.
* @param value the value to set.
* @throws IllegalArgumentException if value cannot cast into attribute type
* @see AttributeSet#setAttribute(String, Object)
*/
default void setAttribute(String name, Object value) {
getAttributeSet().setAttribute(name, value);
}
Expand Down
2 changes: 2 additions & 0 deletions src/turtle/attributes/Attribute.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
* Attribute.java
*
* Holds the getters/setters of a particular attribute.
*
* @author Henry Wang
*/
public class Attribute
{
Expand Down
27 changes: 25 additions & 2 deletions src/turtle/attributes/AttributeSet.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package turtle.attributes;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.*;
Expand All @@ -13,7 +16,7 @@
* @author Henry
* @see turtle.attributes.Attributable
*/
public class AttributeSet<A extends Attributable>
public class AttributeSet<A extends Attributable> implements Serializable
{

@SuppressWarnings("unused")
Expand All @@ -23,7 +26,8 @@ public class AttributeSet<A extends Attributable>
private static final String SET_PREFIX = "set";
private static final HashMap<Class<?>, Attribute[]> attributeCache = new
HashMap<>();
private final Hashtable<String, Attribute> attrs = new Hashtable<>();

private transient Hashtable<String, Attribute> attrs;
private final A attributable;

/**
Expand All @@ -33,6 +37,7 @@ public class AttributeSet<A extends Attributable>
public AttributeSet(A obj)
{
attributable = obj;
attrs = new Hashtable<>();
if (!attributeCache.containsKey(obj.getClass()))
extractAttributes(attributable);
for (Attribute attr : attributeCache.get(obj.getClass()))
Expand Down Expand Up @@ -271,4 +276,22 @@ public void setAttribute(String name, Object value)
attr.set(attributable, value);
}

/**
* Reads this object from the provided input stream.
*
* @param in the input stream to read from
* @throws IOException if an I/O error occurs
* @throws ClassNotFoundException if a class cannot be found.
*/
private void readObject(ObjectInputStream in)
throws IOException, ClassNotFoundException
{
in.defaultReadObject();
attrs = new Hashtable<>();
Attributable obj = getAttributable();
if (!attributeCache.containsKey(obj.getClass()))
extractAttributes(attributable);
for (Attribute attr : attributeCache.get(obj.getClass()))
attrs.put(attr.getName(), attr);
}
}
1 change: 1 addition & 0 deletions src/turtle/attributes/NotAttribute.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* NotAttribute.java
*
* This annotation remarks that a particular method is not an attribute.
* @author Henry
*/
@Documented
@Retention(value = RetentionPolicy.RUNTIME)
Expand Down
55 changes: 23 additions & 32 deletions src/turtle/comp/Bird.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package turtle.comp;

import turtle.core.Direction;
import turtle.core.Location;

/**
Expand All @@ -14,22 +15,12 @@
*/
public class Bird extends Enemy
{
/**
* The default image for this component
*/
public static final int DEFAULT_IMAGE = 52;

private static final int BIRD_STILL_IMAGE = 52;
private static final int BIRD_STILL_IMAGE = DEFAULT_IMAGE;
private static final int BIRD_FLYING_IMAGE = 53;

private static final long serialVersionUID = -387220900586289450L;

/**
* Creates a new bird and initializes image.
*/
public Bird()
{
setImageFrame(BIRD_STILL_IMAGE);
}

/**
* Updates frames so that the bird will change between flying/ still
Expand All @@ -47,9 +38,9 @@ public void updateFrame(long frame)
Player p = getParentGrid().getPlayer();
if (p != null)
{
int[] movement = calculateDirection();
Direction[] movement = calculateDirection();
boolean moved = false;
for (int dir : movement)
for (Direction dir : movement)
{
if (traverseDirection(dir))
{
Expand All @@ -62,7 +53,7 @@ public void updateFrame(long frame)
if (!moved)
{
setImageFrame(BIRD_STILL_IMAGE);
setHeading(NORTH);
setHeading(Direction.NORTH);
}
}
}
Expand All @@ -74,45 +65,45 @@ public void updateFrame(long frame)
*
* @return a list of possible directions to move into.
*/
private int[] calculateDirection()
private Direction[] calculateDirection()
{
Player player = getParentGrid().getPlayer();
if (player == null)
return new int[0];
return new Direction[0];

Location playerLoc = player.getHeadLocation();
Location loc = getHeadLocation();
if (!playerLoc.isValidLocation() || !loc.isValidLocation())
return new int[0];
return new Direction[0];

int dr = playerLoc.getRow() - loc.getRow();
int dc = playerLoc.getColumn() - loc.getColumn();

int rowDir = -1;
int colDir = -1;
Direction rowDir = null;
Direction colDir = null;

if (dr < 0)
rowDir = NORTH;
rowDir = Direction.NORTH;
else if (dr > 0)
rowDir = SOUTH;
rowDir = Direction.SOUTH;

if (dc < 0)
colDir = WEST;
colDir = Direction.WEST;
else if (dc > 0)
colDir = EAST;
colDir = Direction.EAST;

if (rowDir == -1 && colDir == -1)
return new int[0];
if (rowDir == null && colDir == null)
return new Direction[0];

if (rowDir == -1)
return new int[]{colDir};
if (colDir == -1)
return new int[]{rowDir};
if (rowDir == null)
return new Direction[]{colDir};
if (colDir == null)
return new Direction[]{rowDir};

if (Math.abs(dr) > Math.abs(dc))
{
return new int[]{rowDir, colDir};
return new Direction[]{rowDir, colDir};
} else
return new int[]{colDir, rowDir};
return new Direction[]{colDir, rowDir};
}
}
Loading

0 comments on commit c496c78

Please sign in to comment.