-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Started adding maps and implementing observers
- Loading branch information
Showing
15 changed files
with
201 additions
and
38 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
2 changes: 1 addition & 1 deletion
2
.../my/project/simulation/utils/MapArea.java → .../my/project/simulation/enums/MapArea.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package my.project.simulation.utils; | ||
package my.project.simulation.enums; | ||
|
||
public enum MapArea { | ||
JUNGLE, | ||
|
4 changes: 3 additions & 1 deletion
4
...roject/simulation/utils/MapDirection.java → ...roject/simulation/enums/MapDirection.java
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package my.project.simulation.maps; | ||
|
||
import my.project.simulation.IObserver; | ||
import my.project.simulation.enums.MapArea; | ||
import my.project.simulation.sprites.Animal; | ||
import my.project.simulation.sprites.Grass; | ||
import my.project.simulation.sprites.ISprite; | ||
import my.project.simulation.utils.EnergyComparator; | ||
import my.project.simulation.utils.Vector2D; | ||
|
||
import java.util.*; | ||
|
||
public abstract class AbstractMap implements IMap, IObserver { | ||
private final int totalWidth; | ||
private final int totalHeight; | ||
private final int jungleWidth; | ||
private final int jungleHeight; | ||
|
||
protected final Map<Vector2D, SortedSet<Animal>> mapAnimals = new HashMap<>(); | ||
protected final Map<Vector2D, Grass> mapGrass = new HashMap<>(); | ||
|
||
AbstractMap(int totalWidth, int totalHeight, int jungleWidth, int jungleHeight) { | ||
this.totalWidth = totalWidth; | ||
this.totalHeight = totalHeight; | ||
this.jungleWidth = jungleWidth; | ||
this.jungleHeight = jungleHeight; | ||
} | ||
|
||
@Override | ||
public void changeSpritePosition(ISprite sprite) throws IllegalArgumentException, NoSuchElementException { | ||
if (!(sprite instanceof Animal)) { | ||
throw new IllegalArgumentException("Cannot change position of a sprite which is not an animal"); | ||
} else { | ||
Vector2D prevPosition = ((Animal) sprite).getPrevPosition(); | ||
SortedSet<Animal> animals = mapAnimals.get(prevPosition); | ||
if (animals == null || !animals.remove(sprite)) { | ||
throw new NoSuchElementException("Sprite " + sprite + " is not on the map"); | ||
} | ||
addSprite(sprite); | ||
} | ||
} | ||
|
||
@Override | ||
public void removeSprite(ISprite sprite) throws NoSuchElementException { | ||
Vector2D position; | ||
// If a sprite is an animal object | ||
if (sprite instanceof Animal) { | ||
position = ((Animal) sprite).getCurrPosition(); | ||
SortedSet<Animal> animals = mapAnimals.get(position); | ||
if (animals == null || !animals.remove(sprite)) { | ||
throw new NoSuchElementException("Sprite " + sprite + " is not on the map"); | ||
} | ||
// If a sprite is a grass object | ||
} else if (sprite instanceof Grass) { | ||
position = ((Grass) sprite).getPosition(); | ||
if (mapGrass.remove(position) == null) { | ||
throw new NoSuchElementException("Sprite " + sprite + " is not on the map"); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void addSprite(ISprite sprite) throws IllegalArgumentException { | ||
Vector2D position; | ||
// If a sprite is an animal object | ||
if (sprite instanceof Animal) { | ||
position = ((Animal) sprite).getCurrPosition(); | ||
// Create the new animals list if there is no animals list on the specified position | ||
if (mapAnimals.get(position) == null) { | ||
mapAnimals.put(position, new TreeSet<>(new EnergyComparator())); | ||
} | ||
// Add an animal to the list | ||
mapAnimals.get(position).add((Animal)sprite); | ||
// If a sprite is a grass object | ||
} else if (sprite instanceof Grass) { | ||
position = ((Grass) sprite).getPosition(); | ||
// Place grass on a field only if there is no other element occupying this field | ||
if (mapGrass.get(position) != null || mapAnimals.get(position) != null) { | ||
String message = "Cannot place grass on field: " + position + ". Field is not empty."; | ||
throw new IllegalArgumentException(message); | ||
} | ||
mapGrass.put(position, (Grass)sprite); | ||
} | ||
} | ||
|
||
@Override | ||
public MapArea getAreaType(Vector2D position) { | ||
int x = position.getX(); | ||
int y = position.getY(); | ||
if (Math.abs(x) < (jungleWidth + 1) / 2 && Math.abs(y) < (jungleHeight + 1) / 2) { | ||
return MapArea.JUNGLE; | ||
} | ||
return MapArea.STEPPE; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package my.project.simulation.maps; | ||
|
||
import my.project.simulation.utils.Vector2D; | ||
|
||
public class FencedMap extends AbstractMap { | ||
FencedMap(int totalWidth, int totalHeight, int jungleWidth, int jungleHeight) { | ||
super(totalWidth, totalHeight, jungleWidth, jungleHeight); | ||
} | ||
|
||
@Override | ||
public Vector2D getNextPosition(Vector2D currPosition, Vector2D moveVector) { | ||
return null; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package my.project.simulation.maps; | ||
|
||
import my.project.simulation.utils.Vector2D; | ||
|
||
public class FoldingMap extends AbstractMap { | ||
FoldingMap(int totalWidth, int totalHeight, int jungleWidth, int jungleHeight) { | ||
super(totalWidth, totalHeight, jungleWidth, jungleHeight); | ||
} | ||
|
||
@Override | ||
public Vector2D getNextPosition(Vector2D currPosition, Vector2D moveVector) { | ||
return null; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,17 @@ | ||
package my.project.simulation.maps; | ||
|
||
import my.project.simulation.enums.MapArea; | ||
import my.project.simulation.sprites.ISprite; | ||
import my.project.simulation.utils.Vector2D; | ||
|
||
public interface IMap { | ||
void addSprite(ISprite sprite); | ||
|
||
void removeSprite(ISprite sprite); | ||
|
||
void changeSpritePosition(ISprite sprite); | ||
|
||
MapArea getAreaType(Vector2D position); | ||
|
||
Vector2D getNextPosition(Vector2D currPosition, Vector2D moveVector); | ||
} |
16 changes: 6 additions & 10 deletions
16
src/main/java/my/project/simulation/sprites/AbstractSprite.java
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
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
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
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
12 changes: 12 additions & 0 deletions
12
src/main/java/my/project/simulation/utils/EnergyComparator.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package my.project.simulation.utils; | ||
|
||
import my.project.simulation.sprites.Animal; | ||
|
||
import java.util.Comparator; | ||
|
||
public class EnergyComparator implements Comparator<Animal> { | ||
@Override | ||
public int compare(Animal o1, Animal o2) { | ||
return o1.getEnergy() - o2.getEnergy(); | ||
} | ||
} |
Empty file.