Skip to content
This repository
Browse code

initial commit

  • Loading branch information...
commit 16445760bab9abf03f5d8592e5ce7e2ae8ca1a8b 0 parents
Gornova authored

Showing 55 changed files with 2,046 additions and 0 deletions. Show diff stats Hide diff stats

  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 @@
  1 +<?xml version="1.0" encoding="UTF-8"?>
  2 +<classpath>
  3 + <classpathentry kind="src" path="test"/>
  4 + <classpathentry kind="src" path="src"/>
  5 + <classpathentry kind="src" path="data"/>
  6 + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
  7 + <classpathentry kind="lib" path="lib/slick.jar" sourcepath="lib/slick-sources.jar"/>
  8 + <classpathentry kind="lib" path="lib/lwjgl.jar">
  9 + <attributes>
  10 + <attribute name="org.eclipse.jdt.launching.CLASSPATH_ATTR_LIBRARY_PATH_ENTRY" value="MarteEngine/lib/native"/>
  11 + </attributes>
  12 + </classpathentry>
  13 + <classpathentry kind="output" path="bin"/>
  14 +</classpath>
17 .project
... ... @@ -0,0 +1,17 @@
  1 +<?xml version="1.0" encoding="UTF-8"?>
  2 +<projectDescription>
  3 + <name>MarteEngine</name>
  4 + <comment></comment>
  5 + <projects>
  6 + </projects>
  7 + <buildSpec>
  8 + <buildCommand>
  9 + <name>org.eclipse.jdt.core.javabuilder</name>
  10 + <arguments>
  11 + </arguments>
  12 + </buildCommand>
  13 + </buildSpec>
  14 + <natures>
  15 + <nature>org.eclipse.jdt.core.javanature</nature>
  16 + </natures>
  17 +</projectDescription>
12 .settings/org.eclipse.jdt.core.prefs
... ... @@ -0,0 +1,12 @@
  1 +#Fri Aug 20 14:44:50 CEST 2010
  2 +eclipse.preferences.version=1
  3 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
  4 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
  5 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
  6 +org.eclipse.jdt.core.compiler.compliance=1.5
  7 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate
  8 +org.eclipse.jdt.core.compiler.debug.localVariable=generate
  9 +org.eclipse.jdt.core.compiler.debug.sourceFile=generate
  10 +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
  11 +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
  12 +org.eclipse.jdt.core.compiler.source=1.5
7 config/config.txt
... ... @@ -0,0 +1,7 @@
  1 +# EXE J CONFIG
  2 +#
  3 +# The command line arguments that will be
  4 +# passed to java.exe/javaw.exe when the user
  5 +# runs the resulting .exe files.
  6 +#
  7 +commandline=-classpath . -jar pong.jar
18 config/create_exe.bat
... ... @@ -0,0 +1,18 @@
  1 +@echo off
  2 +
  3 +echo This example generates .exe "batch" files for
  4 +echo a small java appliction. The java application
  5 +echo is called TestClass2 and it shows a popup
  6 +echo with the arguments passed to the executable.
  7 +echo ***
  8 +
  9 +echo ..\..\exej.exe -cfg config.txt
  10 +exej.exe -cfg config.txt
  11 +
  12 +echo ***
  13 +echo exe files generated in this directory.
  14 +echo Run the executables, for example:
  15 +echo using_javaw.exe arg1 arg2 arg3
  16 +
  17 +
  18 +
BIN  data/128.png
BIN  data/16.png
BIN  data/32.png
BIN  data/64.png
BIN  data/8.png
BIN  data/ball.png
BIN  data/bar.png
18 data/base.tmx
... ... @@ -0,0 +1,18 @@
  1 +<?xml version="1.0" encoding="UTF-8"?>
  2 +<!DOCTYPE map SYSTEM "http://mapeditor.org/dtd/1.0/map.dtd">
  3 +<map version="1.0" orientation="orthogonal" width="100" height="100" tilewidth="32" tileheight="32">
  4 + <tileset name="stone_01" firstgid="1" tilewidth="32" tileheight="32">
  5 + <image source="data/stone_01.png"/>
  6 + <tile id="0">
  7 + <properties>
  8 + <property name="solid" value="true"/>
  9 + </properties>
  10 + </tile>
  11 + </tileset>
  12 + <!-- Layer data is compressed (GZip) binary data, encoded in Base64 -->
  13 + <layer name="BLOCKS" width="100" height="100">
  14 + <data encoding="base64" compression="gzip">
  15 + H4sIAAAAAAAAAO3RQQ0AAAyEsJ1/07PRB01QwO5uRRVDPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPywrrgdQWYlKQJwAAA==
  16 + </data>
  17 + </layer>
  18 +</map>
BIN  data/block.png
BIN  data/bullet.png
BIN  data/cross.png
BIN  data/data/stone_01.png
BIN  data/data/stone_02.png
BIN  data/data/whiteblock_01.png
18 data/level1.tmx
... ... @@ -0,0 +1,18 @@
  1 +<?xml version="1.0" encoding="UTF-8"?>
  2 +<!DOCTYPE map SYSTEM "http://mapeditor.org/dtd/1.0/map.dtd">
  3 +<map version="1.0" orientation="orthogonal" width="100" height="100" tilewidth="32" tileheight="32">
  4 + <tileset name="stone_01" firstgid="1" tilewidth="32" tileheight="32">
  5 + <image source="data/stone_01.png"/>
  6 + <tile id="0">
  7 + <properties>
  8 + <property name="solid" value="true"/>
  9 + </properties>
  10 + </tile>
  11 + </tileset>
  12 + <!-- Layer data is compressed (GZip) binary data, encoded in Base64 -->
  13 + <layer name="BLOCKS" width="100" height="100">
  14 + <data encoding="base64" compression="gzip">
  15 + H4sIAAAAAAAAAO3RQQ0AAAyEsJ1/07PRB01QwO5uRRVDPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPyz9sPTD0g9LPywrrgdQWYlKQJwAAA==
  16 + </data>
  17 + </layer>
  18 +</map>
BIN  data/link.png
BIN  data/sword.png
BIN  data/tiles.png
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 @@
  1 +Marte Engine v0.1
  2 +
  3 +http://thedoglion.wordpress.com/2010/08/09/flashpunk-tutorial-02-loading-and-reading-ogmo-maps-creating-tilemaps/
  4 +
  5 +todo:
  6 +* 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 @@
  1 +package it.randomtower.engine;
  2 +import org.newdawn.slick.GameContainer;
  3 +import org.newdawn.slick.Graphics;
  4 +import org.newdawn.slick.Image;
  5 +import org.newdawn.slick.SlickException;
  6 +import org.newdawn.slick.geom.Vector2f;
  7 +
  8 +public class AIControlledSpaceShip extends Entity {
  9 +
  10 + public static final String NAME = "player";
  11 +
  12 + private static final int HEIGHT = 28;
  13 + private static final int WIDTH = 23;
  14 +
  15 + public static final String STAND_DOWN = "stand_down";
  16 + public static final String STAND_UP = "stand_up";
  17 + public static final String STAND_RIGHT = "stand_right";
  18 + public static final String STAND_LEFT = "stand_left";
  19 +
  20 + public Vector2f mySpeed = new Vector2f(10, 10);
  21 +
  22 + public boolean attacking = false;
  23 +
  24 + public AIControlledSpaceShip(float x, float y, String ref) {
  25 + super(x, y);
  26 +
  27 + // set id
  28 + name = NAME;
  29 +
  30 + // load spriteSheet
  31 + if (ref != null)
  32 + setupAnimations(ref);
  33 +
  34 + // player rendered above everything
  35 + zLevel = ME.Z_LEVEL_TOP;
  36 +
  37 + // define labels for the key
  38 + defineControls();
  39 +
  40 + // define collision box and type
  41 + setHitBox(0, 0, WIDTH, HEIGHT);
  42 + addType(NAME);
  43 + }
  44 +
  45 + private void defineControls() {
  46 + }
  47 +
  48 + public void setupAnimations(String ref) {
  49 + try {
  50 + setGraphic(new Image(ref));
  51 + } catch (SlickException e) {
  52 + e.printStackTrace();
  53 + }
  54 + }
  55 +
  56 + @Override
  57 + public void update(GameContainer container, int delta)
  58 + throws SlickException {
  59 + super.update(container, delta);
  60 +
  61 + // movements
  62 + updateMovements();
  63 + }
  64 +
  65 + private void updateMovements() {
  66 + if (collide(ME.SOLID,x+mySpeed.x*2,y)){
  67 + if (!collide(ME.SOLID,x,y+mySpeed.y)){
  68 + moveDown();
  69 + } else {
  70 + moveUp();
  71 + }
  72 + }
  73 + }
  74 +
  75 + public void moveLeft() {
  76 + if (!collide(ME.SOLID, x - mySpeed.x, y)) {
  77 + x -= mySpeed.x;
  78 + }
  79 + }
  80 +
  81 + public void moveRight() {
  82 + if (!collide(ME.SOLID, x + mySpeed.x, y)) {
  83 + x += mySpeed.x;
  84 + }
  85 + }
  86 +
  87 + public void moveDown() {
  88 + if (!collide(ME.SOLID, x, y + mySpeed.y) && y+mySpeed.y < ME.container.getHeight() ) {
  89 + y += mySpeed.y;
  90 + }
  91 + }
  92 +
  93 + public void moveUp() {
  94 + if (!collide(ME.SOLID, x, y - mySpeed.y) && y+mySpeed.y > 0) {
  95 + y -= mySpeed.y;
  96 + }
  97 + }
  98 +
  99 + @Override
  100 + public void render(GameContainer container, Graphics g)
  101 + throws SlickException {
  102 + super.render(container, g);
  103 + }
  104 +
  105 + public boolean isRightMoving() {
  106 + if (currentAnim.equalsIgnoreCase(ME.WALK_RIGHT)) {
  107 + return true;
  108 + }
  109 + return false;
  110 + }
  111 +
  112 + public boolean isLeftMoving() {
  113 + if (currentAnim.equalsIgnoreCase(ME.WALK_LEFT)) {
  114 + return true;
  115 + }
  116 + return false;
  117 + }
  118 +
  119 + public boolean isUpMoving() {
  120 + if (currentAnim.equalsIgnoreCase(ME.WALK_UP)) {
  121 + return true;
  122 + }
  123 + return false;
  124 + }
  125 +
  126 + public boolean isDownMoving() {
  127 + if (currentAnim.equalsIgnoreCase(ME.WALK_DOWN)) {
  128 + return true;
  129 + }
  130 + return false;
  131 + }
  132 +
  133 + public boolean isRightStanding() {
  134 + if (currentAnim.equalsIgnoreCase(AIControlledSpaceShip.STAND_RIGHT)) {
  135 + return true;
  136 + }
  137 + return false;
  138 + }
  139 +
  140 + public boolean isLeftStanding() {
  141 + if (currentAnim.equalsIgnoreCase(AIControlledSpaceShip.STAND_LEFT)) {
  142 + return true;
  143 + }
  144 + return false;
  145 + }
  146 +
  147 + public boolean isUpStanding() {
  148 + if (currentAnim.equalsIgnoreCase(AIControlledSpaceShip.STAND_UP)) {
  149 + return true;
  150 + }
  151 + return false;
  152 + }
  153 +
  154 + public boolean isDownStanding() {
  155 + if (currentAnim.equalsIgnoreCase(AIControlledSpaceShip.STAND_DOWN)) {
  156 + return true;
  157 + }
  158 + return false;
  159 + }
  160 +
  161 +}
90 src/it/randomtower/engine/BallActor.java
... ... @@ -0,0 +1,90 @@
  1 +package it.randomtower.engine;
  2 +import org.newdawn.slick.GameContainer;
  3 +import org.newdawn.slick.Graphics;
  4 +import org.newdawn.slick.Image;
  5 +import org.newdawn.slick.SlickException;
  6 +import org.newdawn.slick.geom.Vector2f;
  7 +
  8 +public class BallActor extends Entity {
  9 +
  10 + public static final String NAME = "ball";
  11 +
  12 + public static final String STAND_DOWN = "stand_down";
  13 + public static final String STAND_UP = "stand_up";
  14 +
  15 + public Vector2f mySpeed = new Vector2f(5, 5);
  16 +
  17 + public boolean attacking = false;
  18 +
  19 + public BallActor(float x, float y, String ref) {
  20 + super(x, y);
  21 +
  22 + // set id
  23 + this.name = NAME;
  24 +
  25 + // load spriteSheet
  26 + if (ref != null)
  27 + setupGraphic(ref);
  28 +
  29 + // player rendered above everything
  30 + zLevel = ME.Z_LEVEL_TOP;
  31 +
  32 + // define collision box and type
  33 + setHitBox(0, 0, currentImage.getWidth(), currentImage.getHeight());
  34 + addType(NAME,ME.SOLID);
  35 + }
  36 +
  37 + public void setupGraphic(String ref) {
  38 + try {
  39 + setGraphic(new Image(ref));
  40 + } catch (SlickException e) {
  41 + e.printStackTrace();
  42 + }
  43 + }
  44 +
  45 + @Override
  46 + public void update(GameContainer container, int delta)
  47 + throws SlickException {
  48 + super.update(container, delta);
  49 +
  50 + // movements
  51 + updateMovements();
  52 +
  53 + // check ball ends
  54 + if (x < 0 ){
  55 + int score = (Integer) ME.attributes.get("score2");
  56 + ME.attributes.put("score2",++score);
  57 + ME.remove(this);
  58 + } else if (x > ME.container.getWidth()){
  59 + int score = (Integer) ME.attributes.get("score1");
  60 + ME.attributes.put("score1",++score);
  61 + ME.remove(this);
  62 + }
  63 + }
  64 +
  65 + private void updateMovements() {
  66 + if (!collide(ME.SOLID, x+mySpeed.x, y)){
  67 + x+=mySpeed.x;
  68 + }
  69 + if (!collide(ME.SOLID, x, y+mySpeed.y)){
  70 + y+=mySpeed.y;
  71 + }
  72 + }
  73 +
  74 + @Override
  75 + public void render(GameContainer container, Graphics g)
  76 + throws SlickException {
  77 + super.render(container, g);
  78 + }
  79 +
  80 + @Override
  81 + public void collisionResponse(Entity entity) {
  82 + if (entity.name.equalsIgnoreCase("player2")||entity.name.equalsIgnoreCase("player1")){
  83 + mySpeed.x = -1 *mySpeed.x;
  84 + }
  85 + if (entity.name.equalsIgnoreCase(StaticActor.NAME)){
  86 + mySpeed.y = -1 *mySpeed.y;
  87 + }
  88 + }
  89 +
  90 +}
41 src/it/randomtower/engine/Bullet.java
... ... @@ -0,0 +1,41 @@
  1 +package it.randomtower.engine;
  2 +import org.newdawn.slick.GameContainer;
  3 +import org.newdawn.slick.Image;
  4 +import org.newdawn.slick.SlickException;
  5 +import org.newdawn.slick.geom.Vector2f;
  6 +
  7 +public class Bullet extends Entity {
  8 +
  9 + private Vector2f direction;
  10 + public float fireSpeed = 0.5f;
  11 + public static final String NAME = "BULLET";
  12 +
  13 + public Bullet(float startx, float starty, String ref, Vector2f direction) {
  14 + super(startx, starty);
  15 + this.direction = direction;
  16 +
  17 + try {
  18 + setGraphic(new Image(ref));
  19 + } catch (SlickException e) {
  20 + e.printStackTrace();
  21 + }
  22 +
  23 + setHitBox(0, 0, currentImage.getWidth() , currentImage.getHeight());
  24 +
  25 + addType(NAME,ME.SOLID);
  26 + }
  27 +
  28 + @Override
  29 + public void update(GameContainer gc, int delta) throws SlickException {
  30 + x += fireSpeed * direction.x;
  31 + y += fireSpeed * direction.y;
  32 + }
  33 +
  34 + @Override
  35 + public void collisionResponse(Entity entity) {
  36 + if (entity.name.equalsIgnoreCase(StaticActor.NAME)){
  37 + ME.remove(this);
  38 + }
  39 + }
  40 +
  41 +}
39 src/it/randomtower/engine/Camera.java
... ... @@ -0,0 +1,39 @@
  1 +package it.randomtower.engine;
  2 +import org.newdawn.slick.GameContainer;
  3 +import org.newdawn.slick.SlickException;
  4 +import org.newdawn.slick.geom.Rectangle;
  5 +
  6 +public class Camera {
  7 +
  8 + public float x;
  9 + public float y;
  10 + private float width;
  11 + private float height;
  12 +
  13 + private Entity follow;
  14 +
  15 + public Camera(Entity toFollow, int width, int height) {
  16 + this.width = width;
  17 + this.height = height;
  18 + this.follow = toFollow;
  19 + setCamera();
  20 + }
  21 +
  22 + public void update(GameContainer container, int delta)
  23 + throws SlickException {
  24 + setCamera();
  25 + }
  26 +
  27 + private void setCamera() {
  28 + x = -(follow.x - (width / 2));
  29 + y = -(follow.y - (height / 2));
  30 + }
  31 +
  32 + public boolean contains(Entity e) {
  33 + Rectangle camera = new Rectangle(x - 10, y - 10, width + 10,
  34 + height + 10);
  35 + Rectangle entity = new Rectangle(e.x, e.y, e.width, e.height);
  36 + return camera.intersects(entity);
  37 + }
  38 +
  39 +}
266 src/it/randomtower/engine/Entity.java
... ... @@ -0,0 +1,266 @@
  1 +package it.randomtower.engine;
  2 +import java.util.Arrays;
  3 +import java.util.HashSet;
  4 +import java.util.Hashtable;
  5 +
  6 +import org.newdawn.slick.Animation;
  7 +import org.newdawn.slick.Color;
  8 +import org.newdawn.slick.GameContainer;
  9 +import org.newdawn.slick.Graphics;
  10 +import org.newdawn.slick.Image;
  11 +import org.newdawn.slick.Input;
  12 +import org.newdawn.slick.SlickException;
  13 +import org.newdawn.slick.SpriteSheet;
  14 +import org.newdawn.slick.geom.Rectangle;
  15 +import org.newdawn.slick.geom.Vector2f;
  16 +
  17 +public abstract class Entity implements Comparable<Entity> {
  18 +
  19 + // identification
  20 + public String name;
  21 +
  22 + // position
  23 + public float x;
  24 + public float y;
  25 +
  26 + // movement
  27 + public Vector2f speed;
  28 +
  29 + // animations
  30 + protected SpriteSheet sheet;
  31 + public Hashtable<String, Animation> animations = new Hashtable<String, Animation>();
  32 + public String currentAnim;
  33 + public int duration = 200;
  34 + public int zLevel = -1;
  35 +
  36 + // static image (no animation)
  37 + protected Image currentImage;
  38 +
  39 + // commands
  40 + public Hashtable<String, int[]> commands = new Hashtable<String, int[]>();
  41 +
  42 + // collisions
  43 + private HashSet<String> type = new HashSet<String>();
  44 + public boolean collidable = true;
  45 + private float xOffset;
  46 + private float yOffset;
  47 + protected int width;
  48 + protected int height;
  49 +
  50 + /**
  51 + * create a new entity setting initial position
  52 + *
  53 + * @param x
  54 + * @param y
  55 + */
  56 + public Entity(float x, float y) {
  57 + this.x = x;
  58 + this.y = y;
  59 + }
  60 +
  61 + /**
  62 + * Update entity animation
  63 + *
  64 + * @param container
  65 + * @param delta
  66 + * @throws SlickException
  67 + */
  68 + public void update(GameContainer container, int delta)
  69 + throws SlickException {
  70 + if (animations != null) {
  71 + if (currentAnim != null) {
  72 + Animation anim = animations.get(currentAnim);
  73 + if (anim != null) {
  74 + anim.update(delta);
  75 + }
  76 + }
  77 + }
  78 + }
  79 +
  80 + /**
  81 + * Render entity
  82 + *
  83 + * @param container
  84 + * @param g
  85 + * @throws SlickException
  86 + */
  87 + public void render(GameContainer container, Graphics g)
  88 + throws SlickException {
  89 + if (currentAnim != null) {
  90 + animations.get(currentAnim).draw(x, y);
  91 + } else if (currentImage != null) {
  92 + g.drawImage(currentImage, x, y);
  93 + }
  94 + if (ME.debugEnabled) {
  95 + g.setColor(ME.borderColor);
  96 + Rectangle hitBox = new Rectangle(x + xOffset, y + yOffset, width,
  97 + height);
  98 + g.draw(hitBox);
  99 + g.setColor(Color.white);
  100 + }
  101 + }
  102 +
  103 + public void setGraphic(Image image) {
  104 + this.currentImage = image;
  105 + }
  106 +
  107 + public void setGraphic(SpriteSheet sheet) {
  108 + this.sheet = sheet;
  109 + }
  110 +
  111 + /**
  112 + * Add animation to entity, first animation added is current animation
  113 + *
  114 + * @param name
  115 + * @param loop
  116 + * @param row
  117 + * @param frames
  118 + */
  119 + public void add(String name, boolean loop, int row, int... frames) {
  120 + Animation anim = new Animation(false);
  121 + anim.setLooping(loop);
  122 + for (int i = 0; i < frames.length; i++) {
  123 + anim.addFrame(sheet.getSprite(frames[i], row), duration);
  124 + }
  125 + if (animations.size() == 0) {
  126 + currentAnim = name;
  127 + }
  128 + animations.put(name, anim);
  129 + }
  130 +
  131 + /**
  132 + * define commands
  133 + *
  134 + * @param key
  135 + * @param keys
  136 + */
  137 + public void define(String command, int... keys) {
  138 + commands.put(command, keys);
  139 + }
  140 +
  141 + /**
  142 + * Check if a command is down
  143 + *
  144 + * @param key
  145 + * @return
  146 + */
  147 + public boolean check(String command) {
  148 + int[] checked = commands.get(command);
  149 + if (checked == null)
  150 + return false;
  151 + for (int i = 0; i < checked.length; i++) {
  152 + if (ME.container.getInput().isKeyDown(checked[i])) {
  153 + return true;
  154 + }
  155 + }
  156 + return false;
  157 + }
  158 +
  159 + /**
  160 + * Check if a command is pressed
  161 + *
  162 + * @param key
  163 + * @return
  164 + */
  165 + public boolean pressed(String command) {
  166 + int[] checked = commands.get(command);
  167 + if (checked == null)
  168 + return false;
  169 + for (int i = 0; i < checked.length; i++) {
  170 + if (ME.container.getInput().isKeyPressed(checked[i])) {
  171 + return true;
  172 + } else if (checked[i] == Input.MOUSE_LEFT_BUTTON
  173 + || checked[i] == Input.MOUSE_RIGHT_BUTTON) {
  174 + if (ME.container.getInput().isMousePressed(checked[i])) {
  175 + return true;
  176 + }
  177 + }
  178 + }
  179 + return false;
  180 + }
  181 +
  182 + /**
  183 + * Compare to another entity on zLevel
  184 + */
  185 + public int compareTo(Entity o) {
  186 + if (zLevel == o.zLevel)
  187 + return 0;
  188 + if (zLevel > o.zLevel)
  189 + return 1;
  190 + return -1;
  191 + }
  192 +
  193 + /**
  194 + * Set hitbox for collision (by default if and entity have an hitbox, is
  195 + * collidable)
  196 + *
  197 + * @param xOffset
  198 + * @param yOffset
  199 + * @param width
  200 + * @param height
  201 + */
  202 + public void setHitBox(float xOffset, float yOffset, int width, int height) {
  203 + setHitBox(xOffset, yOffset, width, height, true);
  204 + }
  205 +
  206 + public void setHitBox(float xOffset, float yOffset, int width, int height,
  207 + boolean collidable) {
  208 + this.xOffset = xOffset;
  209 + this.yOffset = yOffset;
  210 + this.width = width;
  211 + this.height = height;
  212 + this.collidable = true;
  213 + }
  214 +
  215 + /**
  216 + * Add collision types to entity
  217 + *
  218 + * @param types
  219 + * @return
  220 + */
  221 + public boolean addType(String... types) {
  222 + return type.addAll(Arrays.asList(types));
  223 + }
  224 +
  225 + /**
  226 + * check collision with another entity
  227 + *
  228 + * @param type
  229 + * @param x
  230 + * @param y
  231 + * @return
  232 + */
  233 + public boolean collide(String type, float x, float y) {
  234 + if (type == null)
  235 + return false;
  236 + if (type.length() == 0)
  237 + return false;
  238 + // offset
  239 + for (Entity entity : ME.getEntities()) {
  240 + if (entity.collidable && entity.type.contains(type)) {
  241 + if (!entity.equals(this)
  242 + && x + xOffset + width > entity.x + entity.xOffset
  243 + && y + yOffset + height > entity.y + entity.yOffset
  244 + && x + xOffset < entity.x + entity.xOffset
  245 + + entity.width
  246 + && y + yOffset < entity.y + entity.yOffset
  247 + + entity.height) {
  248 + this.collisionResponse(entity);
  249 + entity.collisionResponse(this);
  250 + return true;
  251 + }
  252 + }
  253 + }
  254 + return false;
  255 + }
  256 +
  257 + /**
  258 + * Response to a collision with another entity
  259 + *
  260 + * @param entity
  261 + */
  262 + public void collisionResponse(Entity entity) {
  263 +
  264 + }
  265 +
  266 +}
167 src/it/randomtower/engine/ME.java
... ... @@ -0,0 +1,167 @@
  1 +package it.randomtower.engine;
  2 +import java.util.ArrayList;
  3 +import java.util.Collections;
  4 +import java.util.Hashtable;
  5 +import java.util.List;
  6 +
  7 +import org.newdawn.slick.Color;
  8 +import org.newdawn.slick.GameContainer;
  9 +import org.newdawn.slick.Graphics;
  10 +import org.newdawn.slick.SlickException;
  11 +import org.newdawn.slick.geom.RoundedRectangle;
  12 +
  13 +/**
  14 + * Marte Engine Utility class
  15 + *
  16 + * @author RandomTower
  17 + * @project MarteEngine
  18 + */
  19 +public class ME {
  20 +
  21 + public static boolean debugEnabled = false;
  22 + public static int keyToggleDebug = -1;
  23 +
  24 + public static GameContainer container;
  25 +
  26 + private static final List<Entity> entities = new ArrayList<Entity>();
  27 + private static final List<Entity> removeable = new ArrayList<Entity>();
  28 + private static final List<Entity> addable = new ArrayList<Entity>();
  29 +
  30 + public static float scaleX = 1;
  31 + public static float scaleY = 1;
  32 + public static Camera camera;
  33 +
  34 + public static final String SOLID = "solid";
  35 +
  36 + public static final Integer Z_LEVEL_TOP = 100;
  37 +
  38 + public static final String WALK_LEFT = "walk_Left";
  39 + public static final String WALK_RIGHT = "walk_Right";
  40 + public static final String WALK_UP = "walk_Up";
  41 + public static final String WALK_DOWN = "walk_Down";
  42 +
  43 + public static Hashtable<String, Object> attributes = new Hashtable<String, Object>();
  44 + public static Color borderColor = Color.red;
  45 +
  46 +
  47 + public static void add(Entity e) {
  48 + // sort in z order
  49 + if (entities.size() > 1) {
  50 + Collections.sort(entities);
  51 + }
  52 + addable.add(e);
  53 + }
  54 +
  55 + public static void update(GameContainer container, int delta)
  56 + throws SlickException {
  57 + if (container == null)
  58 + throw new SlickException("no container set");
  59 + removeable.clear();
  60 +
  61 + // special key handling
  62 + if (keyToggleDebug != -1) {
  63 + if (container.getInput().isKeyPressed(keyToggleDebug)) {
  64 + debugEnabled = debugEnabled ? false : true;
  65 + }
  66 + }
  67 +
  68 + // update camera
  69 + if (camera != null) {
  70 + camera.update(container, delta);
  71 + }
  72 +
  73 + // add new entities
  74 + for (Entity entity : addable) {
  75 + entities.add(entity);
  76 + }
  77 + addable.clear();
  78 +
  79 + // update entities
  80 + for (Entity e : entities) {
  81 + e.update(container, delta);
  82 + }
  83 + // remove signed entities
  84 + for (Entity entity : removeable) {
  85 + entities.remove(entity);
  86 + }
  87 + }
  88 +
  89 + public static void render(GameContainer container, Graphics g)
  90 + throws SlickException {
  91 + if (scaleX != 1 || scaleY != 1)
  92 + g.scale(scaleX, scaleY);
  93 + // center to camera position
  94 + if (camera != null)
  95 + g.translate(camera.x, camera.y);
  96 +
  97 + // render entities
  98 + for (Entity e : entities) {
  99 + if (camera != null) {
  100 + // TODO
  101 + // if (camera.contains(e)) {
  102 + e.render(container, g);
  103 + // }
  104 + } else {
  105 + e.render(container, g);
  106 + }
  107 + }
  108 +
  109 + if (camera != null)
  110 + g.translate(-camera.x, -camera.y);
  111 +
  112 + // render debug stuff
  113 + if (debugEnabled) {
  114 + RoundedRectangle r = new RoundedRectangle(1, 1,
  115 + container.getWidth() - 1, 40, 20);
  116 + Color c = Color.lightGray;
  117 + c.a = 0.3f;
  118 + g.setColor(c);
  119 + g.fill(r);
  120 + g.draw(r);
  121 + g.setColor(Color.white);
  122 + g.drawString("Entities: " + entities.size(),
  123 + container.getWidth() - 110, 10);
  124 + container.setShowFPS(true);
  125 +
  126 + } else {
  127 + container.setShowFPS(false);
  128 + }
  129 + }
  130 +
  131 + public static List<Entity> getEntities() {
  132 + return entities;
  133 + }
  134 +
  135 + public static boolean remove(Entity entity) {
  136 + if (!removeable.contains(entity)) {
  137 + return removeable.add(entity);
  138 + }
  139 + return false;
  140 + }
  141 +
  142 + public static Entity find(String name) {
  143 + if (name == null)
  144 + return null;
  145 + for (Entity entity : entities) {
  146 + if (entity.name.equalsIgnoreCase(name)) {
  147 + return entity;
  148 + }
  149 + }
  150 + return null;
  151 + }
  152 +
  153 + public static void clear() {
  154 + entities.clear();
  155 + }
  156 +
  157 + public static void scale(float sx, float sy) {
  158 + scaleX = sx;
  159 + scaleY = sy;
  160 + }
  161 +
  162 + public static void setCamera(Camera camera) {
  163 + ME.camera = camera;
  164 + // ME.add(ME.camera);
  165 + }
  166 +
  167 +}
25 src/it/randomtower/engine/Map.java
... ... @@ -0,0 +1,25 @@
  1 +package it.randomtower.engine;
  2 +import org.newdawn.slick.SlickException;
  3 +import org.newdawn.slick.tiled.TiledMap;
  4 +
  5 +public class Map extends TiledMap {
  6 + private static final String BLOCKS = "BLOCKS";
  7 + private int blocksIndex;
  8 +
  9 + public Map(String ref) throws SlickException {
  10 + super(ref);
  11 + blocksIndex = getLayerIndex(BLOCKS);
  12 +
  13 + for (int i = 0; i < getWidth(); i++) {
  14 + for (int j = 0; j < getHeight(); j++) {
  15 + int tileID = getTileId(i, j, blocksIndex);
  16 + if (Boolean.valueOf(getTileProperty(tileID, "solid", "false"))) {
  17 +
  18 + StaticActor block = new StaticActor(i*32, j*32, 32,32,"data/block.png" );
  19 + ME.add(block);
  20 + }
  21 + }
  22 + }
  23 +
  24 + }
  25 +}
102 src/it/randomtower/engine/PongBarActor.java
... ... @@ -0,0 +1,102 @@
  1 +package it.randomtower.engine;
  2 +import org.newdawn.slick.GameContainer;
  3 +import org.newdawn.slick.Graphics;
  4 +import org.newdawn.slick.Image;
  5 +import org.newdawn.slick.SlickException;
  6 +import org.newdawn.slick.geom.Vector2f;
  7 +
  8 +public class PongBarActor extends Entity {
  9 +
  10 + public static final String STAND_DOWN = "stand_down";
  11 + public static final String STAND_UP = "stand_up";
  12 +
  13 + public Vector2f mySpeed = new Vector2f(6, 6);
  14 +
  15 + public boolean attacking = false;
  16 +
  17 + public PongBarActor(float x, float y, String ref, String name, int up, int down) {
  18 + super(x, y);
  19 +
  20 + // set id
  21 + this.name = name;
  22 +
  23 + // load spriteSheet
  24 + if (ref != null)
  25 + setupAnimations(ref);
  26 +
  27 + // player rendered above everything
  28 + zLevel = ME.Z_LEVEL_TOP;
  29 +
  30 + // define labels for the key
  31 + defineControls(up,down);
  32 +
  33 + // define collision box and type
  34 + setHitBox(0, 0, currentImage.getWidth(), currentImage.getHeight());
  35 + addType(name,ME.SOLID);
  36 + }
  37 +
  38 + private void defineControls(int up, int down) {
  39 + define(ME.WALK_UP, up);
  40 + define(ME.WALK_DOWN, down);
  41 + }
  42 +
  43 + public void setupAnimations(String ref) {
  44 + try {
  45 + setGraphic(new Image(ref));
  46 + } catch (SlickException e) {
  47 + e.printStackTrace();
  48 + }
  49 + }
  50 +
  51 + @Override
  52 + public void update(GameContainer container, int delta)
  53 + throws SlickException {
  54 + super.update(container, delta);
  55 +
  56 + // movements
  57 + updateMovements();
  58 + }
  59 +
  60 + private void updateMovements() {
  61 +
  62 + if (check(ME.WALK_UP)) {
  63 + moveUp();
  64 + } else if (check(ME.WALK_DOWN)) {
  65 + moveDown();
  66 + }
  67 +
  68 + }
  69 +
  70 + public void moveDown() {
  71 + if (!collide(ME.SOLID, x, y + mySpeed.y) && y + mySpeed.y + height < 600) {
  72 + y += mySpeed.y;
  73 + }
  74 + }
  75 +
  76 + public void moveUp() {
  77 + if (!collide(ME.SOLID, x, y - mySpeed.y) && y - mySpeed.y > 0) {
  78 + y -= mySpeed.y;
  79 + }
  80 + }
  81 +
  82 + @Override
  83 + public void render(GameContainer container, Graphics g)
  84 + throws SlickException {
  85 + super.render(container, g);
  86 + }
  87 +
  88 + public boolean isUpMoving() {
  89 + if (currentAnim.equalsIgnoreCase(ME.WALK_UP)) {
  90 + return true;
  91 + }
  92 + return false;
  93 + }
  94 +
  95 + public boolean isDownMoving() {
  96 + if (currentAnim.equalsIgnoreCase(ME.WALK_DOWN)) {
  97 + return true;
  98 + }
  99 + return false;
  100 + }
  101 +
  102 +}
155 src/it/randomtower/engine/ResizeSquare.java
... ... @@ -0,0 +1,155 @@
  1 +package it.randomtower.engine;
  2 +import org.newdawn.slick.GameContainer;
  3 +import org.newdawn.slick.Image;
  4 +import org.newdawn.slick.Input;
  5 +import org.newdawn.slick.SlickException;
  6 +import org.newdawn.slick.geom.Vector2f;
  7 +import org.newdawn.slick.util.Log;
  8 +
  9 +public class ResizeSquare extends Entity {
  10 +
  11 + public static final String NAME = "resize";
  12 + private static final String ADD = "add";
  13 + private static final String MINUS = "minus";
  14 +
  15 + private int value = 8;
  16 +
  17 + public Vector2f mySpeed = new Vector2f(2, 2);
  18 + public float rotation;
  19 + public static final Vector2f SLOWEST = new Vector2f(2, 2);
  20 + public static final Vector2f SLOW = new Vector2f(4, 4);
  21 + public static final Vector2f NORMAL = new Vector2f(6, 6);
  22 + public static final Vector2f FAST = new Vector2f(8, 8);
  23 + public static final Vector2f FASTEST = new Vector2f(10, 10);
  24 + private static final String ATTACK1 = "ATTACK1";
  25 +
  26 + public ResizeSquare(float x, float y, int width, int height, String ref) {
  27 + super(x, y);
  28 +
  29 + // set id
  30 + name = NAME;
  31 +
  32 + // set image
  33 + setupImage();
  34 +
  35 + // define collision box and type
  36 + setHitBox(0, 0, width, height);
  37 + addType(NAME, ME.SOLID);
  38 +
  39 + // define labels for the key
  40 + defineControls();
  41 + }
  42 +
  43 + public void setupImage() {
  44 + try {
  45 + setGraphic(new Image("data/" + value + ".png"));
  46 + } catch (SlickException e) {
  47 + e.printStackTrace();
  48 + }
  49 + }
  50 +
  51 + private void defineControls() {
  52 + define(ME.WALK_UP, Input.KEY_UP, Input.KEY_W);
  53 + define(ME.WALK_DOWN, Input.KEY_DOWN, Input.KEY_S);
  54 + define(ME.WALK_LEFT, Input.KEY_LEFT, Input.KEY_A);
  55 + define(ME.WALK_RIGHT, Input.KEY_RIGHT, Input.KEY_D);
  56 + define(ADD, Input.KEY_Z);
  57 + define(MINUS, Input.KEY_X);
  58 + define(ATTACK1, Input.MOUSE_LEFT_BUTTON);