Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

initial commit

  • Loading branch information...
commit 16445760bab9abf03f5d8592e5ce7e2ae8ca1a8b 0 parents
Gornova authored
Showing with 2,046 additions and 0 deletions.
  1. +14 −0 .classpath
  2. +17 −0 .project
  3. +12 −0 .settings/org.eclipse.jdt.core.prefs
  4. +7 −0 config/config.txt
  5. +18 −0 config/create_exe.bat
  6. BIN  data/128.png
  7. BIN  data/16.png
  8. BIN  data/32.png
  9. BIN  data/64.png
  10. BIN  data/8.png
  11. BIN  data/ball.png
  12. BIN  data/bar.png
  13. +18 −0 data/base.tmx
  14. BIN  data/block.png
  15. BIN  data/bullet.png
  16. BIN  data/cross.png
  17. BIN  data/data/stone_01.png
  18. BIN  data/data/stone_02.png
  19. BIN  data/data/whiteblock_01.png
  20. +18 −0 data/level1.tmx
  21. BIN  data/link.png
  22. BIN  data/sword.png
  23. BIN  data/tiles.png
  24. BIN  lib/lwjgl.jar
  25. BIN  lib/native/OpenAL32.dll
  26. BIN  lib/native/jinput-dx8.dll
  27. BIN  lib/native/jinput-raw.dll
  28. BIN  lib/native/libjinput-linux.so
  29. BIN  lib/native/libjinput-linux64.so
  30. BIN  lib/native/libjinput-osx.jnilib
  31. BIN  lib/native/liblwjgl.jnilib
  32. BIN  lib/native/liblwjgl.so
  33. BIN  lib/native/liblwjgl64.so
  34. BIN  lib/native/libopenal.so
  35. BIN  lib/native/lwjgl.dll
  36. BIN  lib/native/openal.dylib
  37. BIN  lib/slick-sources.jar
  38. BIN  lib/slick.jar
  39. +6 −0 readme.txt
  40. +161 −0 src/it/randomtower/engine/AIControlledSpaceShip.java
  41. +90 −0 src/it/randomtower/engine/BallActor.java
  42. +41 −0 src/it/randomtower/engine/Bullet.java
  43. +39 −0 src/it/randomtower/engine/Camera.java
  44. +266 −0 src/it/randomtower/engine/Entity.java
  45. +167 −0 src/it/randomtower/engine/ME.java
  46. +25 −0 src/it/randomtower/engine/Map.java
  47. +102 −0 src/it/randomtower/engine/PongBarActor.java
  48. +155 −0 src/it/randomtower/engine/ResizeSquare.java
  49. +50 −0 src/it/randomtower/engine/StaticActor.java
  50. +213 −0 src/it/randomtower/engine/Sword.java
  51. +204 −0 src/it/randomtower/engine/TopDownActor.java
  52. +69 −0 test/it/randomtower/test/MoveAvatarTest.java
  53. +181 −0 test/it/randomtower/test/PongTest.java
  54. +92 −0 test/it/randomtower/test/ResizeTest.java
  55. +81 −0 test/it/randomtower/test/SpaceDefenseTest.java
14 .classpath
@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+ <classpathentry kind="src" path="test"/>
+ <classpathentry kind="src" path="src"/>
+ <classpathentry kind="src" path="data"/>
+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
+ <classpathentry kind="lib" path="lib/slick.jar" sourcepath="lib/slick-sources.jar"/>
+ <classpathentry kind="lib" path="lib/lwjgl.jar">
+ <attributes>
+ <attribute name="org.eclipse.jdt.launching.CLASSPATH_ATTR_LIBRARY_PATH_ENTRY" value="MarteEngine/lib/native"/>
+ </attributes>
+ </classpathentry>
+ <classpathentry kind="output" path="bin"/>
+</classpath>
17 .project
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>MarteEngine</name>
+ <comment></comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ <buildCommand>
+ <name>org.eclipse.jdt.core.javabuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>org.eclipse.jdt.core.javanature</nature>
+ </natures>
+</projectDescription>
12 .settings/org.eclipse.jdt.core.prefs
@@ -0,0 +1,12 @@
+#Fri Aug 20 14:44:50 CEST 2010
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
+org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
+org.eclipse.jdt.core.compiler.compliance=1.5
+org.eclipse.jdt.core.compiler.debug.lineNumber=generate
+org.eclipse.jdt.core.compiler.debug.localVariable=generate
+org.eclipse.jdt.core.compiler.debug.sourceFile=generate
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.source=1.5
7 config/config.txt
@@ -0,0 +1,7 @@
+# EXE J CONFIG
+#
+# The command line arguments that will be
+# passed to java.exe/javaw.exe when the user
+# runs the resulting .exe files.
+#
+commandline=-classpath . -jar pong.jar
18 config/create_exe.bat
@@ -0,0 +1,18 @@
+@echo off
+
+echo This example generates .exe "batch" files for
+echo a small java appliction. The java application
+echo is called TestClass2 and it shows a popup
+echo with the arguments passed to the executable.
+echo ***
+
+echo ..\..\exej.exe -cfg config.txt
+exej.exe -cfg config.txt
+
+echo ***
+echo exe files generated in this directory.
+echo Run the executables, for example:
+echo using_javaw.exe arg1 arg2 arg3
+
+
+
BIN  data/128.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN  data/16.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN  data/32.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN  data/64.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN  data/8.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN  data/ball.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN  data/bar.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 data/base.tmx
@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE map SYSTEM "http://mapeditor.org/dtd/1.0/map.dtd">
+<map version="1.0" orientation="orthogonal" width="100" height="100" tilewidth="32" tileheight="32">
+ <tileset name="stone_01" firstgid="1" tilewidth="32" tileheight="32">
+ <image source="data/stone_01.png"/>
+ <tile id="0">
+ <properties>
+ <property name="solid" value="true"/>
+ </properties>
+ </tile>
+ </tileset>
+ <!-- Layer data is compressed (GZip) binary data, encoded in Base64 -->
+ <layer name="BLOCKS" width="100" height="100">
+ <data encoding="base64" compression="gzip">
+ H4sIAAAAAAAAAO3RQQ0AAAyEsJ1/07PRB01QwO5uRRVDPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPywrrgdQWYlKQJwAAA==
+ </data>
+ </layer>
+</map>
BIN  data/block.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN  data/bullet.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN  data/cross.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN  data/data/stone_01.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN  data/data/stone_02.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN  data/data/whiteblock_01.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 data/level1.tmx
@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE map SYSTEM "http://mapeditor.org/dtd/1.0/map.dtd">
+<map version="1.0" orientation="orthogonal" width="100" height="100" tilewidth="32" tileheight="32">
+ <tileset name="stone_01" firstgid="1" tilewidth="32" tileheight="32">
+ <image source="data/stone_01.png"/>
+ <tile id="0">
+ <properties>
+ <property name="solid" value="true"/>
+ </properties>
+ </tile>
+ </tileset>
+ <!-- Layer data is compressed (GZip) binary data, encoded in Base64 -->
+ <layer name="BLOCKS" width="100" height="100">
+ <data encoding="base64" compression="gzip">
+ H4sIAAAAAAAAAO3RQQ0AAAyEsJ1/07PRB01QwO5uRRVDPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPywrrgdQWYlKQJwAAA==
+ </data>
+ </layer>
+</map>
BIN  data/link.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN  data/sword.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN  data/tiles.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN  lib/lwjgl.jar
Binary file not shown
BIN  lib/native/OpenAL32.dll
Binary file not shown
BIN  lib/native/jinput-dx8.dll
Binary file not shown
BIN  lib/native/jinput-raw.dll
Binary file not shown
BIN  lib/native/libjinput-linux.so
Binary file not shown
BIN  lib/native/libjinput-linux64.so
Binary file not shown
BIN  lib/native/libjinput-osx.jnilib
Binary file not shown
BIN  lib/native/liblwjgl.jnilib
Binary file not shown
BIN  lib/native/liblwjgl.so
Binary file not shown
BIN  lib/native/liblwjgl64.so
Binary file not shown
BIN  lib/native/libopenal.so
Binary file not shown
BIN  lib/native/lwjgl.dll
Binary file not shown
BIN  lib/native/openal.dylib
Binary file not shown
BIN  lib/slick-sources.jar
Binary file not shown
BIN  lib/slick.jar
Binary file not shown
6 readme.txt
@@ -0,0 +1,6 @@
+Marte Engine v0.1
+
+http://thedoglion.wordpress.com/2010/08/09/flashpunk-tutorial-02-loading-and-reading-ogmo-maps-creating-tilemaps/
+
+todo:
+* refactor topdown component per muovere su gi� ecc.. anche in altri! animazione annidata in update!
161 src/it/randomtower/engine/AIControlledSpaceShip.java
@@ -0,0 +1,161 @@
+package it.randomtower.engine;
+import org.newdawn.slick.GameContainer;
+import org.newdawn.slick.Graphics;
+import org.newdawn.slick.Image;
+import org.newdawn.slick.SlickException;
+import org.newdawn.slick.geom.Vector2f;
+
+public class AIControlledSpaceShip extends Entity {
+
+ public static final String NAME = "player";
+
+ private static final int HEIGHT = 28;
+ private static final int WIDTH = 23;
+
+ public static final String STAND_DOWN = "stand_down";
+ public static final String STAND_UP = "stand_up";
+ public static final String STAND_RIGHT = "stand_right";
+ public static final String STAND_LEFT = "stand_left";
+
+ public Vector2f mySpeed = new Vector2f(10, 10);
+
+ public boolean attacking = false;
+
+ public AIControlledSpaceShip(float x, float y, String ref) {
+ super(x, y);
+
+ // set id
+ name = NAME;
+
+ // load spriteSheet
+ if (ref != null)
+ setupAnimations(ref);
+
+ // player rendered above everything
+ zLevel = ME.Z_LEVEL_TOP;
+
+ // define labels for the key
+ defineControls();
+
+ // define collision box and type
+ setHitBox(0, 0, WIDTH, HEIGHT);
+ addType(NAME);
+ }
+
+ private void defineControls() {
+ }
+
+ public void setupAnimations(String ref) {
+ try {
+ setGraphic(new Image(ref));
+ } catch (SlickException e) {
+ e.printStackTrace();
+ }
+ }
+
+ @Override
+ public void update(GameContainer container, int delta)
+ throws SlickException {
+ super.update(container, delta);
+
+ // movements
+ updateMovements();
+ }
+
+ private void updateMovements() {
+ if (collide(ME.SOLID,x+mySpeed.x*2,y)){
+ if (!collide(ME.SOLID,x,y+mySpeed.y)){
+ moveDown();
+ } else {
+ moveUp();
+ }
+ }
+ }
+
+ public void moveLeft() {
+ if (!collide(ME.SOLID, x - mySpeed.x, y)) {
+ x -= mySpeed.x;
+ }
+ }
+
+ public void moveRight() {
+ if (!collide(ME.SOLID, x + mySpeed.x, y)) {
+ x += mySpeed.x;
+ }
+ }
+
+ public void moveDown() {
+ if (!collide(ME.SOLID, x, y + mySpeed.y) && y+mySpeed.y < ME.container.getHeight() ) {
+ y += mySpeed.y;
+ }
+ }
+
+ public void moveUp() {
+ if (!collide(ME.SOLID, x, y - mySpeed.y) && y+mySpeed.y > 0) {
+ y -= mySpeed.y;
+ }
+ }
+
+ @Override
+ public void render(GameContainer container, Graphics g)
+ throws SlickException {
+ super.render(container, g);
+ }
+
+ public boolean isRightMoving() {
+ if (currentAnim.equalsIgnoreCase(ME.WALK_RIGHT)) {
+ return true;
+ }
+ return false;
+ }
+
+ public boolean isLeftMoving() {
+ if (currentAnim.equalsIgnoreCase(ME.WALK_LEFT)) {
+ return true;
+ }
+ return false;
+ }
+
+ public boolean isUpMoving() {
+ if (currentAnim.equalsIgnoreCase(ME.WALK_UP)) {
+ return true;
+ }
+ return false;
+ }
+
+ public boolean isDownMoving() {
+ if (currentAnim.equalsIgnoreCase(ME.WALK_DOWN)) {
+ return true;
+ }
+ return false;
+ }
+
+ public boolean isRightStanding() {
+ if (currentAnim.equalsIgnoreCase(AIControlledSpaceShip.STAND_RIGHT)) {
+ return true;
+ }
+ return false;
+ }
+
+ public boolean isLeftStanding() {
+ if (currentAnim.equalsIgnoreCase(AIControlledSpaceShip.STAND_LEFT)) {
+ return true;
+ }
+ return false;
+ }
+
+ public boolean isUpStanding() {
+ if (currentAnim.equalsIgnoreCase(AIControlledSpaceShip.STAND_UP)) {
+ return true;
+ }
+ return false;
+ }
+
+ public boolean isDownStanding() {
+ if (currentAnim.equalsIgnoreCase(AIControlledSpaceShip.STAND_DOWN)) {
+ return true;
+ }
+ return false;
+ }
+
+}
90 src/it/randomtower/engine/BallActor.java
@@ -0,0 +1,90 @@
+package it.randomtower.engine;
+import org.newdawn.slick.GameContainer;
+import org.newdawn.slick.Graphics;
+import org.newdawn.slick.Image;
+import org.newdawn.slick.SlickException;
+import org.newdawn.slick.geom.Vector2f;
+
+public class BallActor extends Entity {
+
+ public static final String NAME = "ball";
+
+ public static final String STAND_DOWN = "stand_down";
+ public static final String STAND_UP = "stand_up";
+
+ public Vector2f mySpeed = new Vector2f(5, 5);
+
+ public boolean attacking = false;
+
+ public BallActor(float x, float y, String ref) {
+ super(x, y);
+
+ // set id
+ this.name = NAME;
+
+ // load spriteSheet
+ if (ref != null)
+ setupGraphic(ref);
+
+ // player rendered above everything
+ zLevel = ME.Z_LEVEL_TOP;
+
+ // define collision box and type
+ setHitBox(0, 0, currentImage.getWidth(), currentImage.getHeight());
+ addType(NAME,ME.SOLID);
+ }
+
+ public void setupGraphic(String ref) {
+ try {
+ setGraphic(new Image(ref));
+ } catch (SlickException e) {
+ e.printStackTrace();
+ }
+ }
+
+ @Override
+ public void update(GameContainer container, int delta)
+ throws SlickException {
+ super.update(container, delta);
+
+ // movements
+ updateMovements();
+
+ // check ball ends
+ if (x < 0 ){
+ int score = (Integer) ME.attributes.get("score2");
+ ME.attributes.put("score2",++score);
+ ME.remove(this);
+ } else if (x > ME.container.getWidth()){
+ int score = (Integer) ME.attributes.get("score1");
+ ME.attributes.put("score1",++score);
+ ME.remove(this);
+ }
+ }
+
+ private void updateMovements() {
+ if (!collide(ME.SOLID, x+mySpeed.x, y)){
+ x+=mySpeed.x;
+ }
+ if (!collide(ME.SOLID, x, y+mySpeed.y)){
+ y+=mySpeed.y;
+ }
+ }
+
+ @Override
+ public void render(GameContainer container, Graphics g)
+ throws SlickException {
+ super.render(container, g);
+ }
+
+ @Override
+ public void collisionResponse(Entity entity) {
+ if (entity.name.equalsIgnoreCase("player2")||entity.name.equalsIgnoreCase("player1")){
+ mySpeed.x = -1 *mySpeed.x;
+ }
+ if (entity.name.equalsIgnoreCase(StaticActor.NAME)){
+ mySpeed.y = -1 *mySpeed.y;
+ }
+ }
+
+}
41 src/it/randomtower/engine/Bullet.java
@@ -0,0 +1,41 @@
+package it.randomtower.engine;
+import org.newdawn.slick.GameContainer;
+import org.newdawn.slick.Image;
+import org.newdawn.slick.SlickException;
+import org.newdawn.slick.geom.Vector2f;
+
+public class Bullet extends Entity {
+
+ private Vector2f direction;
+ public float fireSpeed = 0.5f;
+ public static final String NAME = "BULLET";
+
+ public Bullet(float startx, float starty, String ref, Vector2f direction) {
+ super(startx, starty);
+ this.direction = direction;
+
+ try {
+ setGraphic(new Image(ref));
+ } catch (SlickException e) {
+ e.printStackTrace();
+ }
+
+ setHitBox(0, 0, currentImage.getWidth() , currentImage.getHeight());
+
+ addType(NAME,ME.SOLID);
+ }
+
+ @Override
+ public void update(GameContainer gc, int delta) throws SlickException {
+ x += fireSpeed * direction.x;
+ y += fireSpeed * direction.y;
+ }
+
+ @Override
+ public void collisionResponse(Entity entity) {
+ if (entity.name.equalsIgnoreCase(StaticActor.NAME)){
+ ME.remove(this);
+ }
+ }
+
+}
39 src/it/randomtower/engine/Camera.java
@@ -0,0 +1,39 @@
+package it.randomtower.engine;
+import org.newdawn.slick.GameContainer;
+import org.newdawn.slick.SlickException;
+import org.newdawn.slick.geom.Rectangle;
+
+public class Camera {
+
+ public float x;
+ public float y;
+ private float width;
+ private float height;
+
+ private Entity follow;
+
+ public Camera(Entity toFollow, int width, int height) {
+ this.width = width;
+ this.height = height;
+ this.follow = toFollow;
+ setCamera();
+ }
+
+ public void update(GameContainer container, int delta)
+ throws SlickException {
+ setCamera();
+ }
+
+ private void setCamera() {
+ x = -(follow.x - (width / 2));
+ y = -(follow.y - (height / 2));
+ }
+
+ public boolean contains(Entity e) {
+ Rectangle camera = new Rectangle(x - 10, y - 10, width + 10,
+ height + 10);
+ Rectangle entity = new Rectangle(e.x, e.y, e.width, e.height);
+ return camera.intersects(entity);
+ }
+
+}
266 src/it/randomtower/engine/Entity.java
@@ -0,0 +1,266 @@
+package it.randomtower.engine;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Hashtable;
+
+import org.newdawn.slick.Animation;
+import org.newdawn.slick.Color;
+import org.newdawn.slick.GameContainer;
+import org.newdawn.slick.Graphics;
+import org.newdawn.slick.Image;
+import org.newdawn.slick.Input;
+import org.newdawn.slick.SlickException;
+import org.newdawn.slick.SpriteSheet;
+import org.newdawn.slick.geom.Rectangle;
+import org.newdawn.slick.geom.Vector2f;
+
+public abstract class Entity implements Comparable<Entity> {
+
+ // identification
+ public String name;
+
+ // position
+ public float x;
+ public float y;
+
+ // movement
+ public Vector2f speed;
+
+ // animations
+ protected SpriteSheet sheet;
+ public Hashtable<String, Animation> animations = new Hashtable<String, Animation>();
+ public String currentAnim;
+ public int duration = 200;
+ public int zLevel = -1;
+
+ // static image (no animation)
+ protected Image currentImage;
+
+ // commands
+ public Hashtable<String, int[]> commands = new Hashtable<String, int[]>();
+
+ // collisions
+ private HashSet<String> type = new HashSet<String>();
+ public boolean collidable = true;
+ private float xOffset;
+ private float yOffset;
+ protected int width;
+ protected int height;
+
+ /**
+ * create a new entity setting initial position
+ *
+ * @param x
+ * @param y
+ */
+ public Entity(float x, float y) {
+ this.x = x;
+ this.y = y;
+ }
+
+ /**
+ * Update entity animation
+ *
+ * @param container
+ * @param delta
+ * @throws SlickException
+ */
+ public void update(GameContainer container, int delta)
+ throws SlickException {
+ if (animations != null) {
+ if (currentAnim != null) {
+ Animation anim = animations.get(currentAnim);
+ if (anim != null) {
+ anim.update(delta);
+ }
+ }
+ }
+ }
+
+ /**
+ * Render entity
+ *
+ * @param container
+ * @param g
+ * @throws SlickException
+ */
+ public void render(GameContainer container, Graphics g)
+ throws SlickException {
+ if (currentAnim != null) {
+ animations.get(currentAnim).draw(x, y);
+ } else if (currentImage != null) {
+ g.drawImage(currentImage, x, y);
+ }
+ if (ME.debugEnabled) {
+ g.setColor(ME.borderColor);
+ Rectangle hitBox = new Rectangle(x + xOffset, y + yOffset, width,
+ height);
+ g.draw(hitBox);
+ g.setColor(Color.white);
+ }
+ }
+
+ public void setGraphic(Image image) {
+ this.currentImage = image;
+ }
+
+ public void setGraphic(SpriteSheet sheet) {
+ this.sheet = sheet;
+ }
+
+ /**
+ * Add animation to entity, first animation added is current animation
+ *
+ * @param name
+ * @param loop
+ * @param row
+ * @param frames
+ */
+ public void add(String name, boolean loop, int row, int... frames) {
+ Animation anim = new Animation(false);
+ anim.setLooping(loop);
+ for (int i = 0; i < frames.length; i++) {
+ anim.addFrame(sheet.getSprite(frames[i], row), duration);
+ }
+ if (animations.size() == 0) {
+ currentAnim = name;
+ }
+ animations.put(name, anim);
+ }
+
+ /**
+ * define commands
+ *
+ * @param key
+ * @param keys
+ */
+ public void define(String command, int... keys) {
+ commands.put(command, keys);
+ }
+
+ /**
+ * Check if a command is down
+ *
+ * @param key
+ * @return
+ */
+ public boolean check(String command) {
+ int[] checked = commands.get(command);
+ if (checked == null)
+ return false;
+ for (int i = 0; i < checked.length; i++) {
+ if (ME.container.getInput().isKeyDown(checked[i])) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Check if a command is pressed
+ *
+ * @param key
+ * @return
+ */
+ public boolean pressed(String command) {
+ int[] checked = commands.get(command);
+ if (checked == null)
+ return false;
+ for (int i = 0; i < checked.length; i++) {
+ if (ME.container.getInput().isKeyPressed(checked[i])) {
+ return true;
+ } else if (checked[i] == Input.MOUSE_LEFT_BUTTON
+ || checked[i] == Input.MOUSE_RIGHT_BUTTON) {
+ if (ME.container.getInput().isMousePressed(checked[i])) {
+ return true;
+ }
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Compare to another entity on zLevel
+ */
+ public int compareTo(Entity o) {
+ if (zLevel == o.zLevel)
+ return 0;
+ if (zLevel > o.zLevel)
+ return 1;
+ return -1;
+ }
+
+ /**
+ * Set hitbox for collision (by default if and entity have an hitbox, is
+ * collidable)
+ *
+ * @param xOffset
+ * @param yOffset
+ * @param width
+ * @param height
+ */
+ public void setHitBox(float xOffset, float yOffset, int width, int height) {
+ setHitBox(xOffset, yOffset, width, height, true);
+ }
+
+ public void setHitBox(float xOffset, float yOffset, int width, int height,
+ boolean collidable) {
+ this.xOffset = xOffset;
+ this.yOffset = yOffset;
+ this.width = width;
+ this.height = height;
+ this.collidable = true;
+ }
+
+ /**
+ * Add collision types to entity
+ *
+ * @param types
+ * @return
+ */
+ public boolean addType(String... types) {
+ return type.addAll(Arrays.asList(types));
+ }
+
+ /**
+ * check collision with another entity
+ *
+ * @param type
+ * @param x
+ * @param y
+ * @return
+ */
+ public boolean collide(String type, float x, float y) {
+ if (type == null)
+ return false;
+ if (type.length() == 0)
+ return false;
+ // offset
+ for (Entity entity : ME.getEntities()) {
+ if (entity.collidable && entity.type.contains(type)) {
+ if (!entity.equals(this)
+ && x + xOffset + width > entity.x + entity.xOffset
+ && y + yOffset + height > entity.y + entity.yOffset
+ && x + xOffset < entity.x + entity.xOffset
+ + entity.width
+ && y + yOffset < entity.y + entity.yOffset
+ + entity.height) {
+ this.collisionResponse(entity);
+ entity.collisionResponse(this);
+ return true;
+ }
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Response to a collision with another entity
+ *
+ * @param entity
+ */
+ public void collisionResponse(Entity entity) {
+
+ }
+
+}
167 src/it/randomtower/engine/ME.java
@@ -0,0 +1,167 @@
+package it.randomtower.engine;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Hashtable;
+import java.util.List;
+
+import org.newdawn.slick.Color;
+import org.newdawn.slick.GameContainer;
+import org.newdawn.slick.Graphics;
+import org.newdawn.slick.SlickException;
+import org.newdawn.slick.geom.RoundedRectangle;
+
+/**
+ * Marte Engine Utility class
+ *
+ * @author RandomTower
+ * @project MarteEngine
+ */
+public class ME {
+
+ public static boolean debugEnabled = false;
+ public static int keyToggleDebug = -1;
+
+ public static GameContainer container;
+
+ private static final List<Entity> entities = new ArrayList<Entity>();
+ private static final List<Entity> removeable = new ArrayList<Entity>();
+ private static final List<Entity> addable = new ArrayList<Entity>();
+
+ public static float scaleX = 1;
+ public static float scaleY = 1;
+ public static Camera camera;
+
+ public static final String SOLID = "solid";
+
+ public static final Integer Z_LEVEL_TOP = 100;
+
+ public static final String WALK_LEFT = "walk_Left";
+ public static final String WALK_RIGHT = "walk_Right";
+ public static final String WALK_UP = "walk_Up";
+ public static final String WALK_DOWN = "walk_Down";
+
+ public static Hashtable<String, Object> attributes = new Hashtable<String, Object>();
+ public static Color borderColor = Color.red;
+
+
+ public static void add(Entity e) {
+ // sort in z order
+ if (entities.size() > 1) {
+ Collections.sort(entities);
+ }
+ addable.add(e);
+ }
+
+ public static void update(GameContainer container, int delta)
+ throws SlickException {
+ if (container == null)
+ throw new SlickException("no container set");
+ removeable.clear();
+
+ // special key handling
+ if (keyToggleDebug != -1) {
+ if (container.getInput().isKeyPressed(keyToggleDebug)) {
+ debugEnabled = debugEnabled ? false : true;
+ }
+ }
+
+ // update camera
+ if (camera != null) {
+ camera.update(container, delta);
+ }
+
+ // add new entities
+ for (Entity entity : addable) {
+ entities.add(entity);
+ }
+ addable.clear();
+
+ // update entities
+ for (Entity e : entities) {
+ e.update(container, delta);
+ }
+ // remove signed entities
+ for (Entity entity : removeable) {
+ entities.remove(entity);
+ }
+ }
+
+ public static void render(GameContainer container, Graphics g)
+ throws SlickException {
+ if (scaleX != 1 || scaleY != 1)
+ g.scale(scaleX, scaleY);
+ // center to camera position
+ if (camera != null)
+ g.translate(camera.x, camera.y);
+
+ // render entities
+ for (Entity e : entities) {
+ if (camera != null) {
+ // TODO
+ // if (camera.contains(e)) {
+ e.render(container, g);
+ // }
+ } else {
+ e.render(container, g);
+ }
+ }
+
+ if (camera != null)
+ g.translate(-camera.x, -camera.y);
+
+ // render debug stuff
+ if (debugEnabled) {
+ RoundedRectangle r = new RoundedRectangle(1, 1,
+ container.getWidth() - 1, 40, 20);
+ Color c = Color.lightGray;
+ c.a = 0.3f;
+ g.setColor(c);
+ g.fill(r);
+ g.draw(r);
+ g.setColor(Color.white);
+ g.drawString("Entities: " + entities.size(),
+ container.getWidth() - 110, 10);
+ container.setShowFPS(true);
+
+ } else {
+ container.setShowFPS(false);
+ }
+ }
+
+ public static List<Entity> getEntities() {
+ return entities;
+ }
+
+ public static boolean remove(Entity entity) {
+ if (!removeable.contains(entity)) {
+ return removeable.add(entity);
+ }
+ return false;
+ }
+
+ public static Entity find(String name) {
+ if (name == null)
+ return null;
+ for (Entity entity : entities) {
+ if (entity.name.equalsIgnoreCase(name)) {
+ return entity;
+ }
+ }
+ return null;
+ }
+
+ public static void clear() {
+ entities.clear();
+ }
+
+ public static void scale(float sx, float sy) {
+ scaleX = sx;
+ scaleY = sy;
+ }
+
+ public static void setCamera(Camera camera) {
+ ME.camera = camera;
+ // ME.add(ME.camera);
+ }
+
+}
25 src/it/randomtower/engine/Map.java
@@ -0,0 +1,25 @@
+package it.randomtower.engine;
+import org.newdawn.slick.SlickException;
+import org.newdawn.slick.tiled.TiledMap;
+
+public class Map extends TiledMap {
+ private static final String BLOCKS = "BLOCKS";
+ private int blocksIndex;
+
+ public Map(String ref) throws SlickException {
+ super(ref);
+ blocksIndex = getLayerIndex(BLOCKS);
+
+ for (int i = 0; i < getWidth(); i++) {
+ for (int j = 0; j < getHeight(); j++) {
+ int tileID = getTileId(i, j, blocksIndex);
+ if (Boolean.valueOf(getTileProperty(tileID, "solid", "false"))) {
+
+ StaticActor block = new StaticActor(i*32, j*32, 32,32,"data/block.png" );
+ ME.add(block);
+ }
+ }
+ }
+
+ }
+}
102 src/it/randomtower/engine/PongBarActor.java
@@ -0,0 +1,102 @@
+package it.randomtower.engine;
+import org.newdawn.slick.GameContainer;
+import org.newdawn.slick.Graphics;
+import org.newdawn.slick.Image;
+import org.newdawn.slick.SlickException;
+import org.newdawn.slick.geom.Vector2f;
+
+public class PongBarActor extends Entity {
+
+ public static final String STAND_DOWN = "stand_down";
+ public static final String STAND_UP = "stand_up";
+
+ public Vector2f mySpeed = new Vector2f(6, 6);
+
+ public boolean attacking = false;
+
+ public PongBarActor(float x, float y, String ref, String name, int up, int down) {
+ super(x, y);
+
+ // set id
+ this.name = name;
+
+ // load spriteSheet
+ if (ref != null)
+ setupAnimations(ref);
+
+ // player rendered above everything
+ zLevel = ME.Z_LEVEL_TOP;
+
+ // define labels for the key
+ defineControls(up,down);
+
+ // define collision box and type
+ setHitBox(0, 0, currentImage.getWidth(), currentImage.getHeight());
+ addType(name,ME.SOLID);
+ }
+
+ private void defineControls(int up, int down) {
+ define(ME.WALK_UP, up);
+ define(ME.WALK_DOWN, down);
+ }
+
+ public void setupAnimations(String ref) {
+ try {
+ setGraphic(new Image(ref));
+ } catch (SlickException e) {
+ e.printStackTrace();
+ }
+ }
+
+ @Override
+ public void update(GameContainer container, int delta)
+ throws SlickException {
+ super.update(container, delta);
+
+ // movements
+ updateMovements();
+ }
+
+ private void updateMovements() {
+
+ if (check(ME.WALK_UP)) {
+ moveUp();
+ } else if (check(ME.WALK_DOWN)) {
+ moveDown();
+ }
+
+ }
+
+ public void moveDown() {
+ if (!collide(ME.SOLID, x, y + mySpeed.y) && y + mySpeed.y + height < 600) {
+ y += mySpeed.y;
+ }
+ }
+
+ public void moveUp() {
+ if (!collide(ME.SOLID, x, y - mySpeed.y) && y - mySpeed.y > 0) {
+ y -= mySpeed.y;
+ }
+ }
+
+ @Override
+ public void render(GameContainer container, Graphics g)
+ throws SlickException {
+ super.render(container, g);
+ }
+
+ public boolean isUpMoving() {
+ if (currentAnim.equalsIgnoreCase(ME.WALK_UP)) {
+ return true;
+ }
+ return false;
+ }
+
+ public boolean isDownMoving() {
+ if (currentAnim.equalsIgnoreCase(ME.WALK_DOWN)) {
+ return true;
+ }
+ return false;
+ }
+
+}
155 src/it/randomtower/engine/ResizeSquare.java
@@ -0,0 +1,155 @@
+package it.randomtower.engine;
+import org.newdawn.slick.GameContainer;
+import org.newdawn.slick.Image;
+import org.newdawn.slick.Input;
+import org.newdawn.slick.SlickException;
+import org.newdawn.slick.geom.Vector2f;
+import org.newdawn.slick.util.Log;
+
+public class ResizeSquare extends Entity {
+
+ public static final String NAME = "resize";
+ private static final String ADD = "add";
+ private static final String MINUS = "minus";
+
+ private int value = 8;
+
+ public Vector2f mySpeed = new Vector2f(2, 2);
+ public float rotation;
+ public static final Vector2f SLOWEST = new Vector2f(2, 2);
+ public static final Vector2f SLOW = new Vector2f(4, 4);
+ public static final Vector2f NORMAL = new Vector2f(6, 6);
+ public static final Vector2f FAST = new Vector2f(8, 8);
+ public static final Vector2f FASTEST = new Vector2f(10, 10);
+ private static final String ATTACK1 = "ATTACK1";
+
+ public ResizeSquare(float x, float y, int width, int height, String ref) {
+ super(x, y);
+
+ // set id
+ name = NAME;
+
+ // set image
+ setupImage();
+
+ // define collision box and type
+ setHitBox(0, 0, width, height);
+ addType(NAME, ME.SOLID);
+
+ // define labels for the key
+ defineControls();
+ }
+
+ public void setupImage() {
+ try {
+ setGraphic(new Image("data/" + value + ".png"));
+ } catch (SlickException e) {
+ e.printStackTrace();
+ }
+ }
+
+ private void defineControls() {
+ define(ME.WALK_UP, Input.KEY_UP, Input.KEY_W);
+ define(ME.WALK_DOWN, Input.KEY_DOWN, Input.KEY_S);
+ define(ME.WALK_LEFT, Input.KEY_LEFT, Input.KEY_A);
+ define(ME.WALK_RIGHT, Input.KEY_RIGHT, Input.KEY_D);
+ define(ADD, Input.KEY_Z);
+ define(MINUS, Input.KEY_X);
+ define(ATTACK1, Input.MOUSE_LEFT_BUTTON);
+ }
+
+ @Override
+ public void update(GameContainer container, int delta)
+ throws SlickException {
+
+ if (check(ME.WALK_UP)) {
+ moveUp();
+ } else if (check(ME.WALK_DOWN)) {
+ moveDown();
+ }
+
+ if (check(ME.WALK_RIGHT)) {
+ moveRight();
+ } else if (check(ME.WALK_LEFT)) {
+ moveLeft();
+ }
+
+ if (pressed(ADD)) {
+ sizeUp();
+ }
+ if (pressed(MINUS)) {
+ sizeDown();
+ }
+
+ width = value;
+ height = value;
+ setHitBox(0, 0, width, height);
+ updateSpeed();
+
+ setGraphic(new Image("data/" + value + ".png"));
+
+ updateRotation();
+ }
+
+ private void updateSpeed() {
+ if (value == 8) {
+ mySpeed.set(FASTEST);
+ } else if (value == 16) {
+ mySpeed.set(FAST);
+ } else if (value == 32) {
+ mySpeed.set(NORMAL);
+ } else if (value == 64) {
+ mySpeed.set(SLOW);
+ } else if (value == 128) {
+ mySpeed.set(SLOWEST);
+ }
+ }
+
+ public void sizeDown() {
+ if (value / 2 >= 8) {
+ value /= 2;
+ }
+ }
+
+ public void sizeUp() {
+ if (value * 2 <= 128) {
+ value *= 2;
+ }
+ }
+
+ public void moveLeft() {
+ if (!collide(ME.SOLID, x - mySpeed.x, y)) {
+ x -= mySpeed.x;
+ }
+ }
+
+ public void moveRight() {
+ if (!collide(ME.SOLID, x + mySpeed.x, y)) {
+ x += mySpeed.x;
+ }
+ }
+
+ public void moveDown() {
+ if (!collide(ME.SOLID, x, y + mySpeed.y)) {
+ y += mySpeed.y;
+ }
+ }
+
+ public void moveUp() {
+ if (!collide(ME.SOLID, x, y - mySpeed.y)) {
+ y -= mySpeed.y;
+ }
+ }
+
+ private void updateRotation() {
+ Input input = ME.container.getInput();
+ int mx = input.getMouseX();
+ int my = input.getMouseY();
+ Vector2f position = new Vector2f(x, y);
+ rotation = (float) Math.toDegrees(Math.atan2(position.x - mx + value
+ / 2, position.y - my + value / 2)
+ * -1) + 180;
+ Log.info("rotation "+rotation);
+ }
+
+}
50 src/it/randomtower/engine/StaticActor.java
@@ -0,0 +1,50 @@
+package it.randomtower.engine;
+import org.newdawn.slick.Image;
+import org.newdawn.slick.SlickException;
+import org.newdawn.slick.SpriteSheet;
+
+public class StaticActor extends Entity {
+
+ public static final String NAME = "STATIC_ACTOR";
+
+ public StaticActor(float x, float y, int width, int height, String image) {
+ super(x, y);
+
+ // set id
+ name = NAME;
+
+ // define collision box and type
+ setHitBox(0, 0, width, height);
+ addType(NAME, ME.SOLID);
+
+ // set image
+ setupGraphic(image);
+ }
+
+ public StaticActor(float x, float y, int width, int height, String ref, int row, int frame ) {
+ this(x,y,width,height,null);
+
+ setupAnimations(ref,row,frame);
+ }
+
+
+ private void setupGraphic(String ref) {
+ if (ref==null) return;
+ try {
+ setGraphic(new Image(ref));
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ private void setupAnimations(String ref, int row, int frame) {
+ try {
+ setGraphic(new SpriteSheet(ref, width, height));
+ add(NAME, false, row, frame);
+ } catch (SlickException e) {
+ e.printStackTrace();
+ }
+ }
+
+
+}
213 src/it/randomtower/engine/Sword.java
@@ -0,0 +1,213 @@
+package it.randomtower.engine;
+import org.newdawn.slick.GameContainer;
+import org.newdawn.slick.Graphics;
+import org.newdawn.slick.Image;
+import org.newdawn.slick.Input;
+import org.newdawn.slick.SlickException;
+import org.newdawn.slick.geom.Vector2f;
+
+public class Sword extends Entity {
+
+ public static final String NAME = "sword";
+
+ private static final String ATTACK = "attack";
+
+ // images
+ private Image upSword;
+ private Image downSword;
+ private Image rightSword;
+ private Image leftSword;
+
+ // images positions relative to player
+ private static final Vector2f SWORD_DOWN_POS = new Vector2f(16, 15);
+ private static final Vector2f SWORD_UP_POS = new Vector2f(15, 3);
+ private static final Vector2f SWORD_LEFT_POS = new Vector2f(5, 10);
+ private static final Vector2f SWORD_RIGHT_POS = new Vector2f(16, 12);
+
+ private static final int WIDTH = 4;
+
+ private static final int HEIGHT = 14;
+
+ // parent entity (player)
+ private TopDownActor parent;
+
+ // animations
+ private int downTimer = -1;
+ private int rightTimer = -1;
+ private int upTimer = -1;
+ private int leftTimer = -1;
+
+ private int[] down = { 0, 1, 2, 2, 1, -1, -2, -2, -1 };
+ private int[] right = { 0, 1, 2, 2, 1, -1, -2, -2, -1 };
+ private int[] up = { 0, -1, -2, -2, -1, 1, 2, 2, 1 };
+ private int[] left = { 0, -1, -2, -2, -1, 1, 2, 2, 1 };
+
+ public Sword(float x, float y, String ref, TopDownActor parent) {
+ super(x + SWORD_DOWN_POS.x, y + SWORD_DOWN_POS.y);
+
+ // set id
+ name = NAME;
+
+ // set parent
+ this.parent = parent;
+
+ // setup sword image
+ setupAnimations(ref);
+ setGraphic(downSword);
+
+ // define collision box and type
+ setHitBox(1, 1, WIDTH, HEIGHT+2);
+ addType(NAME);
+
+ // define labels for the key
+ defineControls();
+ }
+
+ public void setupAnimations(String ref) {
+ try {
+ upSword = new Image(ref);
+ downSword = upSword.copy();
+ downSword.rotate(180);
+ rightSword = upSword.copy();
+ rightSword.rotate(90);
+ leftSword = upSword.copy();
+ leftSword.rotate(-90);
+ } catch (SlickException e) {
+ e.printStackTrace();
+ }
+ }
+
+ private void defineControls() {
+ define(ATTACK, Input.KEY_SPACE);
+ }
+
+ @Override
+ public void update(GameContainer container, int delta)
+ throws SlickException {
+
+ if (parent.isUpMoving()) {
+ setGraphic(upSword);
+
+ x = SWORD_UP_POS.x + parent.x;
+ y = SWORD_UP_POS.y + parent.y;
+
+ setHitBox(1, 1, WIDTH, HEIGHT+2);
+
+ zLevel = parent.zLevel - 2;
+ } else if (parent.isDownMoving()) {
+ setGraphic(downSword);
+
+ x = SWORD_DOWN_POS.x + parent.x;
+ y = SWORD_DOWN_POS.y + parent.y;
+
+ setHitBox(1, 1, WIDTH, HEIGHT+2);
+
+ zLevel = parent.zLevel + 2;
+ }
+
+ if (parent.isRightMoving()) {
+ setGraphic(rightSword);
+
+ x = SWORD_RIGHT_POS.x + parent.x;
+ y = SWORD_RIGHT_POS.y + parent.y;
+
+ setHitBox(-HEIGHT/3, WIDTH+3, HEIGHT, WIDTH);
+
+ zLevel = parent.zLevel + 2;
+ } else if (parent.isLeftMoving()) {
+ setGraphic(leftSword);
+
+ x = SWORD_LEFT_POS.x + parent.x;
+ y = SWORD_LEFT_POS.y + parent.y;
+
+ setHitBox(-HEIGHT/3, WIDTH+3, HEIGHT, WIDTH);
+
+ zLevel = parent.zLevel + 2;
+ }
+
+ // attack
+ updateAttack();
+
+ // collision
+ collide(StaticActor.NAME, x, y);
+ }
+
+ private void updateAttack() {
+ // update animation's timers
+ if (downTimer >= 0) {
+ downTimer += 1;
+ if (downTimer > down.length) {
+ downTimer = -1;
+ }
+ }
+ if (rightTimer >= 0) {
+ rightTimer += 1;
+ if (rightTimer > right.length) {
+ rightTimer = -1;
+ }
+ }
+ if (upTimer >= 0) {
+ upTimer += 1;
+ if (upTimer > up.length) {
+ upTimer = -1;
+ }
+ }
+ if (leftTimer >= 0) {
+ leftTimer += 1;
+ if (leftTimer > left.length) {
+ leftTimer = -1;
+ }
+ }
+
+ if (pressed(ATTACK)) {
+ if (parent.isDownMoving() || parent.isDownStanding()) {
+ downTimer = 0;
+ parent.attacking = true;
+ }
+ if (parent.isUpMoving() || parent.isUpStanding()) {
+ upTimer = 0;
+ parent.attacking = true;
+ }
+ if (parent.isRightMoving() || parent.isRightStanding()) {
+ rightTimer = 0;
+ parent.attacking = true;
+ }
+ if (parent.isLeftMoving() || parent.isLeftStanding()) {
+ leftTimer = 0;
+ parent.attacking = true;
+ }
+ } else {
+ parent.attacking = false;
+ }
+ }
+
+ @Override
+ public void render(GameContainer container, Graphics g)
+ throws SlickException {
+
+ if (downTimer >= 0 && downTimer < down.length) {
+ y += down[downTimer];
+ }
+ if (rightTimer >= 0 && rightTimer < right.length) {
+ x += right[rightTimer];
+ }
+ if (upTimer >= 0 && upTimer < up.length) {
+ y += up[upTimer];
+ }
+ if (leftTimer >= 0 && leftTimer < left.length) {
+ x += left[leftTimer];
+ }
+ super.render(container, g);
+ }
+
+ @Override
+ public void collisionResponse(Entity entity) {
+
+ if (entity.name.equalsIgnoreCase(StaticActor.NAME) && parent.attacking ) {
+ ME.remove(entity);
+ }
+
+
+ }
+
+}
204 src/it/randomtower/engine/TopDownActor.java
@@ -0,0 +1,204 @@
+package it.randomtower.engine;
+import org.newdawn.slick.GameContainer;
+import org.newdawn.slick.Graphics;
+import org.newdawn.slick.Input;
+import org.newdawn.slick.SlickException;
+import org.newdawn.slick.SpriteSheet;
+import org.newdawn.slick.geom.Vector2f;
+
+public class TopDownActor extends Entity {
+
+ public static final String NAME = "player";
+
+ private static final int HEIGHT = 28;
+ private static final int WIDTH = 23;
+
+ public static final String STAND_DOWN = "stand_down";
+ public static final String STAND_UP = "stand_up";
+ public static final String STAND_RIGHT = "stand_right";
+ public static final String STAND_LEFT = "stand_left";
+
+ public Vector2f mySpeed = new Vector2f(2, 2);
+
+ public boolean attacking = false;
+
+ public TopDownActor(float x, float y, String ref) {
+ super(x, y);
+
+ // set id
+ name = NAME;
+
+ // load spriteSheet
+ if (ref != null)
+ setupAnimations(ref);
+
+ // player rendered above everything
+ zLevel = ME.Z_LEVEL_TOP;
+
+ // define labels for the key
+ defineControls();
+
+ // define collision box and type
+ setHitBox(0, 0, WIDTH, HEIGHT);
+ addType(NAME);
+ }
+
+ private void defineControls() {
+ define(ME.WALK_UP, Input.KEY_UP, Input.KEY_W);
+ define(ME.WALK_DOWN, Input.KEY_DOWN, Input.KEY_S);
+ define(ME.WALK_LEFT, Input.KEY_LEFT, Input.KEY_A);
+ define(ME.WALK_RIGHT, Input.KEY_RIGHT, Input.KEY_D);
+ }
+
+ public void setupAnimations(String ref) {
+ try {
+ setGraphic(new SpriteSheet(ref, WIDTH, HEIGHT));
+ duration = 150;
+ add(STAND_DOWN, false, 0, 0);
+ add(ME.WALK_DOWN, true, 0, 0, 1, 2, 3, 4, 5, 6, 7);
+ add(ME.WALK_UP, true, 1, 0, 1, 2, 3, 4, 5, 6, 7);
+ add(ME.WALK_RIGHT, true, 2, 0, 1, 2, 3, 4, 5);
+ add(ME.WALK_LEFT, true, 3, 0, 1, 2, 3, 4, 5);
+ add(STAND_UP, false, 1, 0);
+ add(STAND_RIGHT, false, 2, 0);
+ add(STAND_LEFT, false, 3, 0);
+ } catch (SlickException e) {
+ e.printStackTrace();
+ }
+ }
+
+ @Override
+ public void update(GameContainer container, int delta)
+ throws SlickException {
+ super.update(container, delta);
+
+ // movements
+ updateMovements();
+ }
+
+ private void updateMovements() {
+ boolean horizontalMovement = true;
+ boolean verticalMovement = true;
+
+ if (check(ME.WALK_UP)) {
+ currentAnim = ME.WALK_UP;
+
+ moveUp();
+ } else if (check(ME.WALK_DOWN)) {
+ currentAnim = ME.WALK_DOWN;
+
+ moveDown();
+ } else
+ verticalMovement = false;
+
+ if (check(ME.WALK_RIGHT)) {
+ currentAnim = ME.WALK_RIGHT;
+
+ moveRight();
+ } else if (check(ME.WALK_LEFT)) {
+ currentAnim = ME.WALK_LEFT;
+
+ moveLeft();
+ } else
+ horizontalMovement = false;
+
+ if (!horizontalMovement && !verticalMovement) {
+ if (currentAnim.equalsIgnoreCase(ME.WALK_DOWN)) {
+ currentAnim = STAND_DOWN;
+ } else if (currentAnim.equalsIgnoreCase(ME.WALK_UP)) {
+ currentAnim = STAND_UP;
+ } else if (currentAnim.equalsIgnoreCase(ME.WALK_RIGHT)) {
+ currentAnim = STAND_RIGHT;
+ } else if (currentAnim.equalsIgnoreCase(ME.WALK_LEFT)) {
+ currentAnim = STAND_LEFT;
+ }
+ }
+ }
+
+ public void moveLeft() {
+ if (!collide(ME.SOLID, x - mySpeed.x, y)) {
+ x -= mySpeed.x;
+ }
+ }
+
+ public void moveRight() {
+ if (!collide(ME.SOLID, x + mySpeed.x, y)) {
+ x += mySpeed.x;
+ }
+ }
+
+ public void moveDown() {
+ if (!collide(ME.SOLID, x, y + mySpeed.y)) {
+ y += mySpeed.y;
+ }
+ }
+
+ public void moveUp() {
+ if (!collide(ME.SOLID, x, y - mySpeed.y)) {
+ y -= mySpeed.y;
+ }
+ }
+
+ @Override
+ public void render(GameContainer container, Graphics g)
+ throws SlickException {
+ super.render(container, g);
+ }
+
+ public boolean isRightMoving() {
+ if (currentAnim.equalsIgnoreCase(ME.WALK_RIGHT)) {
+ return true;
+ }
+ return false;
+ }
+
+ public boolean isLeftMoving() {
+ if (currentAnim.equalsIgnoreCase(ME.WALK_LEFT)) {
+ return true;
+ }
+ return false;
+ }
+
+ public boolean isUpMoving() {
+ if (currentAnim.equalsIgnoreCase(ME.WALK_UP)) {
+ return true;
+ }
+ return false;
+ }
+
+ public boolean isDownMoving() {
+ if (currentAnim.equalsIgnoreCase(ME.WALK_DOWN)) {
+ return true;
+ }
+ return false;
+ }
+
+ public boolean isRightStanding() {
+ if (currentAnim.equalsIgnoreCase(TopDownActor.STAND_RIGHT)) {
+ return true;
+ }
+ return false;
+ }
+
+ public boolean isLeftStanding() {
+ if (currentAnim.equalsIgnoreCase(TopDownActor.STAND_LEFT)) {
+ return true;
+ }
+ return false;
+ }
+
+ public boolean isUpStanding() {
+ if (currentAnim.equalsIgnoreCase(TopDownActor.STAND_UP)) {
+ return true;
+ }
+ return false;
+ }
+
+ public boolean isDownStanding() {
+ if (currentAnim.equalsIgnoreCase(TopDownActor.STAND_DOWN)) {
+ return true;
+ }
+ return false;
+ }
+
+}
69 test/it/randomtower/test/MoveAvatarTest.java
@@ -0,0 +1,69 @@
+package it.randomtower.test;
+import org.newdawn.slick.AppGameContainer;
+import org.newdawn.slick.BasicGame;
+import org.newdawn.slick.GameContainer;
+import org.newdawn.slick.Graphics;
+import org.newdawn.slick.Input;
+import org.newdawn.slick.SlickException;
+
+import it.randomtower.engine.*;
+
+public class MoveAvatarTest extends BasicGame {
+
+ public static int keyRestart = Input.KEY_R;
+
+ public MoveAvatarTest() {
+ super("Move Avatar Test");
+ }
+
+ @Override
+ public void init(GameContainer container) throws SlickException {
+ ME.container = container;
+ // create player
+ TopDownActor player = new TopDownActor(400, 400,"data/link.png");
+ // create sword relative to player
+ Sword sword = new Sword(player.x, player.x, "data/sword.png", player);
+ // create temple
+ StaticActor temple = new StaticActor(150, 150,48,48,"data/tiles.png",0,6);
+
+ // add entities
+ ME.add(player);
+ ME.add(temple);
+ ME.add(sword);
+
+ // set screen camera
+ ME.setCamera(new Camera(player,container.getWidth(),ME.container.getHeight()));
+ }
+
+ @Override
+ public void update(GameContainer container, int delta)
+ throws SlickException {
+ ME.update(container, delta);
+ if (keyRestart != -1){
+ if (container.getInput().isKeyPressed(keyRestart)) {
+ ME.clear();
+ init(container);
+ }
+ }
+ }
+
+ public void render(GameContainer container, Graphics g)
+ throws SlickException {
+ //ME.scale(3, 3);
+ ME.render(container, g);
+ }
+
+ public static void main(String[] argv) {
+ try {
+ ME.keyToggleDebug = Input.KEY_1;
+ AppGameContainer container = new AppGameContainer(
+ new MoveAvatarTest());
+ container.setDisplayMode(800, 600, false);
+ container.setTargetFrameRate(60);
+ container.start();
+ } catch (SlickException e) {
+ e.printStackTrace();
+ }
+ }
+
+}
181 test/it/randomtower/test/PongTest.java
@@ -0,0 +1,181 @@
+package it.randomtower.test;
+import it.randomtower.engine.BallActor;
+import it.randomtower.engine.ME;
+import it.randomtower.engine.PongBarActor;
+import it.randomtower.engine.StaticActor;
+
+import org.newdawn.slick.AppGameContainer;
+import org.newdawn.slick.BasicGame;
+import org.newdawn.slick.GameContainer;
+import org.newdawn.slick.Graphics;
+import org.newdawn.slick.Input;
+import org.newdawn.slick.SlickException;
+
+/**
+ * Pong clone
+ *
+ * @author RandomTower
+ * @project MarteEngine
+ */
+public class PongTest extends BasicGame {
+
+ /**
+ * Points for victory
+ */
+ private static final int VICTORY = 5;
+
+ /**
+ * Timer for a new ball
+ */
+ private int newBallTimer = 0;
+
+ private boolean start = true;
+
+ private boolean victory = false;
+
+ private int lastDir = 1;
+
+ public PongTest() {
+ super("Pong Slick Marte Engine clone 1.0");
+ }
+
+ @Override
+ public void init(GameContainer container) throws SlickException {
+ ME.container = container;
+ ME.clear();
+ // create player
+ ME.add(new PongBarActor(20, container.getHeight() / 2, "data/bar.PNG",
+ "player1", Input.KEY_W, Input.KEY_S));
+ ME.add(new PongBarActor(container.getWidth() - 26, container
+ .getHeight() / 2, "data/bar.png", "player2", Input.KEY_UP,
+ Input.KEY_DOWN));
+ ME.add(new StaticActor(0, 0, container.getWidth(), 1, null));
+ ME.add(new StaticActor(0, container.getHeight(), container.getWidth(),
+ 1, null));
+
+ newBallTimer = 0;
+
+ resetScore();
+ }
+
+ private void resetScore() {
+ ME.attributes.put("score1", 0);
+ ME.attributes.put("score2", 0);
+ }
+
+ private void addNewBall(GameContainer container) {
+ BallActor ball = new BallActor(container.getWidth() / 2,
+ container.getHeight() / 2, "data/ball.png");
+ if (lastDir > 0) {
+ lastDir *= -1;
+ } else {
+ lastDir *= -1;
+ }
+ ball.mySpeed.x *= lastDir;
+ ME.add(ball);
+ }
+
+ @Override
+ public void update(GameContainer container, int delta)
+ throws SlickException {
+
+ if (start) {
+ if (container.getInput().isKeyPressed(Input.KEY_SPACE)) {
+ start = false;
+ }
+ return;
+ }
+ if (victory) {
+ if (container.getInput().isKeyPressed(Input.KEY_SPACE)) {
+ victory = false;
+ start = true;
+ init(container);
+ }
+ return;
+ }
+
+ if (!start && container.getInput().isKeyPressed(Input.KEY_ESCAPE)) {
+ start = true;
+ }
+
+ // game victory stop check
+ int score1 = (Integer) ME.attributes.get("score1");
+ int score2 = (Integer) ME.attributes.get("score2");
+ if (score1 >= VICTORY || score2 >= VICTORY) {
+ victory = true;
+ }
+
+ ME.update(container, delta);
+ if (container.getInput().isKeyPressed(Input.KEY_R)) {
+ init(container);
+ }
+ // if there is no ball, add one after 2 seconds
+ if (ME.find(BallActor.NAME) == null && newBallTimer <= 0) {
+ newBallTimer = 2000;
+ }
+ if (newBallTimer > 0) {
+ newBallTimer -= delta;
+ if (newBallTimer <= 0) {
+ newBallTimer = 0;
+ addNewBall(container);
+ }
+ }
+ }
+
+ public void render(GameContainer container, Graphics g)
+ throws SlickException {
+
+ if (start) {
+ g.scale(6, 6);
+ g.drawString("PONG!", 25, 5);
+ g.resetTransform();
+
+ g.drawString(
+ "PRESS SPACE TO START or R to restart, ESC return here ",
+ 100, 250);
+ g.drawString("First player reach 5 points wins!", 100, 300);
+
+ g.drawString("LEFT PLAYER CONTROLS ARE W and S", 100, 350);
+ g.drawString("RIGHT PLAYER CONTROLS ARE UP ARROW and DOWN ARROW",
+ 100, 400);
+
+ g.drawString("2010 - http://randomtower.blogspot.com", 100,
+ container.getHeight() - 40);
+
+ return;
+ }
+ if (victory) {
+ int score1 = (Integer) ME.attributes.get("score1");
+ int score2 = (Integer) ME.attributes.get("score2");
+
+ if (score1 >= VICTORY) {
+ g.drawString("PLAYER 1 WINS!!", 150, 300);
+ } else if (score2 >= VICTORY) {
+ g.drawString("PLAYER 2 WINS!!", 150, 300);
+ }
+
+ g.drawString("PRESS SPACE TO play again!", 150, 400);
+ return;
+ }
+
+ g.drawString("SCORE : " + ME.attributes.get("score1"), 50, 20);
+ g.drawString("SCORE : " + ME.attributes.get("score2"),
+ container.getWidth() - 150, 20);
+
+ ME.render(container, g);
+ }
+
+ public static void main(String[] argv) {
+ try {
+ ME.keyToggleDebug = Input.KEY_1;
+ AppGameContainer container = new AppGameContainer(new PongTest());
+ container.setDisplayMode(640, 480, false);
+ container.setTargetFrameRate(60);
+ container.setShowFPS(false);
+ container.start();
+ } catch (SlickException e) {
+ e.printStackTrace();
+ }
+ }
+
+}
92 test/it/randomtower/test/ResizeTest.java
@@ -0,0 +1,92 @@
+package it.randomtower.test;
+import it.randomtower.engine.Bullet;
+import it.randomtower.engine.Camera;
+import it.randomtower.engine.ME;
+import it.randomtower.engine.Map;
+import it.randomtower.engine.ResizeSquare;
+
+import org.newdawn.slick.AppGameContainer;
+import org.newdawn.slick.BasicGame;
+import org.newdawn.slick.Color;
+import org.newdawn.slick.GameContainer;
+import org.newdawn.slick.Graphics;
+import org.newdawn.slick.Image;
+import org.newdawn.slick.Input;
+import org.newdawn.slick.SlickException;
+import org.newdawn.slick.geom.Vector2f;
+
+public class ResizeTest extends BasicGame {
+
+ public static int keyRestart = Input.KEY_R;
+
+ public ResizeTest() {
+ super("Resize Test");
+ }
+
+ @Override
+ public void init(GameContainer container) throws SlickException {
+ ME.container = container;
+ Image cursor = new Image("data/cross.png", false, 0, Color.white);
+
+ container.setMouseCursor(cursor, 0, 0);
+ // create square
+ ResizeSquare square = new ResizeSquare(100, 100, 8, 8, "data/8.png");
+
+ // add entities
+ ME.add(square);
+
+ new Map("data/level1.tmx");
+
+ // set screen camera
+ ME.setCamera(new Camera(square, container.getWidth(), ME.container
+ .getHeight()));
+ }
+
+ @Override
+ public void update(GameContainer container, int delta)
+ throws SlickException {
+ ME.update(container, delta);
+ if (keyRestart != -1) {
+ if (container.getInput().isKeyPressed(keyRestart)) {
+ ME.clear();
+ init(container);
+ }
+ }
+ }
+
+ @Override
+ public void mouseClicked(int button, int x, int y, int clickCount) {
+ if (button == 0) {
+ ResizeSquare rs = ((ResizeSquare)ME.find(ResizeSquare.NAME));
+ Bullet b = new Bullet(rs.x, rs.y, "data/bullet.png", calculateDirection(x, y,
+ ME.container.getInput().getMouseX(), ME.container
+ .getInput().getMouseY()));
+ ME.add(b);
+ }
+ }
+
+ public Vector2f calculateDirection(float x, float y, int mousex, int mousey) {
+ float rotation = ((ResizeSquare)ME.find(ResizeSquare.NAME)).rotation;
+ float xForce = 10f * (float) Math.sin(Math.toRadians(rotation));
+ float yForce = 10f * (float) Math.cos(Math.toRadians(rotation));
+ return new Vector2f(-xForce, yForce);
+ }
+
+ public void render(GameContainer container, Graphics g)
+ throws SlickException {
+ ME.render(container, g);
+ }
+
+ public static void main(String[] argv) {
+ try {
+ ME.keyToggleDebug = Input.KEY_1;
+ AppGameContainer container = new AppGameContainer(new ResizeTest());
+ container.setDisplayMode(800, 600, false);
+ container.setTargetFrameRate(60);
+ container.start();
+ } catch (SlickException e) {
+ e.printStackTrace();
+ }
+ }
+
+}
81 test/it/randomtower/test/SpaceDefenseTest.java
@@ -0,0 +1,81 @@
+package it.randomtower.test;
+import it.randomtower.engine.AIControlledSpaceShip;
+import it.randomtower.engine.Bullet;
+import it.randomtower.engine.ME;
+
+import org.newdawn.slick.AppGameContainer;
+import org.newdawn.slick.BasicGame;
+import org.newdawn.slick.GameContainer;
+import org.newdawn.slick.Graphics;
+import org.newdawn.slick.Input;
+import org.newdawn.slick.SlickException;
+import org.newdawn.slick.geom.Vector2f;
+
+public class SpaceDefenseTest extends BasicGame {
+
+ public static int keyRestart = Input.KEY_R;
+
+ public SpaceDefenseTest() {
+ super("Space Defense Test");
+ }
+
+ @Override
+ public void init(GameContainer container) throws SlickException {
+ ME.container = container;
+ // create player
+ AIControlledSpaceShip ai = new AIControlledSpaceShip(50, container.getHeight()/2, "data/ball.png");
+
+ //fireBullet(container);
+
+ ME.add(ai);
+
+ }
+
+ public void fireBullet(GameContainer container,float y) {
+ //TODO:
+ //x = container.getWidth()
+ //y = container.getHeight()/2
+ float x = 100;
+ Bullet bullet = new Bullet(x,y , "data/ball.png", new Vector2f(-1, 0));
+ bullet.fireSpeed = 2;
+ ME.add(bullet);
+ }
+
+ @Override
+ public void mouseClicked(int button, int x, int y, int clickCount) {
+ if (button == 0){
+ fireBullet(ME.container, y);
+ }
+ }
+
+ @Override
+ public void update(GameContainer container, int delta)
+ throws SlickException {
+ ME.update(container, delta);
+ if (keyRestart != -1){
+ if (container.getInput().isKeyPressed(keyRestart)) {
+ ME.clear();
+ init(container);
+ }
+ }
+ }
+
+ public void render(GameContainer container, Graphics g)
+ throws SlickException {
+ ME.render(container, g);
+ }
+
+ public static void main(String[] argv) {
+ try {
+ ME.keyToggleDebug = Input.KEY_1;
+ AppGameContainer container = new AppGameContainer(
+ new SpaceDefenseTest());
+ container.setDisplayMode(800, 600, false);
+ container.setTargetFrameRate(60);
+ container.start();
+ } catch (SlickException e) {
+ e.printStackTrace();
+ }
+ }
+
+}
Please sign in to comment.
Something went wrong with that request. Please try again.