├── .gitignore ├── distribution.xml ├── pom.xml ├── readme └── src └── main ├── java ├── lesson01 │ └── WindowGame.java ├── lesson02 │ └── MapGame.java ├── lesson03 │ └── CharacterGame.java ├── lesson04 │ ├── CameraGame.java │ └── mousse │ │ └── CameraGame.java ├── lesson05 │ └── CollisionGame.java ├── lesson06 │ └── TriggerGame.java ├── lesson07 │ └── MusicGame.java ├── lesson08 │ └── readme ├── lesson09 │ └── ChangeMapGame.java ├── lesson10 │ ├── Map.java │ ├── ObjectsGame.java │ └── Player.java ├── lesson11 │ ├── Camera.java │ ├── Map.java │ ├── ObjectsGame.java │ ├── Player.java │ ├── PlayerController.java │ └── TriggerController.java ├── lesson12 │ ├── Camera.java │ ├── ControllerGame.java │ ├── Map.java │ ├── Player.java │ ├── PlayerController.java │ └── TriggerController.java ├── lesson13 │ ├── Camera.java │ ├── Hud.java │ ├── HudGame.java │ ├── Map.java │ ├── Player.java │ ├── PlayerController.java │ └── TriggerController.java ├── lesson14 │ ├── AnalogGame.java │ ├── Camera.java │ ├── Hud.java │ ├── Map.java │ ├── Player.java │ ├── PlayerController.java │ └── TriggerController.java ├── lesson15 │ ├── Camera.java │ ├── Hud.java │ ├── MainScreenGameState.java │ ├── Map.java │ ├── MapGameState.java │ ├── Player.java │ ├── PlayerController.java │ ├── StateGame.java │ └── TriggerController.java ├── lesson16 │ ├── BattleGame.java │ ├── BattleGameState.java │ ├── Camera.java │ ├── Hud.java │ ├── MainScreenGameState.java │ ├── Map.java │ ├── MapGameState.java │ ├── Player.java │ ├── PlayerController.java │ ├── TriggerController.java │ └── randombackground │ │ ├── BattleGameState.java │ │ └── RandomBackgroundBattleGame.java ├── lesson17 │ ├── BattleCommand.java │ ├── BattleController.java │ ├── BattleEnnemy.java │ ├── BattleGame.java │ ├── BattleGameState.java │ ├── BattlePlayer.java │ ├── MainScreenGameState.java │ ├── Map.java │ ├── MapCamera.java │ ├── MapGameState.java │ ├── MapHud.java │ ├── MapPlayer.java │ ├── MapPlayerController.java │ └── MapTriggerController.java ├── lesson18 │ ├── BattleCommand.java │ ├── BattleController.java │ ├── BattleEnnemy.java │ ├── BattleGame.java │ ├── BattleGameState.java │ ├── BattlePlayer.java │ ├── MainScreenGameState.java │ ├── Map.java │ ├── MapCamera.java │ ├── MapGameState.java │ ├── MapHud.java │ ├── MapPlayer.java │ ├── MapPlayerController.java │ └── MapTriggerController.java ├── lesson19 │ ├── BattleCommand.java │ ├── BattleController.java │ ├── BattleEnnemy.java │ ├── BattleGame.java │ ├── BattleGameState.java │ ├── BattlePlayer.java │ ├── MainScreenGameState.java │ ├── Map.java │ ├── MapCamera.java │ ├── MapGameState.java │ ├── MapHud.java │ ├── MapPlayer.java │ ├── MapPlayerController.java │ └── MapTriggerController.java ├── lesson20 │ ├── BattleCommand.java │ ├── BattleController.java │ ├── BattleEnnemy.java │ ├── BattleGame.java │ ├── BattleGameState.java │ ├── BattleHud.java │ ├── BattlePlayer.java │ ├── MainScreenGameState.java │ ├── Map.java │ ├── MapCamera.java │ ├── MapGameState.java │ ├── MapHud.java │ ├── MapPlayer.java │ ├── MapPlayerController.java │ └── MapTriggerController.java ├── lesson21 │ ├── BattleCommand.java │ ├── BattleController.java │ ├── BattleEnnemy.java │ ├── BattleGame.java │ ├── BattleGameState.java │ ├── BattleHud.java │ ├── BattlePlayer.java │ ├── MainScreenGameState.java │ ├── Map.java │ ├── MapCamera.java │ ├── MapGameState.java │ ├── MapHud.java │ ├── MapPlayer.java │ ├── MapPlayerController.java │ └── MapTriggerController.java └── question │ └── TextFieldGame.java └── resources ├── background ├── battle-alternatif.png ├── battle.png └── forest.png ├── battle ├── gobelin.png └── hero.png ├── contribution ├── hud ├── button.png └── player-bar.png ├── map ├── desert.tmx ├── exemple-avec-grotte.tmx ├── exemple-change-map.tmx ├── exemple-collision.tmx ├── exemple.tmx └── tuiles │ ├── barrel.png │ ├── brackish.png │ ├── bridges.png │ ├── buckets.png │ ├── cabinets.png │ ├── castle_lightsources.png │ ├── castle_outside.png │ ├── castlefloors.png │ ├── castlefloors_outside.png │ ├── castlewalls.png │ ├── cement.png │ ├── cementstair.png │ ├── chests.png │ ├── country.png │ ├── cup.png │ ├── desert.png │ ├── dirt.png │ ├── dirt2.png │ ├── dungeon.png │ ├── grass.png │ ├── grassalt.png │ ├── hole.png │ ├── holek.png │ ├── holemid.png │ ├── house.png │ ├── inside.png │ ├── kitchen.png │ ├── lava.png │ ├── lavarock.png │ ├── logic.png │ ├── mountains.png │ ├── rock.png │ ├── signs.png │ ├── stairs.png │ ├── treetop.png │ ├── trunk.png │ ├── victoria.png │ ├── water.png │ ├── waterfall.png │ └── watergrass.png ├── sound ├── lively-meadow-victory-fanfare.ogg ├── lost-in-the-meadows.ogg └── the-last-encounter-short-loop.ogg ├── sprites └── character.png ├── start.bat └── start.sh /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | *.log 3 | 4 | # maven 5 | target/* 6 | 7 | # eclipse 8 | .classpath 9 | .settings 10 | .project 11 | 12 | /target/ 13 | -------------------------------------------------------------------------------- /distribution.xml: -------------------------------------------------------------------------------- 1 | 4 | distribution 5 | 6 | tar.gz 7 | 8 | / 9 | 10 | 11 | target 12 | / 13 | 14 | *.jar 15 | 16 | 17 | 18 | target/natives 19 | natives 20 | 21 | * 22 | 23 | 24 | 25 | src/main/resources 26 | / 27 | 28 | **/* 29 | 30 | 31 | 32 | 33 | 34 | /lib 35 | 36 | org.lwjgl.lwjgl:lwjgl-platform 37 | net.java.jinput:jinput-platform 38 | shionn.tuto:SlickBlogTutorial 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /readme: -------------------------------------------------------------------------------- 1 | Code source d'une serie de tutoriaux paru sur www.shionn.org 2 | - leçon 1 : creer une application slick 3 | - leçon 2 : afficher une carte 4 | - leçon 3 : annimer et deplacer un personnage 5 | - leçon 4 : camera 6 | - leçon 5 : collision 7 | - leçon 6 : trigger 8 | - leçon 7 : musique 9 | - leçon 8 : packaging 10 | - leçon 9 : changement de carte 11 | - leçon 10 & 11 : Convertion à l'objet 12 | - leçon 12 : utiliser un joystick 13 | - leçon 13 : afficher un hud 14 | - leçon 14 : deplacement analogique 15 | - leçon 15 : afficher un ecran titre 16 | - leçon 16,17,18,19,20,21 : combat au tour par tour 17 | -------------------------------------------------------------------------------- /src/main/java/lesson01/WindowGame.java: -------------------------------------------------------------------------------- 1 | package lesson01; 2 | 3 | import org.newdawn.slick.AppGameContainer; 4 | import org.newdawn.slick.BasicGame; 5 | import org.newdawn.slick.GameContainer; 6 | import org.newdawn.slick.Graphics; 7 | import org.newdawn.slick.Input; 8 | import org.newdawn.slick.SlickException; 9 | 10 | /** 11 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 12 | * 13 | * -Djava.library.path=target/natives 14 | * 15 | * @author Shionn, shionn@gmail.com http://shionn.org
16 | * GCS d- s+:+ a C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y+ 17 | */ 18 | public class WindowGame extends BasicGame { 19 | 20 | private GameContainer container; 21 | 22 | public static void main(String[] args) throws SlickException { 23 | new AppGameContainer(new WindowGame(), 800, 600, false).start(); 24 | } 25 | 26 | public WindowGame() { 27 | super("Lesson 1 :: WindowGame"); 28 | } 29 | 30 | @Override 31 | public void init(GameContainer container) throws SlickException { 32 | this.container = container; 33 | } 34 | 35 | @Override 36 | public void render(GameContainer container, Graphics g) throws SlickException { 37 | } 38 | 39 | @Override 40 | public void update(GameContainer container, int delta) throws SlickException { 41 | } 42 | 43 | @Override 44 | public void keyReleased(int key, char c) { 45 | if (Input.KEY_ESCAPE == key) { 46 | container.exit(); 47 | } 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/lesson02/MapGame.java: -------------------------------------------------------------------------------- 1 | package lesson02; 2 | 3 | import org.newdawn.slick.AppGameContainer; 4 | import org.newdawn.slick.BasicGame; 5 | import org.newdawn.slick.GameContainer; 6 | import org.newdawn.slick.Graphics; 7 | import org.newdawn.slick.Input; 8 | import org.newdawn.slick.SlickException; 9 | import org.newdawn.slick.tiled.TiledMap; 10 | 11 | /** 12 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 13 | * 14 | * -Djava.library.path=target/natives 15 | * 16 | * @author Shionn, shionn@gmail.com http://shionn.org
17 | * GCS d- s+:+ a C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y+ 18 | */ 19 | public class MapGame extends BasicGame { 20 | 21 | private GameContainer container; 22 | private TiledMap map; 23 | 24 | public static void main(String[] args) throws SlickException { 25 | new AppGameContainer(new MapGame(), 800, 600, false).start(); 26 | } 27 | 28 | public MapGame() { 29 | super("Lesson 2 :: MapGame"); 30 | } 31 | 32 | @Override 33 | public void init(GameContainer container) throws SlickException { 34 | this.container = container; 35 | this.map = new TiledMap("map/exemple.tmx"); 36 | } 37 | 38 | @Override 39 | public void render(GameContainer container, Graphics g) throws SlickException { 40 | this.map.render(0, 0); 41 | } 42 | 43 | @Override 44 | public void update(GameContainer container, int delta) throws SlickException { 45 | } 46 | 47 | @Override 48 | public void keyReleased(int key, char c) { 49 | if (Input.KEY_ESCAPE == key) { 50 | this.container.exit(); 51 | } 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/lesson08/readme: -------------------------------------------------------------------------------- 1 | cette lecon n'as pas de code java. 2 | il s'agit de packaging d'application et de script de lancement. 3 | 4 | Plus d'information sur www.shionn.org -------------------------------------------------------------------------------- /src/main/java/lesson10/Map.java: -------------------------------------------------------------------------------- 1 | package lesson10; 2 | 3 | import org.newdawn.slick.Color; 4 | import org.newdawn.slick.Image; 5 | import org.newdawn.slick.SlickException; 6 | import org.newdawn.slick.tiled.TiledMap; 7 | 8 | /** 9 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 10 | * 11 | * @author Shionn, shionn@gmail.com http://shionn.org
12 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y- 13 | */ 14 | public class Map { 15 | private TiledMap tiledMap; 16 | 17 | public void init() throws SlickException { 18 | this.tiledMap = new TiledMap("map/exemple-change-map.tmx"); 19 | } 20 | 21 | public void renderBackground() { 22 | this.tiledMap.render(0, 0, 0); 23 | this.tiledMap.render(0, 0, 1); 24 | this.tiledMap.render(0, 0, 2); 25 | } 26 | 27 | public void renderForeground() { 28 | this.tiledMap.render(0, 0, 3); 29 | this.tiledMap.render(0, 0, 4); 30 | } 31 | 32 | public boolean isCollision(float x, float y) { 33 | int tileW = this.tiledMap.getTileWidth(); 34 | int tileH = this.tiledMap.getTileHeight(); 35 | int logicLayer = this.tiledMap.getLayerIndex("logic"); 36 | Image tile = this.tiledMap.getTileImage((int) x / tileW, (int) y / tileH, logicLayer); 37 | boolean collision = tile != null; 38 | if (collision) { 39 | Color color = tile.getColor((int) x % tileW, (int) y % tileH); 40 | collision = color.getAlpha() > 0; 41 | } 42 | return collision; 43 | } 44 | 45 | public void changeMap(String file) throws SlickException { 46 | this.tiledMap = new TiledMap(file); 47 | } 48 | 49 | public int getObjectCount() { 50 | return this.tiledMap.getObjectCount(0); 51 | } 52 | 53 | public String getObjectType(int objectID) { 54 | return this.tiledMap.getObjectType(0, objectID); 55 | } 56 | 57 | public float getObjectX(int objectID) { 58 | return this.tiledMap.getObjectX(0, objectID); 59 | } 60 | 61 | public float getObjectY(int objectID) { 62 | return this.tiledMap.getObjectY(0, objectID); 63 | } 64 | 65 | public float getObjectWidth(int objectID) { 66 | return this.tiledMap.getObjectWidth(0, objectID); 67 | } 68 | 69 | public float getObjectHeight(int objectID) { 70 | return this.tiledMap.getObjectHeight(0, objectID); 71 | } 72 | 73 | public String getObjectProperty(int objectID, String propertyName, String def) { 74 | return this.tiledMap.getObjectProperty(0, objectID, propertyName, def); 75 | } 76 | 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/lesson11/Camera.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package lesson11; 5 | 6 | import org.newdawn.slick.GameContainer; 7 | import org.newdawn.slick.Graphics; 8 | 9 | /** 10 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 11 | * 12 | * @author Shionn, shionn@gmail.com http://shionn.org
13 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y- 14 | */ 15 | public class Camera { 16 | 17 | private Player player; 18 | private float xCamera, yCamera; 19 | 20 | public Camera(Player player) { 21 | this.player = player; 22 | this.xCamera = player.getX(); 23 | this.yCamera = player.getY(); 24 | } 25 | 26 | public void place(GameContainer container, Graphics g) { 27 | g.translate(container.getWidth() / 2 - (int) this.xCamera, container.getHeight() / 2 28 | - (int) this.yCamera); 29 | } 30 | 31 | public void update(GameContainer container) { 32 | int w = container.getWidth() / 4; 33 | if (this.player.getX() > this.xCamera + w) { 34 | this.xCamera = this.player.getX() - w; 35 | } else if (this.player.getX() < this.xCamera - w) { 36 | this.xCamera = this.player.getX() + w; 37 | } 38 | int h = container.getHeight() / 4; 39 | if (this.player.getY() > this.yCamera + h) { 40 | this.yCamera = this.player.getY() - h; 41 | } else if (this.player.getY() < this.yCamera - h) { 42 | this.yCamera = this.player.getY() + h; 43 | } 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/lesson11/Map.java: -------------------------------------------------------------------------------- 1 | package lesson11; 2 | 3 | import org.newdawn.slick.Color; 4 | import org.newdawn.slick.Image; 5 | import org.newdawn.slick.SlickException; 6 | import org.newdawn.slick.tiled.TiledMap; 7 | 8 | /** 9 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 10 | * 11 | * @author Shionn, shionn@gmail.com http://shionn.org
12 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y- 13 | */ 14 | public class Map { 15 | private TiledMap tiledMap; 16 | 17 | public void init() throws SlickException { 18 | this.tiledMap = new TiledMap("map/exemple-change-map.tmx"); 19 | } 20 | 21 | public void renderBackground() { 22 | this.tiledMap.render(0, 0, 0); 23 | this.tiledMap.render(0, 0, 1); 24 | this.tiledMap.render(0, 0, 2); 25 | } 26 | 27 | public void renderForeground() { 28 | this.tiledMap.render(0, 0, 3); 29 | this.tiledMap.render(0, 0, 4); 30 | } 31 | 32 | public boolean isCollision(float x, float y) { 33 | int tileW = this.tiledMap.getTileWidth(); 34 | int tileH = this.tiledMap.getTileHeight(); 35 | int logicLayer = this.tiledMap.getLayerIndex("logic"); 36 | Image tile = this.tiledMap.getTileImage((int) x / tileW, (int) y / tileH, logicLayer); 37 | boolean collision = tile != null; 38 | if (collision) { 39 | Color color = tile.getColor((int) x % tileW, (int) y % tileH); 40 | collision = color.getAlpha() > 0; 41 | } 42 | return collision; 43 | } 44 | 45 | public void changeMap(String file) throws SlickException { 46 | this.tiledMap = new TiledMap(file); 47 | } 48 | 49 | public int getObjectCount() { 50 | return this.tiledMap.getObjectCount(0); 51 | } 52 | 53 | public String getObjectType(int objectID) { 54 | return this.tiledMap.getObjectType(0, objectID); 55 | } 56 | 57 | public float getObjectX(int objectID) { 58 | return this.tiledMap.getObjectX(0, objectID); 59 | } 60 | 61 | public float getObjectY(int objectID) { 62 | return this.tiledMap.getObjectY(0, objectID); 63 | } 64 | 65 | public float getObjectWidth(int objectID) { 66 | return this.tiledMap.getObjectWidth(0, objectID); 67 | } 68 | 69 | public float getObjectHeight(int objectID) { 70 | return this.tiledMap.getObjectHeight(0, objectID); 71 | } 72 | 73 | public String getObjectProperty(int objectID, String propertyName, String def) { 74 | return this.tiledMap.getObjectProperty(0, objectID, propertyName, def); 75 | } 76 | 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/lesson11/ObjectsGame.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 3 | * 4 | * @author Shionn, shionn@gmail.com http://shionn.org
5 | * GCS d- s+:+ a- C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- !y- 6 | */ 7 | package lesson11; 8 | 9 | import org.newdawn.slick.AppGameContainer; 10 | import org.newdawn.slick.BasicGame; 11 | import org.newdawn.slick.GameContainer; 12 | import org.newdawn.slick.Graphics; 13 | import org.newdawn.slick.Input; 14 | import org.newdawn.slick.Music; 15 | import org.newdawn.slick.SlickException; 16 | 17 | /** 18 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 19 | * 20 | * -Djava.library.path=target/natives 21 | * 22 | * @author Shionn, shionn@gmail.com http://shionn.org
23 | * GCS d- s+:+ a C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y+ 24 | */ 25 | public class ObjectsGame extends BasicGame { 26 | 27 | private GameContainer container; 28 | private Map map = new Map(); 29 | private Player player = new Player(map); 30 | private TriggerController triggers = new TriggerController(map, player); 31 | private Camera camera = new Camera(player); 32 | 33 | public static void main(String[] args) throws SlickException { 34 | new AppGameContainer(new ObjectsGame(), 800, 600, false).start(); 35 | } 36 | 37 | public ObjectsGame() { 38 | super("Lesson 11 :: ObjectsGame"); 39 | } 40 | 41 | @Override 42 | public void init(GameContainer container) throws SlickException { 43 | this.container = container; 44 | Music background = new Music("sound/lost-in-the-meadows.ogg"); 45 | background.loop(); 46 | this.map.init(); 47 | this.player.init(); 48 | PlayerController controler = new PlayerController(this.player); 49 | container.getInput().addKeyListener(controler); 50 | } 51 | 52 | @Override 53 | public void render(GameContainer container, Graphics g) throws SlickException { 54 | this.camera.place(container, g); 55 | this.map.renderBackground(); 56 | this.player.render(g); 57 | this.map.renderForeground(); 58 | } 59 | 60 | @Override 61 | public void update(GameContainer container, int delta) throws SlickException { 62 | this.triggers.update(); 63 | this.player.update(delta); 64 | this.camera.update(container); 65 | } 66 | 67 | @Override 68 | public void keyReleased(int key, char c) { 69 | if (Input.KEY_ESCAPE == key) { 70 | this.container.exit(); 71 | } 72 | } 73 | 74 | @Override 75 | public void keyPressed(int key, char c) { 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/lesson11/PlayerController.java: -------------------------------------------------------------------------------- 1 | package lesson11; 2 | 3 | import org.newdawn.slick.Input; 4 | import org.newdawn.slick.KeyListener; 5 | 6 | /** 7 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 8 | * 9 | * @author Shionn, shionn@gmail.com http://shionn.org
10 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y- 11 | */ 12 | public class PlayerController implements KeyListener { 13 | 14 | private Player player; 15 | 16 | public PlayerController(Player player) { 17 | this.player = player; 18 | } 19 | 20 | @Override 21 | public void setInput(Input input) { 22 | 23 | } 24 | 25 | @Override 26 | public boolean isAcceptingInput() { 27 | return true; 28 | } 29 | 30 | @Override 31 | public void inputEnded() { 32 | 33 | } 34 | 35 | @Override 36 | public void inputStarted() { 37 | 38 | } 39 | 40 | @Override 41 | public void keyPressed(int key, char c) { 42 | switch (key) { 43 | case Input.KEY_UP: 44 | this.player.setDirection(0); 45 | this.player.setMoving(true); 46 | break; 47 | case Input.KEY_LEFT: 48 | this.player.setDirection(1); 49 | this.player.setMoving(true); 50 | break; 51 | case Input.KEY_DOWN: 52 | this.player.setDirection(2); 53 | this.player.setMoving(true); 54 | break; 55 | case Input.KEY_RIGHT: 56 | this.player.setDirection(3); 57 | this.player.setMoving(true); 58 | break; 59 | } 60 | } 61 | 62 | @Override 63 | public void keyReleased(int key, char c) { 64 | this.player.setMoving(false); 65 | } 66 | 67 | } 68 | -------------------------------------------------------------------------------- /src/main/java/lesson11/TriggerController.java: -------------------------------------------------------------------------------- 1 | package lesson11; 2 | 3 | import org.newdawn.slick.SlickException; 4 | 5 | /** 6 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 7 | * 8 | * @author Shionn, shionn@gmail.com http://shionn.org
9 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y- 10 | */ 11 | public class TriggerController { 12 | 13 | private Map map; 14 | private Player player; 15 | 16 | public TriggerController(Map map, Player player) { 17 | this.map = map; 18 | this.player = player; 19 | } 20 | 21 | public void update() throws SlickException { 22 | this.player.setOnStair(false); 23 | for (int objectID = 0; objectID < this.map.getObjectCount(); objectID++) { 24 | if (isInTrigger(objectID)) { 25 | if ("teleport".equals(this.map.getObjectType(objectID))) { 26 | this.teleport(objectID); 27 | } else if ("stair".equals(this.map.getObjectType(objectID))) { 28 | this.player.setOnStair(true); 29 | } else if ("change-map".equals(this.map.getObjectType(objectID))) { 30 | this.changeMap(objectID); 31 | } 32 | } 33 | } 34 | } 35 | 36 | private boolean isInTrigger(int id) { 37 | return this.player.getX() > this.map.getObjectX(id) 38 | && this.player.getX() < this.map.getObjectX(id) + this.map.getObjectWidth(id) 39 | && this.player.getY() > this.map.getObjectY(id) 40 | && this.player.getY() < this.map.getObjectY(id) + this.map.getObjectHeight(id); 41 | } 42 | 43 | private void teleport(int objectID) { 44 | this.player.setX(Float.parseFloat(this.map.getObjectProperty(objectID, "dest-x", 45 | Float.toString(this.player.getX())))); 46 | this.player.setY(Float.parseFloat(this.map.getObjectProperty(objectID, "dest-y", 47 | Float.toString(this.player.getY())))); 48 | } 49 | 50 | private void changeMap(int objectID) throws SlickException { 51 | this.teleport(objectID); 52 | String newMap = this.map.getObjectProperty(objectID, "dest-map", "undefined"); 53 | if (!"undefined".equals(newMap)) { 54 | this.map.changeMap("map/" + newMap); 55 | } 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/lesson12/Camera.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package lesson12; 5 | 6 | import org.newdawn.slick.GameContainer; 7 | import org.newdawn.slick.Graphics; 8 | 9 | /** 10 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 11 | * 12 | * @author Shionn, shionn@gmail.com http://shionn.org
13 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y- 14 | */ 15 | public class Camera { 16 | 17 | private Player player; 18 | private float xCamera, yCamera; 19 | 20 | public Camera(Player player) { 21 | this.player = player; 22 | this.xCamera = player.getX(); 23 | this.yCamera = player.getY(); 24 | } 25 | 26 | public void place(GameContainer container, Graphics g) { 27 | g.translate(container.getWidth() / 2 - (int) this.xCamera, container.getHeight() / 2 28 | - (int) this.yCamera); 29 | } 30 | 31 | public void update(GameContainer container) { 32 | int w = container.getWidth() / 4; 33 | if (this.player.getX() > this.xCamera + w) { 34 | this.xCamera = this.player.getX() - w; 35 | } else if (this.player.getX() < this.xCamera - w) { 36 | this.xCamera = this.player.getX() + w; 37 | } 38 | int h = container.getHeight() / 4; 39 | if (this.player.getY() > this.yCamera + h) { 40 | this.yCamera = this.player.getY() - h; 41 | } else if (this.player.getY() < this.yCamera - h) { 42 | this.yCamera = this.player.getY() + h; 43 | } 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/lesson12/ControllerGame.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 3 | * 4 | * @author Shionn, shionn@gmail.com http://shionn.org
5 | * GCS d- s+:+ a- C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- !y- 6 | */ 7 | package lesson12; 8 | 9 | import org.newdawn.slick.AppGameContainer; 10 | import org.newdawn.slick.BasicGame; 11 | import org.newdawn.slick.GameContainer; 12 | import org.newdawn.slick.Graphics; 13 | import org.newdawn.slick.Input; 14 | import org.newdawn.slick.Music; 15 | import org.newdawn.slick.SlickException; 16 | 17 | /** 18 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 19 | * 20 | * -Djava.library.path=target/natives 21 | * 22 | * @author Shionn, shionn@gmail.com http://shionn.org
23 | * GCS d- s+:+ a C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y+ 24 | */ 25 | public class ControllerGame extends BasicGame { 26 | 27 | private GameContainer container; 28 | private Map map = new Map(); 29 | private Player player = new Player(map); 30 | private TriggerController triggers = new TriggerController(map, player); 31 | private Camera camera = new Camera(player); 32 | private PlayerController controller = new PlayerController(this.player); 33 | 34 | public static void main(String[] args) throws SlickException { 35 | new AppGameContainer(new ControllerGame(), 800, 600, false).start(); 36 | } 37 | 38 | public ControllerGame() { 39 | super("Lesson 12 :: ControllerGame"); 40 | } 41 | 42 | @Override 43 | public void init(GameContainer container) throws SlickException { 44 | this.container = container; 45 | Music background = new Music("sound/lost-in-the-meadows.ogg"); 46 | background.loop(); 47 | this.map.init(); 48 | this.player.init(); 49 | container.getInput().addKeyListener(controller); 50 | container.getInput().addControllerListener(controller); 51 | } 52 | 53 | @Override 54 | public void render(GameContainer container, Graphics g) throws SlickException { 55 | this.camera.place(container, g); 56 | this.map.renderBackground(); 57 | this.player.render(g); 58 | this.map.renderForeground(); 59 | } 60 | 61 | @Override 62 | public void update(GameContainer container, int delta) throws SlickException { 63 | this.triggers.update(); 64 | this.player.update(delta); 65 | this.camera.update(container); 66 | } 67 | 68 | @Override 69 | public void keyReleased(int key, char c) { 70 | if (Input.KEY_ESCAPE == key) { 71 | this.container.exit(); 72 | } 73 | } 74 | 75 | @Override 76 | public void keyPressed(int key, char c) { 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /src/main/java/lesson12/Map.java: -------------------------------------------------------------------------------- 1 | package lesson12; 2 | 3 | import org.newdawn.slick.Color; 4 | import org.newdawn.slick.Image; 5 | import org.newdawn.slick.SlickException; 6 | import org.newdawn.slick.tiled.TiledMap; 7 | 8 | /** 9 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 10 | * 11 | * @author Shionn, shionn@gmail.com http://shionn.org
12 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y- 13 | */ 14 | public class Map { 15 | private TiledMap tiledMap; 16 | 17 | public void init() throws SlickException { 18 | this.tiledMap = new TiledMap("map/exemple-change-map.tmx"); 19 | } 20 | 21 | public void renderBackground() { 22 | this.tiledMap.render(0, 0, 0); 23 | this.tiledMap.render(0, 0, 1); 24 | this.tiledMap.render(0, 0, 2); 25 | } 26 | 27 | public void renderForeground() { 28 | this.tiledMap.render(0, 0, 3); 29 | this.tiledMap.render(0, 0, 4); 30 | } 31 | 32 | public boolean isCollision(float x, float y) { 33 | int tileW = this.tiledMap.getTileWidth(); 34 | int tileH = this.tiledMap.getTileHeight(); 35 | int logicLayer = this.tiledMap.getLayerIndex("logic"); 36 | Image tile = this.tiledMap.getTileImage((int) x / tileW, (int) y / tileH, logicLayer); 37 | boolean collision = tile != null; 38 | if (collision) { 39 | Color color = tile.getColor((int) x % tileW, (int) y % tileH); 40 | collision = color.getAlpha() > 0; 41 | } 42 | return collision; 43 | } 44 | 45 | public void changeMap(String file) throws SlickException { 46 | this.tiledMap = new TiledMap(file); 47 | } 48 | 49 | public int getObjectCount() { 50 | return this.tiledMap.getObjectCount(0); 51 | } 52 | 53 | public String getObjectType(int objectID) { 54 | return this.tiledMap.getObjectType(0, objectID); 55 | } 56 | 57 | public float getObjectX(int objectID) { 58 | return this.tiledMap.getObjectX(0, objectID); 59 | } 60 | 61 | public float getObjectY(int objectID) { 62 | return this.tiledMap.getObjectY(0, objectID); 63 | } 64 | 65 | public float getObjectWidth(int objectID) { 66 | return this.tiledMap.getObjectWidth(0, objectID); 67 | } 68 | 69 | public float getObjectHeight(int objectID) { 70 | return this.tiledMap.getObjectHeight(0, objectID); 71 | } 72 | 73 | public String getObjectProperty(int objectID, String propertyName, String def) { 74 | return this.tiledMap.getObjectProperty(0, objectID, propertyName, def); 75 | } 76 | 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/lesson12/TriggerController.java: -------------------------------------------------------------------------------- 1 | package lesson12; 2 | 3 | import org.newdawn.slick.SlickException; 4 | 5 | /** 6 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 7 | * 8 | * @author Shionn, shionn@gmail.com http://shionn.org
9 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y- 10 | */ 11 | public class TriggerController { 12 | 13 | private Map map; 14 | private Player player; 15 | 16 | public TriggerController(Map map, Player player) { 17 | this.map = map; 18 | this.player = player; 19 | } 20 | 21 | public void update() throws SlickException { 22 | this.player.setOnStair(false); 23 | for (int objectID = 0; objectID < this.map.getObjectCount(); objectID++) { 24 | if (isInTrigger(objectID)) { 25 | if ("teleport".equals(this.map.getObjectType(objectID))) { 26 | this.teleport(objectID); 27 | } else if ("stair".equals(this.map.getObjectType(objectID))) { 28 | this.player.setOnStair(true); 29 | } else if ("change-map".equals(this.map.getObjectType(objectID))) { 30 | this.changeMap(objectID); 31 | } 32 | } 33 | } 34 | } 35 | 36 | private boolean isInTrigger(int id) { 37 | return this.player.getX() > this.map.getObjectX(id) 38 | && this.player.getX() < this.map.getObjectX(id) + this.map.getObjectWidth(id) 39 | && this.player.getY() > this.map.getObjectY(id) 40 | && this.player.getY() < this.map.getObjectY(id) + this.map.getObjectHeight(id); 41 | } 42 | 43 | private void teleport(int objectID) { 44 | this.player.setX(Float.parseFloat(this.map.getObjectProperty(objectID, "dest-x", 45 | Float.toString(this.player.getX())))); 46 | this.player.setY(Float.parseFloat(this.map.getObjectProperty(objectID, "dest-y", 47 | Float.toString(this.player.getY())))); 48 | } 49 | 50 | private void changeMap(int objectID) throws SlickException { 51 | this.teleport(objectID); 52 | String newMap = this.map.getObjectProperty(objectID, "dest-map", "undefined"); 53 | if (!"undefined".equals(newMap)) { 54 | this.map.changeMap("map/" + newMap); 55 | } 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/lesson13/Camera.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package lesson13; 5 | 6 | import org.newdawn.slick.GameContainer; 7 | import org.newdawn.slick.Graphics; 8 | 9 | /** 10 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 11 | * 12 | * @author Shionn, shionn@gmail.com http://shionn.org
13 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y- 14 | */ 15 | public class Camera { 16 | 17 | private Player player; 18 | private float xCamera, yCamera; 19 | 20 | public Camera(Player player) { 21 | this.player = player; 22 | this.xCamera = player.getX(); 23 | this.yCamera = player.getY(); 24 | } 25 | 26 | public void place(GameContainer container, Graphics g) { 27 | g.translate(container.getWidth() / 2 - (int) this.xCamera, container.getHeight() / 2 28 | - (int) this.yCamera); 29 | } 30 | 31 | public void update(GameContainer container) { 32 | int w = container.getWidth() / 4; 33 | if (this.player.getX() > this.xCamera + w) { 34 | this.xCamera = this.player.getX() - w; 35 | } else if (this.player.getX() < this.xCamera - w) { 36 | this.xCamera = this.player.getX() + w; 37 | } 38 | int h = container.getHeight() / 4; 39 | if (this.player.getY() > this.yCamera + h) { 40 | this.yCamera = this.player.getY() - h; 41 | } else if (this.player.getY() < this.yCamera - h) { 42 | this.yCamera = this.player.getY() + h; 43 | } 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/lesson13/Hud.java: -------------------------------------------------------------------------------- 1 | package lesson13; 2 | 3 | import org.newdawn.slick.Color; 4 | import org.newdawn.slick.Graphics; 5 | import org.newdawn.slick.Image; 6 | import org.newdawn.slick.SlickException; 7 | 8 | /** 9 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 10 | * 11 | * @author Shionn, shionn@gmail.com http://shionn.org
12 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y- 13 | */ 14 | public class Hud { 15 | 16 | private static final int P_BAR_X = 10; 17 | private static final int P_BAR_Y = 10; 18 | private static final int BAR_X = 84 + P_BAR_X; 19 | private static final int LIFE_BAR_Y = 4 + P_BAR_Y; 20 | private static final int MANA_BAR_Y = 24 + P_BAR_Y; 21 | private static final int XP_BAR_Y = 44 + P_BAR_Y; 22 | private static final int BAR_WIDTH = 80; 23 | private static final int BAR_HEIGHT = 16; 24 | 25 | private static final Color LIFE_COLOR = new Color(255, 0, 0); 26 | private static final Color MANA_COLOR = new Color(0, 0, 255); 27 | private static final Color XP_COLOR = new Color(0, 255, 0); 28 | 29 | private Image playerbars; 30 | 31 | public void init() throws SlickException { 32 | this.playerbars = new Image("hud/player-bar.png"); 33 | } 34 | 35 | public void render(Graphics g) { 36 | g.resetTransform(); 37 | g.setColor(LIFE_COLOR); 38 | g.fillRect(BAR_X, LIFE_BAR_Y, .9f * BAR_WIDTH, BAR_HEIGHT); 39 | g.setColor(MANA_COLOR); 40 | g.fillRect(BAR_X, MANA_BAR_Y, .8f * BAR_WIDTH, BAR_HEIGHT); 41 | g.setColor(XP_COLOR); 42 | g.fillRect(BAR_X, XP_BAR_Y, .2f * BAR_WIDTH, BAR_HEIGHT); 43 | g.drawImage(playerbars, P_BAR_X, P_BAR_Y); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/lesson13/HudGame.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 3 | * 4 | * @author Shionn, shionn@gmail.com http://shionn.org
5 | * GCS d- s+:+ a- C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- !y- 6 | */ 7 | package lesson13; 8 | 9 | import org.newdawn.slick.AppGameContainer; 10 | import org.newdawn.slick.BasicGame; 11 | import org.newdawn.slick.GameContainer; 12 | import org.newdawn.slick.Graphics; 13 | import org.newdawn.slick.Input; 14 | import org.newdawn.slick.Music; 15 | import org.newdawn.slick.SlickException; 16 | 17 | /** 18 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 19 | * 20 | * -Djava.library.path=target/natives 21 | * 22 | * @author Shionn, shionn@gmail.com http://shionn.org
23 | * GCS d- s+:+ a C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y+ 24 | */ 25 | public class HudGame extends BasicGame { 26 | 27 | private GameContainer container; 28 | private Map map = new Map(); 29 | private Player player = new Player(map); 30 | private TriggerController triggers = new TriggerController(map, player); 31 | private Camera camera = new Camera(player); 32 | private Hud hud = new Hud(); 33 | private PlayerController controller = new PlayerController(this.player); 34 | 35 | public static void main(String[] args) throws SlickException { 36 | new AppGameContainer(new HudGame(), 800, 600, false).start(); 37 | } 38 | 39 | public HudGame() { 40 | super("Lesson 13 :: HudGame"); 41 | } 42 | 43 | @Override 44 | public void init(GameContainer container) throws SlickException { 45 | this.container = container; 46 | Music background = new Music("sound/lost-in-the-meadows.ogg"); 47 | background.loop(); 48 | this.map.init(); 49 | this.player.init(); 50 | this.hud.init(); 51 | container.getInput().addKeyListener(controller); 52 | container.getInput().addControllerListener(controller); 53 | } 54 | 55 | @Override 56 | public void render(GameContainer container, Graphics g) throws SlickException { 57 | this.camera.place(container, g); 58 | this.map.renderBackground(); 59 | this.player.render(g); 60 | this.map.renderForeground(); 61 | this.hud.render(g); 62 | } 63 | 64 | @Override 65 | public void update(GameContainer container, int delta) throws SlickException { 66 | this.triggers.update(); 67 | this.player.update(delta); 68 | this.camera.update(container); 69 | } 70 | 71 | @Override 72 | public void keyReleased(int key, char c) { 73 | if (Input.KEY_ESCAPE == key) { 74 | this.container.exit(); 75 | } 76 | } 77 | 78 | @Override 79 | public void keyPressed(int key, char c) { 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /src/main/java/lesson13/Map.java: -------------------------------------------------------------------------------- 1 | package lesson13; 2 | 3 | import org.newdawn.slick.Color; 4 | import org.newdawn.slick.Image; 5 | import org.newdawn.slick.SlickException; 6 | import org.newdawn.slick.tiled.TiledMap; 7 | 8 | /** 9 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 10 | * 11 | * @author Shionn, shionn@gmail.com http://shionn.org
12 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y- 13 | */ 14 | public class Map { 15 | private TiledMap tiledMap; 16 | 17 | public void init() throws SlickException { 18 | this.tiledMap = new TiledMap("map/exemple-change-map.tmx"); 19 | } 20 | 21 | public void renderBackground() { 22 | this.tiledMap.render(0, 0, 0); 23 | this.tiledMap.render(0, 0, 1); 24 | this.tiledMap.render(0, 0, 2); 25 | } 26 | 27 | public void renderForeground() { 28 | this.tiledMap.render(0, 0, 3); 29 | this.tiledMap.render(0, 0, 4); 30 | } 31 | 32 | public boolean isCollision(float x, float y) { 33 | int tileW = this.tiledMap.getTileWidth(); 34 | int tileH = this.tiledMap.getTileHeight(); 35 | int logicLayer = this.tiledMap.getLayerIndex("logic"); 36 | Image tile = this.tiledMap.getTileImage((int) x / tileW, (int) y / tileH, logicLayer); 37 | boolean collision = tile != null; 38 | if (collision) { 39 | Color color = tile.getColor((int) x % tileW, (int) y % tileH); 40 | collision = color.getAlpha() > 0; 41 | } 42 | return collision; 43 | } 44 | 45 | public void changeMap(String file) throws SlickException { 46 | this.tiledMap = new TiledMap(file); 47 | } 48 | 49 | public int getObjectCount() { 50 | return this.tiledMap.getObjectCount(0); 51 | } 52 | 53 | public String getObjectType(int objectID) { 54 | return this.tiledMap.getObjectType(0, objectID); 55 | } 56 | 57 | public float getObjectX(int objectID) { 58 | return this.tiledMap.getObjectX(0, objectID); 59 | } 60 | 61 | public float getObjectY(int objectID) { 62 | return this.tiledMap.getObjectY(0, objectID); 63 | } 64 | 65 | public float getObjectWidth(int objectID) { 66 | return this.tiledMap.getObjectWidth(0, objectID); 67 | } 68 | 69 | public float getObjectHeight(int objectID) { 70 | return this.tiledMap.getObjectHeight(0, objectID); 71 | } 72 | 73 | public String getObjectProperty(int objectID, String propertyName, String def) { 74 | return this.tiledMap.getObjectProperty(0, objectID, propertyName, def); 75 | } 76 | 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/lesson13/TriggerController.java: -------------------------------------------------------------------------------- 1 | package lesson13; 2 | 3 | import org.newdawn.slick.SlickException; 4 | 5 | /** 6 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 7 | * 8 | * @author Shionn, shionn@gmail.com http://shionn.org
9 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y- 10 | */ 11 | public class TriggerController { 12 | 13 | private Map map; 14 | private Player player; 15 | 16 | public TriggerController(Map map, Player player) { 17 | this.map = map; 18 | this.player = player; 19 | } 20 | 21 | public void update() throws SlickException { 22 | this.player.setOnStair(false); 23 | for (int objectID = 0; objectID < this.map.getObjectCount(); objectID++) { 24 | if (isInTrigger(objectID)) { 25 | if ("teleport".equals(this.map.getObjectType(objectID))) { 26 | this.teleport(objectID); 27 | } else if ("stair".equals(this.map.getObjectType(objectID))) { 28 | this.player.setOnStair(true); 29 | } else if ("change-map".equals(this.map.getObjectType(objectID))) { 30 | this.changeMap(objectID); 31 | } 32 | } 33 | } 34 | } 35 | 36 | private boolean isInTrigger(int id) { 37 | return this.player.getX() > this.map.getObjectX(id) 38 | && this.player.getX() < this.map.getObjectX(id) + this.map.getObjectWidth(id) 39 | && this.player.getY() > this.map.getObjectY(id) 40 | && this.player.getY() < this.map.getObjectY(id) + this.map.getObjectHeight(id); 41 | } 42 | 43 | private void teleport(int objectID) { 44 | this.player.setX(Float.parseFloat(this.map.getObjectProperty(objectID, "dest-x", 45 | Float.toString(this.player.getX())))); 46 | this.player.setY(Float.parseFloat(this.map.getObjectProperty(objectID, "dest-y", 47 | Float.toString(this.player.getY())))); 48 | } 49 | 50 | private void changeMap(int objectID) throws SlickException { 51 | this.teleport(objectID); 52 | String newMap = this.map.getObjectProperty(objectID, "dest-map", "undefined"); 53 | if (!"undefined".equals(newMap)) { 54 | this.map.changeMap("map/" + newMap); 55 | } 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/lesson14/AnalogGame.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 3 | * 4 | * @author Shionn, shionn@gmail.com http://shionn.org
5 | * GCS d- s+:+ a- C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- !y- 6 | */ 7 | package lesson14; 8 | 9 | import org.newdawn.slick.AppGameContainer; 10 | import org.newdawn.slick.BasicGame; 11 | import org.newdawn.slick.GameContainer; 12 | import org.newdawn.slick.Graphics; 13 | import org.newdawn.slick.Input; 14 | import org.newdawn.slick.Music; 15 | import org.newdawn.slick.SlickException; 16 | 17 | /** 18 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 19 | * 20 | * -Djava.library.path=target/natives 21 | * 22 | * @author Shionn, shionn@gmail.com http://shionn.org
23 | * GCS d- s+:+ a C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y+ 24 | */ 25 | public class AnalogGame extends BasicGame { 26 | 27 | private GameContainer container; 28 | private Map map = new Map(); 29 | private Player player = new Player(map); 30 | private TriggerController triggers = new TriggerController(map, player); 31 | private Camera camera = new Camera(player); 32 | private PlayerController controller = new PlayerController(player); 33 | private Hud hud = new Hud(); 34 | 35 | public static void main(String[] args) throws SlickException { 36 | new AppGameContainer(new AnalogGame(), 800, 600, false).start(); 37 | } 38 | 39 | public AnalogGame() { 40 | super("Lesson 14 :: AnalogGame"); 41 | } 42 | 43 | @Override 44 | public void init(GameContainer container) throws SlickException { 45 | this.container = container; 46 | Music background = new Music("sound/lost-in-the-meadows.ogg"); 47 | background.loop(); 48 | this.map.init(); 49 | this.player.init(); 50 | this.hud.init(); 51 | this.controller.setInput(container.getInput()); 52 | container.getInput().addKeyListener(controller); 53 | } 54 | 55 | @Override 56 | public void render(GameContainer container, Graphics g) throws SlickException { 57 | this.camera.place(container, g); 58 | this.map.renderBackground(); 59 | this.player.render(g); 60 | this.map.renderForeground(); 61 | this.hud.render(g); 62 | } 63 | 64 | @Override 65 | public void update(GameContainer container, int delta) throws SlickException { 66 | this.controller.update(); 67 | this.triggers.update(); 68 | this.player.update(delta); 69 | this.camera.update(container); 70 | } 71 | 72 | @Override 73 | public void keyReleased(int key, char c) { 74 | if (Input.KEY_ESCAPE == key) { 75 | this.container.exit(); 76 | } 77 | } 78 | 79 | @Override 80 | public void keyPressed(int key, char c) { 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /src/main/java/lesson14/Camera.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package lesson14; 5 | 6 | import org.newdawn.slick.GameContainer; 7 | import org.newdawn.slick.Graphics; 8 | 9 | /** 10 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 11 | * 12 | * @author Shionn, shionn@gmail.com http://shionn.org
13 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y- 14 | */ 15 | public class Camera { 16 | 17 | private Player player; 18 | private float xCamera, yCamera; 19 | 20 | public Camera(Player player) { 21 | this.player = player; 22 | this.xCamera = player.getX(); 23 | this.yCamera = player.getY(); 24 | } 25 | 26 | public void place(GameContainer container, Graphics g) { 27 | g.translate(container.getWidth() / 2 - (int) this.xCamera, container.getHeight() / 2 28 | - (int) this.yCamera); 29 | } 30 | 31 | public void update(GameContainer container) { 32 | int w = container.getWidth() / 4; 33 | if (this.player.getX() > this.xCamera + w) { 34 | this.xCamera = this.player.getX() - w; 35 | } else if (this.player.getX() < this.xCamera - w) { 36 | this.xCamera = this.player.getX() + w; 37 | } 38 | int h = container.getHeight() / 4; 39 | if (this.player.getY() > this.yCamera + h) { 40 | this.yCamera = this.player.getY() - h; 41 | } else if (this.player.getY() < this.yCamera - h) { 42 | this.yCamera = this.player.getY() + h; 43 | } 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/lesson14/Hud.java: -------------------------------------------------------------------------------- 1 | package lesson14; 2 | 3 | import org.newdawn.slick.Color; 4 | import org.newdawn.slick.Graphics; 5 | import org.newdawn.slick.Image; 6 | import org.newdawn.slick.SlickException; 7 | 8 | /** 9 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 10 | * 11 | * @author Shionn, shionn@gmail.com http://shionn.org
12 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y- 13 | */ 14 | public class Hud { 15 | 16 | private static final int P_BAR_X = 10; 17 | private static final int P_BAR_Y = 10; 18 | private static final int BAR_X = 84 + P_BAR_X; 19 | private static final int LIFE_BAR_Y = 4 + P_BAR_Y; 20 | private static final int MANA_BAR_Y = 24 + P_BAR_Y; 21 | private static final int XP_BAR_Y = 44 + P_BAR_Y; 22 | private static final int BAR_WIDTH = 80; 23 | private static final int BAR_HEIGHT = 16; 24 | 25 | private static final Color LIFE_COLOR = new Color(255, 0, 0); 26 | private static final Color MANA_COLOR = new Color(0, 0, 255); 27 | private static final Color XP_COLOR = new Color(0, 255, 0); 28 | 29 | private Image playerbars; 30 | 31 | public void init() throws SlickException { 32 | this.playerbars = new Image("hud/player-bar.png"); 33 | } 34 | 35 | public void render(Graphics g) { 36 | g.resetTransform(); 37 | g.setColor(LIFE_COLOR); 38 | g.fillRect(BAR_X, LIFE_BAR_Y, .9f * BAR_WIDTH, BAR_HEIGHT); 39 | g.setColor(MANA_COLOR); 40 | g.fillRect(BAR_X, MANA_BAR_Y, .8f * BAR_WIDTH, BAR_HEIGHT); 41 | g.setColor(XP_COLOR); 42 | g.fillRect(BAR_X, XP_BAR_Y, .2f * BAR_WIDTH, BAR_HEIGHT); 43 | g.drawImage(playerbars, P_BAR_X, P_BAR_Y); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/lesson14/Map.java: -------------------------------------------------------------------------------- 1 | package lesson14; 2 | 3 | import org.newdawn.slick.Color; 4 | import org.newdawn.slick.Image; 5 | import org.newdawn.slick.SlickException; 6 | import org.newdawn.slick.tiled.TiledMap; 7 | 8 | /** 9 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 10 | * 11 | * @author Shionn, shionn@gmail.com http://shionn.org
12 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y- 13 | */ 14 | public class Map { 15 | private TiledMap tiledMap; 16 | 17 | public void init() throws SlickException { 18 | this.tiledMap = new TiledMap("map/exemple-change-map.tmx"); 19 | } 20 | 21 | public void renderBackground() { 22 | this.tiledMap.render(0, 0, 0); 23 | this.tiledMap.render(0, 0, 1); 24 | this.tiledMap.render(0, 0, 2); 25 | } 26 | 27 | public void renderForeground() { 28 | this.tiledMap.render(0, 0, 3); 29 | this.tiledMap.render(0, 0, 4); 30 | } 31 | 32 | public boolean isCollision(float x, float y) { 33 | int tileW = this.tiledMap.getTileWidth(); 34 | int tileH = this.tiledMap.getTileHeight(); 35 | int logicLayer = this.tiledMap.getLayerIndex("logic"); 36 | Image tile = this.tiledMap.getTileImage((int) x / tileW, (int) y / tileH, logicLayer); 37 | boolean collision = tile != null; 38 | if (collision) { 39 | Color color = tile.getColor((int) x % tileW, (int) y % tileH); 40 | collision = color.getAlpha() > 0; 41 | } 42 | return collision; 43 | } 44 | 45 | public void changeMap(String file) throws SlickException { 46 | this.tiledMap = new TiledMap(file); 47 | } 48 | 49 | public int getObjectCount() { 50 | return this.tiledMap.getObjectCount(0); 51 | } 52 | 53 | public String getObjectType(int objectID) { 54 | return this.tiledMap.getObjectType(0, objectID); 55 | } 56 | 57 | public float getObjectX(int objectID) { 58 | return this.tiledMap.getObjectX(0, objectID); 59 | } 60 | 61 | public float getObjectY(int objectID) { 62 | return this.tiledMap.getObjectY(0, objectID); 63 | } 64 | 65 | public float getObjectWidth(int objectID) { 66 | return this.tiledMap.getObjectWidth(0, objectID); 67 | } 68 | 69 | public float getObjectHeight(int objectID) { 70 | return this.tiledMap.getObjectHeight(0, objectID); 71 | } 72 | 73 | public String getObjectProperty(int objectID, String propertyName, String def) { 74 | return this.tiledMap.getObjectProperty(0, objectID, propertyName, def); 75 | } 76 | 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/lesson14/PlayerController.java: -------------------------------------------------------------------------------- 1 | package lesson14; 2 | 3 | import org.newdawn.slick.Input; 4 | import org.newdawn.slick.KeyListener; 5 | 6 | /** 7 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 8 | * 9 | * @author Shionn, shionn@gmail.com http://shionn.org
10 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y- 11 | */ 12 | public class PlayerController implements KeyListener { 13 | 14 | private Player player; 15 | private Input input; 16 | 17 | public PlayerController(Player player) { 18 | this.player = player; 19 | } 20 | 21 | public void update() { 22 | if (input.getControllerCount() > 0) { 23 | player.setDx(input.getAxisValue(0, 1)); 24 | player.setDy(input.getAxisValue(0, 2)); 25 | } 26 | } 27 | 28 | @Override 29 | public void setInput(Input input) { 30 | this.input = input; 31 | } 32 | 33 | @Override 34 | public boolean isAcceptingInput() { 35 | return true; 36 | } 37 | 38 | @Override 39 | public void inputEnded() { 40 | 41 | } 42 | 43 | @Override 44 | public void inputStarted() { 45 | 46 | } 47 | 48 | @Override 49 | public void keyPressed(int key, char c) { 50 | switch (key) { 51 | case Input.KEY_UP: 52 | this.player.setDy(-1); 53 | break; 54 | case Input.KEY_LEFT: 55 | this.player.setDx(-1); 56 | break; 57 | case Input.KEY_DOWN: 58 | this.player.setDy(1); 59 | break; 60 | case Input.KEY_RIGHT: 61 | this.player.setDx(1); 62 | break; 63 | } 64 | } 65 | 66 | @Override 67 | public void keyReleased(int key, char c) { 68 | switch (key) { 69 | case Input.KEY_UP: 70 | case Input.KEY_DOWN: 71 | this.player.setDy(0); 72 | break; 73 | case Input.KEY_LEFT: 74 | case Input.KEY_RIGHT: 75 | this.player.setDx(0); 76 | break; 77 | } 78 | } 79 | 80 | } 81 | -------------------------------------------------------------------------------- /src/main/java/lesson14/TriggerController.java: -------------------------------------------------------------------------------- 1 | package lesson14; 2 | 3 | import org.newdawn.slick.SlickException; 4 | 5 | /** 6 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 7 | * 8 | * @author Shionn, shionn@gmail.com http://shionn.org
9 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y- 10 | */ 11 | public class TriggerController { 12 | 13 | private Map map; 14 | private Player player; 15 | 16 | public TriggerController(Map map, Player player) { 17 | this.map = map; 18 | this.player = player; 19 | } 20 | 21 | public void update() throws SlickException { 22 | this.player.setOnStair(false); 23 | for (int objectID = 0; objectID < this.map.getObjectCount(); objectID++) { 24 | if (isInTrigger(objectID)) { 25 | if ("teleport".equals(this.map.getObjectType(objectID))) { 26 | this.teleport(objectID); 27 | } else if ("stair".equals(this.map.getObjectType(objectID))) { 28 | this.player.setOnStair(true); 29 | } else if ("change-map".equals(this.map.getObjectType(objectID))) { 30 | this.changeMap(objectID); 31 | } 32 | } 33 | } 34 | } 35 | 36 | private boolean isInTrigger(int id) { 37 | return this.player.getX() > this.map.getObjectX(id) 38 | && this.player.getX() < this.map.getObjectX(id) + this.map.getObjectWidth(id) 39 | && this.player.getY() > this.map.getObjectY(id) 40 | && this.player.getY() < this.map.getObjectY(id) + this.map.getObjectHeight(id); 41 | } 42 | 43 | private void teleport(int objectID) { 44 | this.player.setX(Float.parseFloat(this.map.getObjectProperty(objectID, "dest-x", 45 | Float.toString(this.player.getX())))); 46 | this.player.setY(Float.parseFloat(this.map.getObjectProperty(objectID, "dest-y", 47 | Float.toString(this.player.getY())))); 48 | } 49 | 50 | private void changeMap(int objectID) throws SlickException { 51 | this.teleport(objectID); 52 | String newMap = this.map.getObjectProperty(objectID, "dest-map", "undefined"); 53 | if (!"undefined".equals(newMap)) { 54 | this.map.changeMap("map/" + newMap); 55 | } 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/lesson15/Camera.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package lesson15; 5 | 6 | import org.newdawn.slick.GameContainer; 7 | import org.newdawn.slick.Graphics; 8 | 9 | /** 10 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 11 | * 12 | * @author Shionn, shionn@gmail.com http://shionn.org
13 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y- 14 | */ 15 | public class Camera { 16 | 17 | private Player player; 18 | private float xCamera, yCamera; 19 | 20 | public Camera(Player player) { 21 | this.player = player; 22 | this.xCamera = player.getX(); 23 | this.yCamera = player.getY(); 24 | } 25 | 26 | public void place(GameContainer container, Graphics g) { 27 | g.translate(container.getWidth() / 2 - (int) this.xCamera, container.getHeight() / 2 28 | - (int) this.yCamera); 29 | } 30 | 31 | public void update(GameContainer container) { 32 | int w = container.getWidth() / 4; 33 | if (this.player.getX() > this.xCamera + w) { 34 | this.xCamera = this.player.getX() - w; 35 | } else if (this.player.getX() < this.xCamera - w) { 36 | this.xCamera = this.player.getX() + w; 37 | } 38 | int h = container.getHeight() / 4; 39 | if (this.player.getY() > this.yCamera + h) { 40 | this.yCamera = this.player.getY() - h; 41 | } else if (this.player.getY() < this.yCamera - h) { 42 | this.yCamera = this.player.getY() + h; 43 | } 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/lesson15/Hud.java: -------------------------------------------------------------------------------- 1 | package lesson15; 2 | 3 | import org.newdawn.slick.Color; 4 | import org.newdawn.slick.Graphics; 5 | import org.newdawn.slick.Image; 6 | import org.newdawn.slick.SlickException; 7 | 8 | /** 9 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 10 | * 11 | * @author Shionn, shionn@gmail.com http://shionn.org
12 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y- 13 | */ 14 | public class Hud { 15 | 16 | private static final int P_BAR_X = 10; 17 | private static final int P_BAR_Y = 10; 18 | private static final int BAR_X = 84 + P_BAR_X; 19 | private static final int LIFE_BAR_Y = 4 + P_BAR_Y; 20 | private static final int MANA_BAR_Y = 24 + P_BAR_Y; 21 | private static final int XP_BAR_Y = 44 + P_BAR_Y; 22 | private static final int BAR_WIDTH = 80; 23 | private static final int BAR_HEIGHT = 16; 24 | 25 | private static final Color LIFE_COLOR = new Color(255, 0, 0); 26 | private static final Color MANA_COLOR = new Color(0, 0, 255); 27 | private static final Color XP_COLOR = new Color(0, 255, 0); 28 | 29 | private Image playerbars; 30 | 31 | public void init() throws SlickException { 32 | this.playerbars = new Image("hud/player-bar.png"); 33 | } 34 | 35 | public void render(Graphics g) { 36 | g.resetTransform(); 37 | g.setColor(LIFE_COLOR); 38 | g.fillRect(BAR_X, LIFE_BAR_Y, .9f * BAR_WIDTH, BAR_HEIGHT); 39 | g.setColor(MANA_COLOR); 40 | g.fillRect(BAR_X, MANA_BAR_Y, .8f * BAR_WIDTH, BAR_HEIGHT); 41 | g.setColor(XP_COLOR); 42 | g.fillRect(BAR_X, XP_BAR_Y, .2f * BAR_WIDTH, BAR_HEIGHT); 43 | g.drawImage(playerbars, P_BAR_X, P_BAR_Y); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/lesson15/MainScreenGameState.java: -------------------------------------------------------------------------------- 1 | package lesson15; 2 | 3 | import org.newdawn.slick.GameContainer; 4 | import org.newdawn.slick.Graphics; 5 | import org.newdawn.slick.Image; 6 | import org.newdawn.slick.SlickException; 7 | import org.newdawn.slick.state.BasicGameState; 8 | import org.newdawn.slick.state.StateBasedGame; 9 | 10 | /** 11 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 12 | * 13 | * @author Shionn, shionn@gmail.com http://shionn.org
14 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y+ 15 | */ 16 | public class MainScreenGameState extends BasicGameState { 17 | 18 | public static final int ID = 1; 19 | private Image background; 20 | private StateBasedGame game; 21 | 22 | @Override 23 | public void init(GameContainer container, StateBasedGame game) throws SlickException { 24 | this.game = game; 25 | this.background = new Image("background/forest.png"); 26 | 27 | } 28 | 29 | /** 30 | * Contenons nous d'afficher l'image de fond. . 31 | */ 32 | @Override 33 | public void render(GameContainer container, StateBasedGame game, Graphics g) 34 | throws SlickException { 35 | background.draw(0, 0, container.getWidth(), container.getHeight()); 36 | g.drawString("Appuyer sur une touche", 300, 300); 37 | } 38 | 39 | @Override 40 | public void update(GameContainer container, StateBasedGame game, int delta) 41 | throws SlickException { 42 | } 43 | 44 | /** 45 | * Passer à l'ecran de jeu à l'appui de n'importe quel touche. 46 | */ 47 | @Override 48 | public void keyReleased(int key, char c) { 49 | game.enterState(MapGameState.ID); 50 | } 51 | 52 | /** 53 | * L'identifiant permet d'identifier les différentes boucles, pour passer de l'une à l'autre. 54 | */ 55 | @Override 56 | public int getID() { 57 | return ID; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/lesson15/Map.java: -------------------------------------------------------------------------------- 1 | package lesson15; 2 | 3 | import org.newdawn.slick.Color; 4 | import org.newdawn.slick.Image; 5 | import org.newdawn.slick.SlickException; 6 | import org.newdawn.slick.tiled.TiledMap; 7 | 8 | /** 9 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 10 | * 11 | * @author Shionn, shionn@gmail.com http://shionn.org
12 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y- 13 | */ 14 | public class Map { 15 | private TiledMap tiledMap; 16 | 17 | public void init() throws SlickException { 18 | this.tiledMap = new TiledMap("map/exemple-change-map.tmx"); 19 | } 20 | 21 | public void renderBackground() { 22 | this.tiledMap.render(0, 0, 0); 23 | this.tiledMap.render(0, 0, 1); 24 | this.tiledMap.render(0, 0, 2); 25 | } 26 | 27 | public void renderForeground() { 28 | this.tiledMap.render(0, 0, 3); 29 | this.tiledMap.render(0, 0, 4); 30 | } 31 | 32 | public boolean isCollision(float x, float y) { 33 | int tileW = this.tiledMap.getTileWidth(); 34 | int tileH = this.tiledMap.getTileHeight(); 35 | int logicLayer = this.tiledMap.getLayerIndex("logic"); 36 | Image tile = this.tiledMap.getTileImage((int) x / tileW, (int) y / tileH, logicLayer); 37 | boolean collision = tile != null; 38 | if (collision) { 39 | Color color = tile.getColor((int) x % tileW, (int) y % tileH); 40 | collision = color.getAlpha() > 0; 41 | } 42 | return collision; 43 | } 44 | 45 | public void changeMap(String file) throws SlickException { 46 | this.tiledMap = new TiledMap(file); 47 | } 48 | 49 | public int getObjectCount() { 50 | return this.tiledMap.getObjectCount(0); 51 | } 52 | 53 | public String getObjectType(int objectID) { 54 | return this.tiledMap.getObjectType(0, objectID); 55 | } 56 | 57 | public float getObjectX(int objectID) { 58 | return this.tiledMap.getObjectX(0, objectID); 59 | } 60 | 61 | public float getObjectY(int objectID) { 62 | return this.tiledMap.getObjectY(0, objectID); 63 | } 64 | 65 | public float getObjectWidth(int objectID) { 66 | return this.tiledMap.getObjectWidth(0, objectID); 67 | } 68 | 69 | public float getObjectHeight(int objectID) { 70 | return this.tiledMap.getObjectHeight(0, objectID); 71 | } 72 | 73 | public String getObjectProperty(int objectID, String propertyName, String def) { 74 | return this.tiledMap.getObjectProperty(0, objectID, propertyName, def); 75 | } 76 | 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/lesson15/MapGameState.java: -------------------------------------------------------------------------------- 1 | package lesson15; 2 | 3 | import org.newdawn.slick.GameContainer; 4 | import org.newdawn.slick.Graphics; 5 | import org.newdawn.slick.Input; 6 | import org.newdawn.slick.Music; 7 | import org.newdawn.slick.SlickException; 8 | import org.newdawn.slick.state.BasicGameState; 9 | import org.newdawn.slick.state.StateBasedGame; 10 | 11 | /** 12 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 13 | * 14 | * @author Shionn, shionn@gmail.com http://shionn.org
15 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y- 16 | */ 17 | public class MapGameState extends BasicGameState { 18 | public static final int ID = 2; 19 | 20 | private GameContainer container; 21 | private Map map = new Map(); 22 | private Player player = new Player(map); 23 | private TriggerController triggers = new TriggerController(map, player); 24 | private Camera camera = new Camera(player); 25 | private PlayerController controller = new PlayerController(player); 26 | private Hud hud = new Hud(); 27 | 28 | @Override 29 | public void init(GameContainer container, StateBasedGame game) throws SlickException { 30 | this.container = container; 31 | Music background = new Music("sound/lost-in-the-meadows.ogg"); 32 | background.loop(); 33 | this.map.init(); 34 | this.player.init(); 35 | this.hud.init(); 36 | this.controller.setInput(container.getInput()); 37 | container.getInput().addKeyListener(controller); 38 | } 39 | 40 | @Override 41 | public void render(GameContainer container, StateBasedGame game, Graphics g) 42 | throws SlickException { 43 | this.camera.place(container, g); 44 | this.map.renderBackground(); 45 | this.player.render(g); 46 | this.map.renderForeground(); 47 | this.hud.render(g); 48 | } 49 | 50 | @Override 51 | public void update(GameContainer container, StateBasedGame game, int delta) 52 | throws SlickException { 53 | this.controller.update(); 54 | this.triggers.update(); 55 | this.player.update(delta); 56 | this.camera.update(container); 57 | } 58 | 59 | @Override 60 | public void keyReleased(int key, char c) { 61 | if (Input.KEY_ESCAPE == key) { 62 | this.container.exit(); 63 | } 64 | } 65 | 66 | @Override 67 | public int getID() { 68 | return ID; 69 | } 70 | 71 | } 72 | -------------------------------------------------------------------------------- /src/main/java/lesson15/PlayerController.java: -------------------------------------------------------------------------------- 1 | package lesson15; 2 | 3 | import org.newdawn.slick.Input; 4 | import org.newdawn.slick.KeyListener; 5 | 6 | /** 7 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 8 | * 9 | * @author Shionn, shionn@gmail.com http://shionn.org
10 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y- 11 | */ 12 | public class PlayerController implements KeyListener { 13 | 14 | private Player player; 15 | private Input input; 16 | 17 | public PlayerController(Player player) { 18 | this.player = player; 19 | } 20 | 21 | public void update() { 22 | if (input.getControllerCount() > 0) { 23 | player.setDx(input.getAxisValue(0, 1)); 24 | player.setDy(input.getAxisValue(0, 2)); 25 | } 26 | } 27 | 28 | @Override 29 | public void setInput(Input input) { 30 | this.input = input; 31 | } 32 | 33 | @Override 34 | public boolean isAcceptingInput() { 35 | return true; 36 | } 37 | 38 | @Override 39 | public void inputEnded() { 40 | 41 | } 42 | 43 | @Override 44 | public void inputStarted() { 45 | 46 | } 47 | 48 | @Override 49 | public void keyPressed(int key, char c) { 50 | switch (key) { 51 | case Input.KEY_UP: 52 | this.player.setDy(-1); 53 | break; 54 | case Input.KEY_LEFT: 55 | this.player.setDx(-1); 56 | break; 57 | case Input.KEY_DOWN: 58 | this.player.setDy(1); 59 | break; 60 | case Input.KEY_RIGHT: 61 | this.player.setDx(1); 62 | break; 63 | } 64 | } 65 | 66 | @Override 67 | public void keyReleased(int key, char c) { 68 | switch (key) { 69 | case Input.KEY_UP: 70 | case Input.KEY_DOWN: 71 | this.player.setDy(0); 72 | break; 73 | case Input.KEY_LEFT: 74 | case Input.KEY_RIGHT: 75 | this.player.setDx(0); 76 | break; 77 | } 78 | } 79 | 80 | } 81 | -------------------------------------------------------------------------------- /src/main/java/lesson15/StateGame.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 3 | * 4 | * @author Shionn, shionn@gmail.com http://shionn.org
5 | * GCS d- s+:+ a- C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- !y- 6 | */ 7 | package lesson15; 8 | 9 | import org.newdawn.slick.AppGameContainer; 10 | import org.newdawn.slick.GameContainer; 11 | import org.newdawn.slick.SlickException; 12 | import org.newdawn.slick.state.StateBasedGame; 13 | 14 | /** 15 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 16 | * 17 | * -Djava.library.path=target/natives 18 | * 19 | * @author Shionn, shionn@gmail.com http://shionn.org
20 | * GCS d- s+:+ a C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y+ 21 | */ 22 | public class StateGame extends StateBasedGame { 23 | 24 | public static void main(String[] args) throws SlickException { 25 | new AppGameContainer(new StateGame(), 800, 600, false).start(); 26 | } 27 | 28 | public StateGame() { 29 | super("Lesson 15 :: StateGame"); 30 | } 31 | 32 | /** 33 | * Ici il suffit d'ajouter nos deux boucles de jeux. La première ajoutèe sera celle qui sera 34 | * utilisée au début 35 | */ 36 | @Override 37 | public void initStatesList(GameContainer container) throws SlickException { 38 | addState(new MainScreenGameState()); 39 | addState(new MapGameState()); 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/lesson15/TriggerController.java: -------------------------------------------------------------------------------- 1 | package lesson15; 2 | 3 | import org.newdawn.slick.SlickException; 4 | 5 | /** 6 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 7 | * 8 | * @author Shionn, shionn@gmail.com http://shionn.org
9 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y- 10 | */ 11 | public class TriggerController { 12 | 13 | private Map map; 14 | private Player player; 15 | 16 | public TriggerController(Map map, Player player) { 17 | this.map = map; 18 | this.player = player; 19 | } 20 | 21 | public void update() throws SlickException { 22 | this.player.setOnStair(false); 23 | for (int objectID = 0; objectID < this.map.getObjectCount(); objectID++) { 24 | if (isInTrigger(objectID)) { 25 | if ("teleport".equals(this.map.getObjectType(objectID))) { 26 | this.teleport(objectID); 27 | } else if ("stair".equals(this.map.getObjectType(objectID))) { 28 | this.player.setOnStair(true); 29 | } else if ("change-map".equals(this.map.getObjectType(objectID))) { 30 | this.changeMap(objectID); 31 | } 32 | } 33 | } 34 | } 35 | 36 | private boolean isInTrigger(int id) { 37 | return this.player.getX() > this.map.getObjectX(id) 38 | && this.player.getX() < this.map.getObjectX(id) + this.map.getObjectWidth(id) 39 | && this.player.getY() > this.map.getObjectY(id) 40 | && this.player.getY() < this.map.getObjectY(id) + this.map.getObjectHeight(id); 41 | } 42 | 43 | private void teleport(int objectID) { 44 | this.player.setX(Float.parseFloat(this.map.getObjectProperty(objectID, "dest-x", 45 | Float.toString(this.player.getX())))); 46 | this.player.setY(Float.parseFloat(this.map.getObjectProperty(objectID, "dest-y", 47 | Float.toString(this.player.getY())))); 48 | } 49 | 50 | private void changeMap(int objectID) throws SlickException { 51 | this.teleport(objectID); 52 | String newMap = this.map.getObjectProperty(objectID, "dest-map", "undefined"); 53 | if (!"undefined".equals(newMap)) { 54 | this.map.changeMap("map/" + newMap); 55 | } 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/lesson16/BattleGame.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 3 | * 4 | * @author Shionn, shionn@gmail.com http://shionn.org
5 | * GCS d- s+:+ a- C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- !y- 6 | */ 7 | package lesson16; 8 | 9 | import org.newdawn.slick.AppGameContainer; 10 | import org.newdawn.slick.GameContainer; 11 | import org.newdawn.slick.SlickException; 12 | import org.newdawn.slick.state.StateBasedGame; 13 | 14 | /** 15 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 16 | * 17 | * -Djava.library.path=target/natives 18 | * 19 | * @author Shionn, shionn@gmail.com http://shionn.org
20 | * GCS d- s+:+ a C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y+ 21 | */ 22 | public class BattleGame extends StateBasedGame { 23 | 24 | public static void main(String[] args) throws SlickException { 25 | new AppGameContainer(new BattleGame(), 800, 600, false).start(); 26 | } 27 | 28 | public BattleGame() { 29 | super("Lesson 16 :: BattleGame"); 30 | } 31 | 32 | /** 33 | * Ici il suffit d'ajouter nos boucles de jeux. La première ajoutèe sera celle qui sera utilisée 34 | * au début 35 | */ 36 | @Override 37 | public void initStatesList(GameContainer container) throws SlickException { 38 | addState(new MainScreenGameState()); 39 | addState(new MapGameState()); 40 | addState(new BattleGameState()); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/lesson16/BattleGameState.java: -------------------------------------------------------------------------------- 1 | package lesson16; 2 | 3 | import org.newdawn.slick.GameContainer; 4 | import org.newdawn.slick.Graphics; 5 | import org.newdawn.slick.Image; 6 | import org.newdawn.slick.SlickException; 7 | import org.newdawn.slick.state.BasicGameState; 8 | import org.newdawn.slick.state.StateBasedGame; 9 | 10 | /** 11 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 12 | * 13 | * @author Shionn, shionn@gmail.com http://shionn.org
14 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y+ 15 | */ 16 | public class BattleGameState extends BasicGameState { 17 | 18 | public static final int ID = 3; 19 | private StateBasedGame game; 20 | private Image background; 21 | private Image ennemy; 22 | private Image hero; 23 | 24 | @Override 25 | public void init(GameContainer container, StateBasedGame game) throws SlickException { 26 | this.game = game; 27 | this.background = new Image("background/battle.png"); 28 | this.ennemy = new Image("battle/gobelin.png").getScaledCopy(2); 29 | this.hero = new Image("battle/hero.png").getScaledCopy(2); 30 | 31 | } 32 | 33 | @Override 34 | public void render(GameContainer container, StateBasedGame game, Graphics g) 35 | throws SlickException { 36 | background.draw(0, 0, container.getWidth(), container.getHeight()); 37 | hero.drawCentered(container.getWidth() * 1 / 4, container.getHeight() / 2); 38 | ennemy.drawCentered(container.getWidth() * 3 / 4, container.getHeight() / 2); 39 | } 40 | 41 | @Override 42 | public void update(GameContainer container, StateBasedGame game, int delta) 43 | throws SlickException { 44 | } 45 | 46 | @Override 47 | public void keyPressed(int key, char c) { 48 | game.enterState(MapGameState.ID); 49 | } 50 | 51 | @Override 52 | public int getID() { 53 | return ID; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/lesson16/Camera.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package lesson16; 5 | 6 | import org.newdawn.slick.GameContainer; 7 | import org.newdawn.slick.Graphics; 8 | 9 | /** 10 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 11 | * 12 | * @author Shionn, shionn@gmail.com http://shionn.org
13 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y- 14 | */ 15 | public class Camera { 16 | 17 | private Player player; 18 | private float xCamera, yCamera; 19 | 20 | public Camera(Player player) { 21 | this.player = player; 22 | this.xCamera = player.getX(); 23 | this.yCamera = player.getY(); 24 | } 25 | 26 | public void place(GameContainer container, Graphics g) { 27 | g.translate(container.getWidth() / 2 - (int) this.xCamera, container.getHeight() / 2 28 | - (int) this.yCamera); 29 | } 30 | 31 | public void update(GameContainer container) { 32 | int w = container.getWidth() / 4; 33 | if (this.player.getX() > this.xCamera + w) { 34 | this.xCamera = this.player.getX() - w; 35 | } else if (this.player.getX() < this.xCamera - w) { 36 | this.xCamera = this.player.getX() + w; 37 | } 38 | int h = container.getHeight() / 4; 39 | if (this.player.getY() > this.yCamera + h) { 40 | this.yCamera = this.player.getY() - h; 41 | } else if (this.player.getY() < this.yCamera - h) { 42 | this.yCamera = this.player.getY() + h; 43 | } 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/lesson16/Hud.java: -------------------------------------------------------------------------------- 1 | package lesson16; 2 | 3 | import org.newdawn.slick.Color; 4 | import org.newdawn.slick.Graphics; 5 | import org.newdawn.slick.Image; 6 | import org.newdawn.slick.SlickException; 7 | 8 | /** 9 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 10 | * 11 | * @author Shionn, shionn@gmail.com http://shionn.org
12 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y- 13 | */ 14 | public class Hud { 15 | 16 | private static final int P_BAR_X = 10; 17 | private static final int P_BAR_Y = 10; 18 | private static final int BAR_X = 84 + P_BAR_X; 19 | private static final int LIFE_BAR_Y = 4 + P_BAR_Y; 20 | private static final int MANA_BAR_Y = 24 + P_BAR_Y; 21 | private static final int XP_BAR_Y = 44 + P_BAR_Y; 22 | private static final int BAR_WIDTH = 80; 23 | private static final int BAR_HEIGHT = 16; 24 | 25 | private static final Color LIFE_COLOR = new Color(255, 0, 0); 26 | private static final Color MANA_COLOR = new Color(0, 0, 255); 27 | private static final Color XP_COLOR = new Color(0, 255, 0); 28 | 29 | private Image playerbars; 30 | 31 | public void init() throws SlickException { 32 | this.playerbars = new Image("hud/player-bar.png"); 33 | } 34 | 35 | public void render(Graphics g) { 36 | g.resetTransform(); 37 | g.setColor(LIFE_COLOR); 38 | g.fillRect(BAR_X, LIFE_BAR_Y, .9f * BAR_WIDTH, BAR_HEIGHT); 39 | g.setColor(MANA_COLOR); 40 | g.fillRect(BAR_X, MANA_BAR_Y, .8f * BAR_WIDTH, BAR_HEIGHT); 41 | g.setColor(XP_COLOR); 42 | g.fillRect(BAR_X, XP_BAR_Y, .2f * BAR_WIDTH, BAR_HEIGHT); 43 | g.drawImage(playerbars, P_BAR_X, P_BAR_Y); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/lesson16/MainScreenGameState.java: -------------------------------------------------------------------------------- 1 | package lesson16; 2 | 3 | import org.newdawn.slick.GameContainer; 4 | import org.newdawn.slick.Graphics; 5 | import org.newdawn.slick.Image; 6 | import org.newdawn.slick.SlickException; 7 | import org.newdawn.slick.state.BasicGameState; 8 | import org.newdawn.slick.state.StateBasedGame; 9 | 10 | /** 11 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 12 | * 13 | * @author Shionn, shionn@gmail.com http://shionn.org
14 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y+ 15 | */ 16 | public class MainScreenGameState extends BasicGameState { 17 | 18 | public static final int ID = 1; 19 | private Image background; 20 | private StateBasedGame game; 21 | 22 | @Override 23 | public void init(GameContainer container, StateBasedGame game) throws SlickException { 24 | this.game = game; 25 | this.background = new Image("background/forest.png"); 26 | 27 | } 28 | 29 | /** 30 | * Contenons nous d'afficher l'image de fond. . 31 | */ 32 | @Override 33 | public void render(GameContainer container, StateBasedGame game, Graphics g) 34 | throws SlickException { 35 | background.draw(0, 0, container.getWidth(), container.getHeight()); 36 | g.drawString("Appuyer sur une touche", 300, 300); 37 | } 38 | 39 | @Override 40 | public void update(GameContainer container, StateBasedGame game, int delta) 41 | throws SlickException { 42 | } 43 | 44 | /** 45 | * Passer à l'ecran de jeu à l'appui de n'importe quel touche. 46 | */ 47 | @Override 48 | public void keyReleased(int key, char c) { 49 | game.enterState(MapGameState.ID); 50 | } 51 | 52 | /** 53 | * L'identifiant permet d'identifier les différentes boucles, pour passer de l'une à l'autre. 54 | */ 55 | @Override 56 | public int getID() { 57 | return ID; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/lesson16/Map.java: -------------------------------------------------------------------------------- 1 | package lesson16; 2 | 3 | import org.newdawn.slick.Color; 4 | import org.newdawn.slick.Image; 5 | import org.newdawn.slick.SlickException; 6 | import org.newdawn.slick.tiled.TiledMap; 7 | 8 | /** 9 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 10 | * 11 | * @author Shionn, shionn@gmail.com http://shionn.org
12 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y- 13 | */ 14 | public class Map { 15 | private TiledMap tiledMap; 16 | 17 | public void init() throws SlickException { 18 | this.tiledMap = new TiledMap("map/exemple-change-map.tmx"); 19 | } 20 | 21 | public void renderBackground() { 22 | this.tiledMap.render(0, 0, 0); 23 | this.tiledMap.render(0, 0, 1); 24 | this.tiledMap.render(0, 0, 2); 25 | } 26 | 27 | public void renderForeground() { 28 | this.tiledMap.render(0, 0, 3); 29 | this.tiledMap.render(0, 0, 4); 30 | } 31 | 32 | public boolean isCollision(float x, float y) { 33 | int tileW = this.tiledMap.getTileWidth(); 34 | int tileH = this.tiledMap.getTileHeight(); 35 | int logicLayer = this.tiledMap.getLayerIndex("logic"); 36 | Image tile = this.tiledMap.getTileImage((int) x / tileW, (int) y / tileH, logicLayer); 37 | boolean collision = tile != null; 38 | if (collision) { 39 | Color color = tile.getColor((int) x % tileW, (int) y % tileH); 40 | collision = color.getAlpha() > 0; 41 | } 42 | return collision; 43 | } 44 | 45 | public void changeMap(String file) throws SlickException { 46 | this.tiledMap = new TiledMap(file); 47 | } 48 | 49 | public int getObjectCount() { 50 | return this.tiledMap.getObjectCount(0); 51 | } 52 | 53 | public String getObjectType(int objectID) { 54 | return this.tiledMap.getObjectType(0, objectID); 55 | } 56 | 57 | public float getObjectX(int objectID) { 58 | return this.tiledMap.getObjectX(0, objectID); 59 | } 60 | 61 | public float getObjectY(int objectID) { 62 | return this.tiledMap.getObjectY(0, objectID); 63 | } 64 | 65 | public float getObjectWidth(int objectID) { 66 | return this.tiledMap.getObjectWidth(0, objectID); 67 | } 68 | 69 | public float getObjectHeight(int objectID) { 70 | return this.tiledMap.getObjectHeight(0, objectID); 71 | } 72 | 73 | public String getObjectProperty(int objectID, String propertyName, String def) { 74 | return this.tiledMap.getObjectProperty(0, objectID, propertyName, def); 75 | } 76 | 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/lesson16/MapGameState.java: -------------------------------------------------------------------------------- 1 | package lesson16; 2 | 3 | import org.newdawn.slick.GameContainer; 4 | import org.newdawn.slick.Graphics; 5 | import org.newdawn.slick.Input; 6 | import org.newdawn.slick.Music; 7 | import org.newdawn.slick.SlickException; 8 | import org.newdawn.slick.state.BasicGameState; 9 | import org.newdawn.slick.state.StateBasedGame; 10 | 11 | /** 12 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 13 | * 14 | * @author Shionn, shionn@gmail.com http://shionn.org
15 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y- 16 | */ 17 | public class MapGameState extends BasicGameState { 18 | public static final int ID = 2; 19 | 20 | private GameContainer container; 21 | private Map map = new Map(); 22 | private Player player = new Player(map); 23 | private TriggerController triggers = new TriggerController(map, player); 24 | private Camera camera = new Camera(player); 25 | private PlayerController controller = new PlayerController(player); 26 | private Hud hud = new Hud(); 27 | 28 | @Override 29 | public void init(GameContainer container, StateBasedGame game) throws SlickException { 30 | this.container = container; 31 | Music background = new Music("sound/lost-in-the-meadows.ogg"); 32 | background.loop(); 33 | this.map.init(); 34 | this.player.init(); 35 | this.hud.init(); 36 | this.controller.setInput(container.getInput()); 37 | container.getInput().addKeyListener(controller); 38 | } 39 | 40 | @Override 41 | public void render(GameContainer container, StateBasedGame game, Graphics g) 42 | throws SlickException { 43 | this.camera.place(container, g); 44 | this.map.renderBackground(); 45 | this.player.render(g); 46 | this.map.renderForeground(); 47 | this.hud.render(g); 48 | } 49 | 50 | @Override 51 | public void update(GameContainer container, StateBasedGame game, int delta) 52 | throws SlickException { 53 | this.controller.update(); 54 | this.triggers.update(); 55 | this.player.update(delta); 56 | this.camera.update(container); 57 | if (Math.random() < 0.001 && player.isMoving()) { 58 | game.enterState(BattleGameState.ID); 59 | } 60 | } 61 | 62 | @Override 63 | public void keyReleased(int key, char c) { 64 | if (Input.KEY_ESCAPE == key) { 65 | this.container.exit(); 66 | } 67 | } 68 | 69 | @Override 70 | public int getID() { 71 | return ID; 72 | } 73 | 74 | } 75 | -------------------------------------------------------------------------------- /src/main/java/lesson16/PlayerController.java: -------------------------------------------------------------------------------- 1 | package lesson16; 2 | 3 | import org.newdawn.slick.Input; 4 | import org.newdawn.slick.KeyListener; 5 | 6 | /** 7 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 8 | * 9 | * @author Shionn, shionn@gmail.com http://shionn.org
10 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y- 11 | */ 12 | public class PlayerController implements KeyListener { 13 | 14 | private Player player; 15 | private Input input; 16 | 17 | public PlayerController(Player player) { 18 | this.player = player; 19 | } 20 | 21 | public void update() { 22 | if (input.getControllerCount() > 0) { 23 | player.setDx(input.getAxisValue(0, 1)); 24 | player.setDy(input.getAxisValue(0, 2)); 25 | } 26 | } 27 | 28 | @Override 29 | public void setInput(Input input) { 30 | this.input = input; 31 | } 32 | 33 | @Override 34 | public boolean isAcceptingInput() { 35 | return true; 36 | } 37 | 38 | @Override 39 | public void inputEnded() { 40 | 41 | } 42 | 43 | @Override 44 | public void inputStarted() { 45 | 46 | } 47 | 48 | @Override 49 | public void keyPressed(int key, char c) { 50 | switch (key) { 51 | case Input.KEY_UP: 52 | this.player.setDy(-1); 53 | break; 54 | case Input.KEY_LEFT: 55 | this.player.setDx(-1); 56 | break; 57 | case Input.KEY_DOWN: 58 | this.player.setDy(1); 59 | break; 60 | case Input.KEY_RIGHT: 61 | this.player.setDx(1); 62 | break; 63 | } 64 | } 65 | 66 | @Override 67 | public void keyReleased(int key, char c) { 68 | switch (key) { 69 | case Input.KEY_UP: 70 | case Input.KEY_DOWN: 71 | this.player.setDy(0); 72 | break; 73 | case Input.KEY_LEFT: 74 | case Input.KEY_RIGHT: 75 | this.player.setDx(0); 76 | break; 77 | } 78 | } 79 | 80 | } 81 | -------------------------------------------------------------------------------- /src/main/java/lesson16/TriggerController.java: -------------------------------------------------------------------------------- 1 | package lesson16; 2 | 3 | import org.newdawn.slick.SlickException; 4 | 5 | /** 6 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 7 | * 8 | * @author Shionn, shionn@gmail.com http://shionn.org
9 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y- 10 | */ 11 | public class TriggerController { 12 | 13 | private Map map; 14 | private Player player; 15 | 16 | public TriggerController(Map map, Player player) { 17 | this.map = map; 18 | this.player = player; 19 | } 20 | 21 | public void update() throws SlickException { 22 | this.player.setOnStair(false); 23 | for (int objectID = 0; objectID < this.map.getObjectCount(); objectID++) { 24 | if (isInTrigger(objectID)) { 25 | if ("teleport".equals(this.map.getObjectType(objectID))) { 26 | this.teleport(objectID); 27 | } else if ("stair".equals(this.map.getObjectType(objectID))) { 28 | this.player.setOnStair(true); 29 | } else if ("change-map".equals(this.map.getObjectType(objectID))) { 30 | this.changeMap(objectID); 31 | } 32 | } 33 | } 34 | } 35 | 36 | private boolean isInTrigger(int id) { 37 | return this.player.getX() > this.map.getObjectX(id) 38 | && this.player.getX() < this.map.getObjectX(id) + this.map.getObjectWidth(id) 39 | && this.player.getY() > this.map.getObjectY(id) 40 | && this.player.getY() < this.map.getObjectY(id) + this.map.getObjectHeight(id); 41 | } 42 | 43 | private void teleport(int objectID) { 44 | this.player.setX(Float.parseFloat(this.map.getObjectProperty(objectID, "dest-x", 45 | Float.toString(this.player.getX())))); 46 | this.player.setY(Float.parseFloat(this.map.getObjectProperty(objectID, "dest-y", 47 | Float.toString(this.player.getY())))); 48 | } 49 | 50 | private void changeMap(int objectID) throws SlickException { 51 | this.teleport(objectID); 52 | String newMap = this.map.getObjectProperty(objectID, "dest-map", "undefined"); 53 | if (!"undefined".equals(newMap)) { 54 | this.map.changeMap("map/" + newMap); 55 | } 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/lesson16/randombackground/BattleGameState.java: -------------------------------------------------------------------------------- 1 | package lesson16.randombackground; 2 | 3 | import java.util.Random; 4 | 5 | import org.newdawn.slick.GameContainer; 6 | import org.newdawn.slick.Graphics; 7 | import org.newdawn.slick.Image; 8 | import org.newdawn.slick.SlickException; 9 | import org.newdawn.slick.state.BasicGameState; 10 | import org.newdawn.slick.state.StateBasedGame; 11 | 12 | import lesson16.MapGameState; 13 | 14 | /** 15 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 16 | * 17 | * @author Shionn, shionn@gmail.com http://shionn.org
18 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y+ 19 | */ 20 | public class BattleGameState extends BasicGameState { 21 | 22 | public static final int ID = 3; 23 | private Random random = new Random(); 24 | private StateBasedGame game; 25 | private Image[] backgrounds = new Image[2]; // Les background 26 | private int chosenBackground = 0; // le background actif 27 | private Image ennemy; 28 | private Image hero; 29 | 30 | /** 31 | * A l'initialisation du jeux je charge tous les backgrounds (ici que deux) 32 | */ 33 | @Override 34 | public void init(GameContainer container, StateBasedGame game) throws SlickException { 35 | this.game = game; 36 | this.backgrounds[0] = new Image("background/battle.png"); 37 | this.backgrounds[1] = new Image("background/battle-alternatif.png"); 38 | this.ennemy = new Image("battle/gobelin.png").getScaledCopy(2); 39 | this.hero = new Image("battle/hero.png").getScaledCopy(2); 40 | } 41 | 42 | /** 43 | * Quand je commence un combat (c'est à dire quand j'entre dans cette boucle) je choisis 44 | * aléatoirement le fond. La méthode serait la même pour toutes autre chose. 45 | */ 46 | @Override 47 | public void enter(GameContainer container, StateBasedGame game) throws SlickException { 48 | this.chosenBackground = random.nextInt(backgrounds.length); 49 | } 50 | 51 | /** 52 | * J'affiche le background choisi 53 | */ 54 | @Override 55 | public void render(GameContainer container, StateBasedGame game, Graphics g) 56 | throws SlickException { 57 | backgrounds[chosenBackground].draw(0, 0, container.getWidth(), container.getHeight()); 58 | hero.drawCentered(container.getWidth() * 1 / 4, container.getHeight() / 2); 59 | ennemy.drawCentered(container.getWidth() * 3 / 4, container.getHeight() / 2); 60 | } 61 | 62 | @Override 63 | public void update(GameContainer container, StateBasedGame game, int delta) 64 | throws SlickException { 65 | } 66 | 67 | @Override 68 | public void keyPressed(int key, char c) { 69 | game.enterState(MapGameState.ID); 70 | } 71 | 72 | @Override 73 | public int getID() { 74 | return ID; 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src/main/java/lesson16/randombackground/RandomBackgroundBattleGame.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 3 | * 4 | * @author Shionn, shionn@gmail.com http://shionn.org
5 | * GCS d- s+:+ a- C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- !y- 6 | */ 7 | package lesson16.randombackground; 8 | 9 | import org.newdawn.slick.AppGameContainer; 10 | import org.newdawn.slick.GameContainer; 11 | import org.newdawn.slick.SlickException; 12 | import org.newdawn.slick.state.StateBasedGame; 13 | 14 | import lesson16.MainScreenGameState; 15 | import lesson16.MapGameState; 16 | 17 | /** 18 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 19 | * 20 | * -Djava.library.path=target/natives 21 | * 22 | * @author Shionn, shionn@gmail.com http://shionn.org
23 | * GCS d- s+:+ a C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y+ 24 | */ 25 | public class RandomBackgroundBattleGame extends StateBasedGame { 26 | 27 | public static void main(String[] args) throws SlickException { 28 | new AppGameContainer(new RandomBackgroundBattleGame(), 800, 600, false).start(); 29 | } 30 | 31 | /** 32 | * La seule difference etant au niveau de la classe de BattleGameState, tous le reste est 33 | * identique à la leçon 16 34 | */ 35 | public RandomBackgroundBattleGame() { 36 | super("Lesson 16 Question de Lecteur :: RandomBackgroundBattleGame"); 37 | } 38 | 39 | /** 40 | * Ici il suffit d'ajouter nos boucles de jeux. La première ajoutèe sera celle qui sera utilisée 41 | * au début 42 | */ 43 | @Override 44 | public void initStatesList(GameContainer container) throws SlickException { 45 | addState(new MainScreenGameState()); 46 | addState(new MapGameState()); 47 | addState(new BattleGameState()); 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/lesson17/BattleCommand.java: -------------------------------------------------------------------------------- 1 | package lesson17; 2 | 3 | import org.newdawn.slick.command.Command; 4 | 5 | public enum BattleCommand implements Command { 6 | ATTACK, DEFEND, FLEE 7 | 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/lesson17/BattleEnnemy.java: -------------------------------------------------------------------------------- 1 | package lesson17; 2 | 3 | import org.newdawn.slick.Color; 4 | import org.newdawn.slick.Font; 5 | import org.newdawn.slick.GameContainer; 6 | import org.newdawn.slick.Graphics; 7 | import org.newdawn.slick.Image; 8 | import org.newdawn.slick.SlickException; 9 | 10 | /** 11 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 12 | * 13 | * @author Shionn, shionn@gmail.com http://shionn.org
14 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y+ 15 | */ 16 | public class BattleEnnemy { 17 | private int pv = 12; 18 | private Image ennemy; 19 | 20 | public void init() throws SlickException { 21 | this.ennemy = new Image("battle/gobelin.png").getScaledCopy(2); 22 | } 23 | 24 | public void render(GameContainer container, Graphics g) { 25 | this.ennemy.drawCentered(container.getWidth() * 3 / 4, container.getHeight() / 2); 26 | Font font = g.getFont(); 27 | String text = Integer.toString(pv); 28 | font.drawString(container.getWidth() * 3 / 4 - font.getWidth(text) / 2, 29 | container.getHeight() / 2 - ennemy.getHeight() / 2 - font.getLineHeight(), text, 30 | Color.white); 31 | } 32 | 33 | public void setPv(int pv) { 34 | this.pv = pv; 35 | } 36 | 37 | public int getPv() { 38 | return pv; 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/lesson17/BattleGame.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 3 | * 4 | * @author Shionn, shionn@gmail.com http://shionn.org
5 | * GCS d- s+:+ a- C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- !y- 6 | */ 7 | package lesson17; 8 | 9 | import org.newdawn.slick.AppGameContainer; 10 | import org.newdawn.slick.GameContainer; 11 | import org.newdawn.slick.SlickException; 12 | import org.newdawn.slick.state.StateBasedGame; 13 | 14 | /** 15 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 16 | * 17 | * -Djava.library.path=target/natives 18 | * 19 | * @author Shionn, shionn@gmail.com http://shionn.org
20 | * GCS d- s+:+ a C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y+ 21 | */ 22 | public class BattleGame extends StateBasedGame { 23 | 24 | public static void main(String[] args) throws SlickException { 25 | new AppGameContainer(new BattleGame(), 800, 600, false).start(); 26 | } 27 | 28 | public BattleGame() { 29 | super("Lesson 17 :: BattleGame"); 30 | } 31 | 32 | /** 33 | * Ici il suffit d'ajouter nos boucles de jeux. La première ajoutèe sera celle qui sera utilisée 34 | * au début 35 | */ 36 | @Override 37 | public void initStatesList(GameContainer container) throws SlickException { 38 | addState(new MainScreenGameState()); 39 | addState(new MapGameState()); 40 | addState(new BattleGameState()); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/lesson17/BattleGameState.java: -------------------------------------------------------------------------------- 1 | package lesson17; 2 | 3 | import org.newdawn.slick.GameContainer; 4 | import org.newdawn.slick.Graphics; 5 | import org.newdawn.slick.Image; 6 | import org.newdawn.slick.Input; 7 | import org.newdawn.slick.SlickException; 8 | import org.newdawn.slick.command.InputProvider; 9 | import org.newdawn.slick.command.KeyControl; 10 | import org.newdawn.slick.state.BasicGameState; 11 | import org.newdawn.slick.state.StateBasedGame; 12 | 13 | /** 14 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 15 | * 16 | * @author Shionn, shionn@gmail.com http://shionn.org
17 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y+ 18 | */ 19 | public class BattleGameState extends BasicGameState { 20 | 21 | public static final int ID = 3; 22 | private Image background; 23 | private BattleEnnemy ennemy = new BattleEnnemy(); 24 | private BattlePlayer player = new BattlePlayer(); 25 | 26 | @Override 27 | public void init(GameContainer container, StateBasedGame game) throws SlickException { 28 | this.background = new Image("background/battle.png"); 29 | this.ennemy.init(); 30 | this.player.init(); 31 | 32 | InputProvider provider = new InputProvider(container.getInput()); 33 | provider.bindCommand(new KeyControl(Input.KEY_A), BattleCommand.ATTACK); 34 | provider.bindCommand(new KeyControl(Input.KEY_D), BattleCommand.DEFEND); 35 | provider.bindCommand(new KeyControl(Input.KEY_F), BattleCommand.FLEE); 36 | provider.addListener(new BattleController(player, ennemy, game)); 37 | } 38 | 39 | @Override 40 | public void render(GameContainer container, StateBasedGame game, Graphics g) 41 | throws SlickException { 42 | this.background.draw(0, 0, container.getWidth(), container.getHeight()); 43 | this.player.render(container, g); 44 | this.ennemy.render(container, g); 45 | } 46 | 47 | @Override 48 | public void update(GameContainer container, StateBasedGame game, int delta) 49 | throws SlickException { 50 | } 51 | 52 | @Override 53 | public void keyPressed(int key, char c) { 54 | } 55 | 56 | @Override 57 | public int getID() { 58 | return ID; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/lesson17/BattlePlayer.java: -------------------------------------------------------------------------------- 1 | package lesson17; 2 | 3 | import org.newdawn.slick.Color; 4 | import org.newdawn.slick.Font; 5 | import org.newdawn.slick.GameContainer; 6 | import org.newdawn.slick.Graphics; 7 | import org.newdawn.slick.Image; 8 | import org.newdawn.slick.SlickException; 9 | 10 | /** 11 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 12 | * 13 | * @author Shionn, shionn@gmail.com http://shionn.org
14 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y+ 15 | */ 16 | public class BattlePlayer { 17 | 18 | private int pv = 50; 19 | private Image hero; 20 | 21 | public void init() throws SlickException { 22 | this.hero = new Image("battle/hero.png").getScaledCopy(2); 23 | } 24 | 25 | public void render(GameContainer container, Graphics g) { 26 | hero.drawCentered(container.getWidth() * 1 / 4, container.getHeight() / 2); 27 | Font font = g.getFont(); 28 | String text = Integer.toString(pv); 29 | font.drawString(container.getWidth() * 1 / 4 - font.getWidth(text) / 2, 30 | container.getHeight() / 2 - hero.getHeight() / 2 - font.getLineHeight(), text, 31 | Color.white); 32 | } 33 | 34 | public int getPv() { 35 | return pv; 36 | } 37 | 38 | public void setPv(int pv) { 39 | this.pv = pv; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/lesson17/MainScreenGameState.java: -------------------------------------------------------------------------------- 1 | package lesson17; 2 | 3 | import org.newdawn.slick.GameContainer; 4 | import org.newdawn.slick.Graphics; 5 | import org.newdawn.slick.Image; 6 | import org.newdawn.slick.SlickException; 7 | import org.newdawn.slick.state.BasicGameState; 8 | import org.newdawn.slick.state.StateBasedGame; 9 | 10 | /** 11 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 12 | * 13 | * @author Shionn, shionn@gmail.com http://shionn.org
14 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y+ 15 | */ 16 | public class MainScreenGameState extends BasicGameState { 17 | 18 | public static final int ID = 1; 19 | private Image background; 20 | private StateBasedGame game; 21 | 22 | @Override 23 | public void init(GameContainer container, StateBasedGame game) throws SlickException { 24 | this.game = game; 25 | this.background = new Image("background/forest.png"); 26 | 27 | } 28 | 29 | /** 30 | * Contenons nous d'afficher l'image de fond. . 31 | */ 32 | @Override 33 | public void render(GameContainer container, StateBasedGame game, Graphics g) 34 | throws SlickException { 35 | background.draw(0, 0, container.getWidth(), container.getHeight()); 36 | g.drawString("Appuyer sur une touche", 300, 300); 37 | } 38 | 39 | @Override 40 | public void update(GameContainer container, StateBasedGame game, int delta) 41 | throws SlickException { 42 | } 43 | 44 | /** 45 | * Passer à l'ecran de jeu à l'appui de n'importe quel touche. 46 | */ 47 | @Override 48 | public void keyReleased(int key, char c) { 49 | game.enterState(MapGameState.ID); 50 | } 51 | 52 | /** 53 | * L'identifiant permet d'identifier les différentes boucles, pour passer de l'une à l'autre. 54 | */ 55 | @Override 56 | public int getID() { 57 | return ID; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/lesson17/Map.java: -------------------------------------------------------------------------------- 1 | package lesson17; 2 | 3 | import org.newdawn.slick.Color; 4 | import org.newdawn.slick.Image; 5 | import org.newdawn.slick.SlickException; 6 | import org.newdawn.slick.tiled.TiledMap; 7 | 8 | /** 9 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 10 | * 11 | * @author Shionn, shionn@gmail.com http://shionn.org
12 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y- 13 | */ 14 | public class Map { 15 | private TiledMap tiledMap; 16 | 17 | public void init() throws SlickException { 18 | this.tiledMap = new TiledMap("map/exemple-change-map.tmx"); 19 | } 20 | 21 | public void renderBackground() { 22 | this.tiledMap.render(0, 0, 0); 23 | this.tiledMap.render(0, 0, 1); 24 | this.tiledMap.render(0, 0, 2); 25 | } 26 | 27 | public void renderForeground() { 28 | this.tiledMap.render(0, 0, 3); 29 | this.tiledMap.render(0, 0, 4); 30 | } 31 | 32 | public boolean isCollision(float x, float y) { 33 | int tileW = this.tiledMap.getTileWidth(); 34 | int tileH = this.tiledMap.getTileHeight(); 35 | int logicLayer = this.tiledMap.getLayerIndex("logic"); 36 | Image tile = this.tiledMap.getTileImage((int) x / tileW, (int) y / tileH, logicLayer); 37 | boolean collision = tile != null; 38 | if (collision) { 39 | Color color = tile.getColor((int) x % tileW, (int) y % tileH); 40 | collision = color.getAlpha() > 0; 41 | } 42 | return collision; 43 | } 44 | 45 | public void changeMap(String file) throws SlickException { 46 | this.tiledMap = new TiledMap(file); 47 | } 48 | 49 | public int getObjectCount() { 50 | return this.tiledMap.getObjectCount(0); 51 | } 52 | 53 | public String getObjectType(int objectID) { 54 | return this.tiledMap.getObjectType(0, objectID); 55 | } 56 | 57 | public float getObjectX(int objectID) { 58 | return this.tiledMap.getObjectX(0, objectID); 59 | } 60 | 61 | public float getObjectY(int objectID) { 62 | return this.tiledMap.getObjectY(0, objectID); 63 | } 64 | 65 | public float getObjectWidth(int objectID) { 66 | return this.tiledMap.getObjectWidth(0, objectID); 67 | } 68 | 69 | public float getObjectHeight(int objectID) { 70 | return this.tiledMap.getObjectHeight(0, objectID); 71 | } 72 | 73 | public String getObjectProperty(int objectID, String propertyName, String def) { 74 | return this.tiledMap.getObjectProperty(0, objectID, propertyName, def); 75 | } 76 | 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/lesson17/MapCamera.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package lesson17; 5 | 6 | import org.newdawn.slick.GameContainer; 7 | import org.newdawn.slick.Graphics; 8 | 9 | /** 10 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 11 | * 12 | * @author Shionn, shionn@gmail.com http://shionn.org
13 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y- 14 | */ 15 | public class MapCamera { 16 | 17 | private MapPlayer player; 18 | private float xCamera, yCamera; 19 | 20 | public MapCamera(MapPlayer player) { 21 | this.player = player; 22 | this.xCamera = player.getX(); 23 | this.yCamera = player.getY(); 24 | } 25 | 26 | public void place(GameContainer container, Graphics g) { 27 | g.translate(container.getWidth() / 2 - (int) this.xCamera, container.getHeight() / 2 28 | - (int) this.yCamera); 29 | } 30 | 31 | public void update(GameContainer container) { 32 | int w = container.getWidth() / 4; 33 | if (this.player.getX() > this.xCamera + w) { 34 | this.xCamera = this.player.getX() - w; 35 | } else if (this.player.getX() < this.xCamera - w) { 36 | this.xCamera = this.player.getX() + w; 37 | } 38 | int h = container.getHeight() / 4; 39 | if (this.player.getY() > this.yCamera + h) { 40 | this.yCamera = this.player.getY() - h; 41 | } else if (this.player.getY() < this.yCamera - h) { 42 | this.yCamera = this.player.getY() + h; 43 | } 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/lesson17/MapGameState.java: -------------------------------------------------------------------------------- 1 | package lesson17; 2 | 3 | import org.newdawn.slick.GameContainer; 4 | import org.newdawn.slick.Graphics; 5 | import org.newdawn.slick.Input; 6 | import org.newdawn.slick.Music; 7 | import org.newdawn.slick.SlickException; 8 | import org.newdawn.slick.state.BasicGameState; 9 | import org.newdawn.slick.state.StateBasedGame; 10 | 11 | /** 12 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 13 | * 14 | * @author Shionn, shionn@gmail.com http://shionn.org
15 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y- 16 | */ 17 | public class MapGameState extends BasicGameState { 18 | public static final int ID = 2; 19 | 20 | private GameContainer container; 21 | private Map map = new Map(); 22 | private MapPlayer player = new MapPlayer(map); 23 | private MapTriggerController triggers = new MapTriggerController(map, player); 24 | private MapCamera camera = new MapCamera(player); 25 | private MapPlayerController controller = new MapPlayerController(player); 26 | private MapHud hud = new MapHud(); 27 | 28 | @Override 29 | public void init(GameContainer container, StateBasedGame game) throws SlickException { 30 | this.container = container; 31 | Music background = new Music("sound/lost-in-the-meadows.ogg"); 32 | background.loop(); 33 | this.map.init(); 34 | this.player.init(); 35 | this.hud.init(); 36 | this.controller.setInput(container.getInput()); 37 | container.getInput().addKeyListener(controller); 38 | } 39 | 40 | @Override 41 | public void render(GameContainer container, StateBasedGame game, Graphics g) 42 | throws SlickException { 43 | this.camera.place(container, g); 44 | this.map.renderBackground(); 45 | this.player.render(g); 46 | this.map.renderForeground(); 47 | this.hud.render(g); 48 | } 49 | 50 | @Override 51 | public void update(GameContainer container, StateBasedGame game, int delta) 52 | throws SlickException { 53 | this.controller.update(); 54 | this.triggers.update(); 55 | this.player.update(delta); 56 | this.camera.update(container); 57 | if (Math.random() < 0.01 && player.isMoving()) { 58 | game.enterState(BattleGameState.ID); 59 | } 60 | } 61 | 62 | @Override 63 | public void keyReleased(int key, char c) { 64 | if (Input.KEY_ESCAPE == key) { 65 | this.container.exit(); 66 | } 67 | } 68 | 69 | @Override 70 | public int getID() { 71 | return ID; 72 | } 73 | 74 | } 75 | -------------------------------------------------------------------------------- /src/main/java/lesson17/MapHud.java: -------------------------------------------------------------------------------- 1 | package lesson17; 2 | 3 | import org.newdawn.slick.Color; 4 | import org.newdawn.slick.Graphics; 5 | import org.newdawn.slick.Image; 6 | import org.newdawn.slick.SlickException; 7 | 8 | /** 9 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 10 | * 11 | * @author Shionn, shionn@gmail.com http://shionn.org
12 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y- 13 | */ 14 | public class MapHud { 15 | 16 | private static final int P_BAR_X = 10; 17 | private static final int P_BAR_Y = 10; 18 | private static final int BAR_X = 84 + P_BAR_X; 19 | private static final int LIFE_BAR_Y = 4 + P_BAR_Y; 20 | private static final int MANA_BAR_Y = 24 + P_BAR_Y; 21 | private static final int XP_BAR_Y = 44 + P_BAR_Y; 22 | private static final int BAR_WIDTH = 80; 23 | private static final int BAR_HEIGHT = 16; 24 | 25 | private static final Color LIFE_COLOR = new Color(255, 0, 0); 26 | private static final Color MANA_COLOR = new Color(0, 0, 255); 27 | private static final Color XP_COLOR = new Color(0, 255, 0); 28 | 29 | private Image playerbars; 30 | 31 | public void init() throws SlickException { 32 | this.playerbars = new Image("hud/player-bar.png"); 33 | } 34 | 35 | public void render(Graphics g) { 36 | g.resetTransform(); 37 | g.setColor(LIFE_COLOR); 38 | g.fillRect(BAR_X, LIFE_BAR_Y, .9f * BAR_WIDTH, BAR_HEIGHT); 39 | g.setColor(MANA_COLOR); 40 | g.fillRect(BAR_X, MANA_BAR_Y, .8f * BAR_WIDTH, BAR_HEIGHT); 41 | g.setColor(XP_COLOR); 42 | g.fillRect(BAR_X, XP_BAR_Y, .2f * BAR_WIDTH, BAR_HEIGHT); 43 | g.drawImage(playerbars, P_BAR_X, P_BAR_Y); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/lesson17/MapPlayerController.java: -------------------------------------------------------------------------------- 1 | package lesson17; 2 | 3 | import org.newdawn.slick.Input; 4 | import org.newdawn.slick.KeyListener; 5 | 6 | /** 7 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 8 | * 9 | * @author Shionn, shionn@gmail.com http://shionn.org
10 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y- 11 | */ 12 | public class MapPlayerController implements KeyListener { 13 | 14 | private MapPlayer player; 15 | private Input input; 16 | 17 | public MapPlayerController(MapPlayer player) { 18 | this.player = player; 19 | } 20 | 21 | public void update() { 22 | if (input.getControllerCount() > 0) { 23 | player.setDx(input.getAxisValue(0, 1)); 24 | player.setDy(input.getAxisValue(0, 2)); 25 | } 26 | } 27 | 28 | @Override 29 | public void setInput(Input input) { 30 | this.input = input; 31 | } 32 | 33 | @Override 34 | public boolean isAcceptingInput() { 35 | return true; 36 | } 37 | 38 | @Override 39 | public void inputEnded() { 40 | 41 | } 42 | 43 | @Override 44 | public void inputStarted() { 45 | 46 | } 47 | 48 | @Override 49 | public void keyPressed(int key, char c) { 50 | switch (key) { 51 | case Input.KEY_UP: 52 | this.player.setDy(-1); 53 | break; 54 | case Input.KEY_LEFT: 55 | this.player.setDx(-1); 56 | break; 57 | case Input.KEY_DOWN: 58 | this.player.setDy(1); 59 | break; 60 | case Input.KEY_RIGHT: 61 | this.player.setDx(1); 62 | break; 63 | } 64 | } 65 | 66 | @Override 67 | public void keyReleased(int key, char c) { 68 | switch (key) { 69 | case Input.KEY_UP: 70 | case Input.KEY_DOWN: 71 | this.player.setDy(0); 72 | break; 73 | case Input.KEY_LEFT: 74 | case Input.KEY_RIGHT: 75 | this.player.setDx(0); 76 | break; 77 | } 78 | } 79 | 80 | } 81 | -------------------------------------------------------------------------------- /src/main/java/lesson17/MapTriggerController.java: -------------------------------------------------------------------------------- 1 | package lesson17; 2 | 3 | import org.newdawn.slick.SlickException; 4 | 5 | /** 6 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 7 | * 8 | * @author Shionn, shionn@gmail.com http://shionn.org
9 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y- 10 | */ 11 | public class MapTriggerController { 12 | 13 | private Map map; 14 | private MapPlayer player; 15 | 16 | public MapTriggerController(Map map, MapPlayer player) { 17 | this.map = map; 18 | this.player = player; 19 | } 20 | 21 | public void update() throws SlickException { 22 | this.player.setOnStair(false); 23 | for (int objectID = 0; objectID < this.map.getObjectCount(); objectID++) { 24 | if (isInTrigger(objectID)) { 25 | if ("teleport".equals(this.map.getObjectType(objectID))) { 26 | this.teleport(objectID); 27 | } else if ("stair".equals(this.map.getObjectType(objectID))) { 28 | this.player.setOnStair(true); 29 | } else if ("change-map".equals(this.map.getObjectType(objectID))) { 30 | this.changeMap(objectID); 31 | } 32 | } 33 | } 34 | } 35 | 36 | private boolean isInTrigger(int id) { 37 | return this.player.getX() > this.map.getObjectX(id) 38 | && this.player.getX() < this.map.getObjectX(id) + this.map.getObjectWidth(id) 39 | && this.player.getY() > this.map.getObjectY(id) 40 | && this.player.getY() < this.map.getObjectY(id) + this.map.getObjectHeight(id); 41 | } 42 | 43 | private void teleport(int objectID) { 44 | this.player.setX(Float.parseFloat(this.map.getObjectProperty(objectID, "dest-x", 45 | Float.toString(this.player.getX())))); 46 | this.player.setY(Float.parseFloat(this.map.getObjectProperty(objectID, "dest-y", 47 | Float.toString(this.player.getY())))); 48 | } 49 | 50 | private void changeMap(int objectID) throws SlickException { 51 | this.teleport(objectID); 52 | String newMap = this.map.getObjectProperty(objectID, "dest-map", "undefined"); 53 | if (!"undefined".equals(newMap)) { 54 | this.map.changeMap("map/" + newMap); 55 | } 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/lesson18/BattleCommand.java: -------------------------------------------------------------------------------- 1 | package lesson18; 2 | 3 | import org.newdawn.slick.command.Command; 4 | 5 | public enum BattleCommand implements Command { 6 | ATTACK, DEFEND, FLEE 7 | 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/lesson18/BattleEnnemy.java: -------------------------------------------------------------------------------- 1 | package lesson18; 2 | 3 | import org.newdawn.slick.Color; 4 | import org.newdawn.slick.Font; 5 | import org.newdawn.slick.GameContainer; 6 | import org.newdawn.slick.Graphics; 7 | import org.newdawn.slick.Image; 8 | import org.newdawn.slick.SlickException; 9 | import org.newdawn.slick.geom.Vector2f; 10 | 11 | import shionn.slick.animation.BezierPath; 12 | import shionn.slick.animation.PathAnimation; 13 | 14 | /** 15 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 16 | * 17 | * @author Shionn, shionn@gmail.com http://shionn.org
18 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y+ 19 | */ 20 | public class BattleEnnemy { 21 | private int pv = 12; 22 | private Image ennemy; 23 | private PathAnimation animation; 24 | 25 | public void init() throws SlickException { 26 | this.ennemy = new Image("battle/gobelin.png").getScaledCopy(2); 27 | this.animation = new PathAnimation(new BezierPath(0, 0, -400, 1, 50, 20, 0, 0), 2000); 28 | } 29 | 30 | public void reset() { 31 | this.pv = 12; 32 | } 33 | 34 | public void render(GameContainer container, Graphics g) { 35 | Vector2f p = animation.currentLocation(); 36 | this.ennemy.drawCentered(p.x + container.getWidth() * 3 / 4, p.y + container.getHeight() 37 | / 2); 38 | Font font = g.getFont(); 39 | String text = Integer.toString(pv); 40 | font.drawString(container.getWidth() * 3 / 4 - font.getWidth(text) / 2, 41 | container.getHeight() / 2 - ennemy.getHeight() / 2 - font.getLineHeight(), text, 42 | Color.white); 43 | } 44 | 45 | public void update(int delta) { 46 | animation.update(delta); 47 | } 48 | 49 | public void startAttack() { 50 | animation.start(); 51 | } 52 | 53 | public void setPv(int pv) { 54 | this.pv = pv; 55 | } 56 | 57 | public int getPv() { 58 | return pv; 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /src/main/java/lesson18/BattleGame.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 3 | * 4 | * @author Shionn, shionn@gmail.com http://shionn.org
5 | * GCS d- s+:+ a- C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- !y- 6 | */ 7 | package lesson18; 8 | 9 | import org.newdawn.slick.AppGameContainer; 10 | import org.newdawn.slick.GameContainer; 11 | import org.newdawn.slick.SlickException; 12 | import org.newdawn.slick.state.StateBasedGame; 13 | 14 | /** 15 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 16 | * 17 | * -Djava.library.path=target/natives 18 | * 19 | * @author Shionn, shionn@gmail.com http://shionn.org
20 | * GCS d- s+:+ a C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y+ 21 | */ 22 | public class BattleGame extends StateBasedGame { 23 | 24 | public static void main(String[] args) throws SlickException { 25 | AppGameContainer app = new AppGameContainer(new BattleGame(), 800, 600, false); 26 | app.setVSync(true); 27 | app.setShowFPS(false); 28 | app.start(); 29 | } 30 | 31 | public BattleGame() { 32 | super("Lesson 18 :: BattleGame"); 33 | } 34 | 35 | /** 36 | * Ici il suffit d'ajouter nos boucles de jeux. La première ajoutèe sera celle qui sera utilisée 37 | * au début 38 | */ 39 | @Override 40 | public void initStatesList(GameContainer container) throws SlickException { 41 | addState(new MainScreenGameState()); 42 | addState(new MapGameState()); 43 | addState(new BattleGameState()); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/lesson18/BattleGameState.java: -------------------------------------------------------------------------------- 1 | package lesson18; 2 | 3 | import org.newdawn.slick.GameContainer; 4 | import org.newdawn.slick.Graphics; 5 | import org.newdawn.slick.Image; 6 | import org.newdawn.slick.Input; 7 | import org.newdawn.slick.SlickException; 8 | import org.newdawn.slick.command.InputProvider; 9 | import org.newdawn.slick.command.KeyControl; 10 | import org.newdawn.slick.state.BasicGameState; 11 | import org.newdawn.slick.state.StateBasedGame; 12 | 13 | /** 14 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 15 | * 16 | * @author Shionn, shionn@gmail.com http://shionn.org
17 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y+ 18 | */ 19 | public class BattleGameState extends BasicGameState { 20 | 21 | public static final int ID = 3; 22 | private Image background; 23 | private BattleEnnemy ennemy = new BattleEnnemy(); 24 | private BattlePlayer player = new BattlePlayer(); 25 | 26 | @Override 27 | public void init(GameContainer container, StateBasedGame game) throws SlickException { 28 | this.background = new Image("background/battle.png"); 29 | this.ennemy.init(); 30 | this.player.init(); 31 | 32 | InputProvider provider = new InputProvider(container.getInput()); 33 | provider.bindCommand(new KeyControl(Input.KEY_A), BattleCommand.ATTACK); 34 | provider.bindCommand(new KeyControl(Input.KEY_D), BattleCommand.DEFEND); 35 | provider.bindCommand(new KeyControl(Input.KEY_F), BattleCommand.FLEE); 36 | provider.addListener(new BattleController(player, ennemy, game)); 37 | } 38 | 39 | @Override 40 | public void enter(GameContainer container, StateBasedGame game) throws SlickException { 41 | this.ennemy.reset(); 42 | } 43 | 44 | @Override 45 | public void render(GameContainer container, StateBasedGame game, Graphics g) 46 | throws SlickException { 47 | this.background.draw(0, 0, container.getWidth(), container.getHeight()); 48 | this.player.render(container, g); 49 | this.ennemy.render(container, g); 50 | } 51 | 52 | @Override 53 | public void update(GameContainer container, StateBasedGame game, int delta) 54 | throws SlickException { 55 | this.player.update(delta); 56 | this.ennemy.update(delta); 57 | } 58 | 59 | @Override 60 | public int getID() { 61 | return ID; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/lesson18/BattlePlayer.java: -------------------------------------------------------------------------------- 1 | package lesson18; 2 | 3 | import org.newdawn.slick.Color; 4 | import org.newdawn.slick.Font; 5 | import org.newdawn.slick.GameContainer; 6 | import org.newdawn.slick.Graphics; 7 | import org.newdawn.slick.Image; 8 | import org.newdawn.slick.SlickException; 9 | import org.newdawn.slick.geom.Vector2f; 10 | 11 | import shionn.slick.animation.BezierPath; 12 | import shionn.slick.animation.PathAnimation; 13 | 14 | /** 15 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 16 | * 17 | * @author Shionn, shionn@gmail.com http://shionn.org
18 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y+ 19 | */ 20 | public class BattlePlayer { 21 | 22 | private int pv = 50; 23 | private Image hero; 24 | private PathAnimation animation; 25 | 26 | public void init() throws SlickException { 27 | this.hero = new Image("battle/hero.png").getScaledCopy(2); 28 | this.animation = new PathAnimation(new BezierPath(0, 0, 400, 1, -50, 20, 0, 0), 2000); 29 | } 30 | 31 | public void render(GameContainer container, Graphics g) { 32 | Vector2f p = animation.currentLocation(); 33 | hero.drawCentered(p.x + container.getWidth() * 1 / 4, p.y + container.getHeight() / 2); 34 | Font font = g.getFont(); 35 | String text = Integer.toString(pv); 36 | font.drawString(container.getWidth() * 1 / 4 - font.getWidth(text) / 2, 37 | container.getHeight() / 2 - hero.getHeight() / 2 - font.getLineHeight(), text, 38 | Color.white); 39 | } 40 | 41 | public void update(int delta) { 42 | animation.update(delta); 43 | } 44 | 45 | public void startAttack() { 46 | animation.start(); 47 | } 48 | 49 | public int getPv() { 50 | return pv; 51 | } 52 | 53 | public void setPv(int pv) { 54 | this.pv = pv; 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/lesson18/MainScreenGameState.java: -------------------------------------------------------------------------------- 1 | package lesson18; 2 | 3 | import org.newdawn.slick.GameContainer; 4 | import org.newdawn.slick.Graphics; 5 | import org.newdawn.slick.Image; 6 | import org.newdawn.slick.SlickException; 7 | import org.newdawn.slick.state.BasicGameState; 8 | import org.newdawn.slick.state.StateBasedGame; 9 | 10 | /** 11 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 12 | * 13 | * @author Shionn, shionn@gmail.com http://shionn.org
14 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y+ 15 | */ 16 | public class MainScreenGameState extends BasicGameState { 17 | 18 | public static final int ID = 1; 19 | private Image background; 20 | private StateBasedGame game; 21 | 22 | @Override 23 | public void init(GameContainer container, StateBasedGame game) throws SlickException { 24 | this.game = game; 25 | this.background = new Image("background/forest.png"); 26 | } 27 | 28 | /** 29 | * Contenons nous d'afficher l'image de fond. . 30 | */ 31 | @Override 32 | public void render(GameContainer container, StateBasedGame game, Graphics g) 33 | throws SlickException { 34 | background.draw(0, 0, container.getWidth(), container.getHeight()); 35 | g.drawString("Appuyer sur une touche", 300, 300); 36 | } 37 | 38 | @Override 39 | public void update(GameContainer container, StateBasedGame game, int delta) 40 | throws SlickException { 41 | } 42 | 43 | /** 44 | * Passer à l'ecran de jeu à l'appui de n'importe quel touche. 45 | */ 46 | @Override 47 | public void keyReleased(int key, char c) { 48 | game.enterState(MapGameState.ID); 49 | } 50 | 51 | /** 52 | * L'identifiant permet d'identifier les différentes boucles, pour passer de l'une à l'autre. 53 | */ 54 | @Override 55 | public int getID() { 56 | return ID; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/lesson18/Map.java: -------------------------------------------------------------------------------- 1 | package lesson18; 2 | 3 | import org.newdawn.slick.Color; 4 | import org.newdawn.slick.Image; 5 | import org.newdawn.slick.SlickException; 6 | import org.newdawn.slick.tiled.TiledMap; 7 | 8 | /** 9 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 10 | * 11 | * @author Shionn, shionn@gmail.com http://shionn.org
12 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y- 13 | */ 14 | public class Map { 15 | private TiledMap tiledMap; 16 | 17 | public void init() throws SlickException { 18 | this.tiledMap = new TiledMap("map/exemple-change-map.tmx"); 19 | } 20 | 21 | public void renderBackground() { 22 | this.tiledMap.render(0, 0, 0); 23 | this.tiledMap.render(0, 0, 1); 24 | this.tiledMap.render(0, 0, 2); 25 | } 26 | 27 | public void renderForeground() { 28 | this.tiledMap.render(0, 0, 3); 29 | this.tiledMap.render(0, 0, 4); 30 | } 31 | 32 | public boolean isCollision(float x, float y) { 33 | int tileW = this.tiledMap.getTileWidth(); 34 | int tileH = this.tiledMap.getTileHeight(); 35 | int logicLayer = this.tiledMap.getLayerIndex("logic"); 36 | Image tile = this.tiledMap.getTileImage((int) x / tileW, (int) y / tileH, logicLayer); 37 | boolean collision = tile != null; 38 | if (collision) { 39 | Color color = tile.getColor((int) x % tileW, (int) y % tileH); 40 | collision = color.getAlpha() > 0; 41 | } 42 | return collision; 43 | } 44 | 45 | public void changeMap(String file) throws SlickException { 46 | this.tiledMap = new TiledMap(file); 47 | } 48 | 49 | public int getObjectCount() { 50 | return this.tiledMap.getObjectCount(0); 51 | } 52 | 53 | public String getObjectType(int objectID) { 54 | return this.tiledMap.getObjectType(0, objectID); 55 | } 56 | 57 | public float getObjectX(int objectID) { 58 | return this.tiledMap.getObjectX(0, objectID); 59 | } 60 | 61 | public float getObjectY(int objectID) { 62 | return this.tiledMap.getObjectY(0, objectID); 63 | } 64 | 65 | public float getObjectWidth(int objectID) { 66 | return this.tiledMap.getObjectWidth(0, objectID); 67 | } 68 | 69 | public float getObjectHeight(int objectID) { 70 | return this.tiledMap.getObjectHeight(0, objectID); 71 | } 72 | 73 | public String getObjectProperty(int objectID, String propertyName, String def) { 74 | return this.tiledMap.getObjectProperty(0, objectID, propertyName, def); 75 | } 76 | 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/lesson18/MapCamera.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package lesson18; 5 | 6 | import org.newdawn.slick.GameContainer; 7 | import org.newdawn.slick.Graphics; 8 | 9 | /** 10 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 11 | * 12 | * @author Shionn, shionn@gmail.com http://shionn.org
13 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y- 14 | */ 15 | public class MapCamera { 16 | 17 | private MapPlayer player; 18 | private float xCamera, yCamera; 19 | 20 | public MapCamera(MapPlayer player) { 21 | this.player = player; 22 | this.xCamera = player.getX(); 23 | this.yCamera = player.getY(); 24 | } 25 | 26 | public void place(GameContainer container, Graphics g) { 27 | g.translate(container.getWidth() / 2 - (int) this.xCamera, container.getHeight() / 2 28 | - (int) this.yCamera); 29 | } 30 | 31 | public void update(GameContainer container) { 32 | int w = container.getWidth() / 4; 33 | if (this.player.getX() > this.xCamera + w) { 34 | this.xCamera = this.player.getX() - w; 35 | } else if (this.player.getX() < this.xCamera - w) { 36 | this.xCamera = this.player.getX() + w; 37 | } 38 | int h = container.getHeight() / 4; 39 | if (this.player.getY() > this.yCamera + h) { 40 | this.yCamera = this.player.getY() - h; 41 | } else if (this.player.getY() < this.yCamera - h) { 42 | this.yCamera = this.player.getY() + h; 43 | } 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/lesson18/MapGameState.java: -------------------------------------------------------------------------------- 1 | package lesson18; 2 | 3 | import org.newdawn.slick.GameContainer; 4 | import org.newdawn.slick.Graphics; 5 | import org.newdawn.slick.Input; 6 | import org.newdawn.slick.Music; 7 | import org.newdawn.slick.SlickException; 8 | import org.newdawn.slick.state.BasicGameState; 9 | import org.newdawn.slick.state.StateBasedGame; 10 | 11 | /** 12 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 13 | * 14 | * @author Shionn, shionn@gmail.com http://shionn.org
15 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y- 16 | */ 17 | public class MapGameState extends BasicGameState { 18 | public static final int ID = 2; 19 | 20 | private GameContainer container; 21 | private Map map = new Map(); 22 | private MapPlayer player = new MapPlayer(map); 23 | private MapTriggerController triggers = new MapTriggerController(map, player); 24 | private MapCamera camera = new MapCamera(player); 25 | private MapPlayerController controller = new MapPlayerController(player); 26 | private MapHud hud = new MapHud(); 27 | 28 | @Override 29 | public void init(GameContainer container, StateBasedGame game) throws SlickException { 30 | this.container = container; 31 | Music background = new Music("sound/lost-in-the-meadows.ogg"); 32 | background.loop(); 33 | this.map.init(); 34 | this.player.init(); 35 | this.hud.init(); 36 | this.controller.setInput(container.getInput()); 37 | container.getInput().addKeyListener(controller); 38 | } 39 | 40 | @Override 41 | public void render(GameContainer container, StateBasedGame game, Graphics g) 42 | throws SlickException { 43 | this.camera.place(container, g); 44 | this.map.renderBackground(); 45 | this.player.render(g); 46 | this.map.renderForeground(); 47 | this.hud.render(g); 48 | } 49 | 50 | @Override 51 | public void update(GameContainer container, StateBasedGame game, int delta) 52 | throws SlickException { 53 | this.controller.update(); 54 | this.triggers.update(); 55 | this.player.update(delta); 56 | this.camera.update(container); 57 | if (Math.random() < 0.005 && player.isMoving()) { 58 | game.enterState(BattleGameState.ID); 59 | } 60 | } 61 | 62 | @Override 63 | public void keyReleased(int key, char c) { 64 | if (Input.KEY_ESCAPE == key) { 65 | this.container.exit(); 66 | } 67 | } 68 | 69 | @Override 70 | public int getID() { 71 | return ID; 72 | } 73 | 74 | } 75 | -------------------------------------------------------------------------------- /src/main/java/lesson18/MapHud.java: -------------------------------------------------------------------------------- 1 | package lesson18; 2 | 3 | import org.newdawn.slick.Color; 4 | import org.newdawn.slick.Graphics; 5 | import org.newdawn.slick.Image; 6 | import org.newdawn.slick.SlickException; 7 | 8 | /** 9 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 10 | * 11 | * @author Shionn, shionn@gmail.com http://shionn.org
12 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y- 13 | */ 14 | public class MapHud { 15 | 16 | private static final int P_BAR_X = 10; 17 | private static final int P_BAR_Y = 10; 18 | private static final int BAR_X = 84 + P_BAR_X; 19 | private static final int LIFE_BAR_Y = 4 + P_BAR_Y; 20 | private static final int MANA_BAR_Y = 24 + P_BAR_Y; 21 | private static final int XP_BAR_Y = 44 + P_BAR_Y; 22 | private static final int BAR_WIDTH = 80; 23 | private static final int BAR_HEIGHT = 16; 24 | 25 | private static final Color LIFE_COLOR = new Color(255, 0, 0); 26 | private static final Color MANA_COLOR = new Color(0, 0, 255); 27 | private static final Color XP_COLOR = new Color(0, 255, 0); 28 | 29 | private Image playerbars; 30 | 31 | public void init() throws SlickException { 32 | this.playerbars = new Image("hud/player-bar.png"); 33 | } 34 | 35 | public void render(Graphics g) { 36 | g.resetTransform(); 37 | g.setColor(LIFE_COLOR); 38 | g.fillRect(BAR_X, LIFE_BAR_Y, .9f * BAR_WIDTH, BAR_HEIGHT); 39 | g.setColor(MANA_COLOR); 40 | g.fillRect(BAR_X, MANA_BAR_Y, .8f * BAR_WIDTH, BAR_HEIGHT); 41 | g.setColor(XP_COLOR); 42 | g.fillRect(BAR_X, XP_BAR_Y, .2f * BAR_WIDTH, BAR_HEIGHT); 43 | g.drawImage(playerbars, P_BAR_X, P_BAR_Y); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/lesson18/MapPlayerController.java: -------------------------------------------------------------------------------- 1 | package lesson18; 2 | 3 | import org.newdawn.slick.Input; 4 | import org.newdawn.slick.KeyListener; 5 | 6 | /** 7 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 8 | * 9 | * @author Shionn, shionn@gmail.com http://shionn.org
10 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y- 11 | */ 12 | public class MapPlayerController implements KeyListener { 13 | 14 | private MapPlayer player; 15 | private Input input; 16 | 17 | public MapPlayerController(MapPlayer player) { 18 | this.player = player; 19 | } 20 | 21 | public void update() { 22 | if (input.getControllerCount() > 0) { 23 | player.setDx(input.getAxisValue(0, 1)); 24 | player.setDy(input.getAxisValue(0, 2)); 25 | } 26 | } 27 | 28 | @Override 29 | public void setInput(Input input) { 30 | this.input = input; 31 | } 32 | 33 | @Override 34 | public boolean isAcceptingInput() { 35 | return true; 36 | } 37 | 38 | @Override 39 | public void inputEnded() { 40 | 41 | } 42 | 43 | @Override 44 | public void inputStarted() { 45 | 46 | } 47 | 48 | @Override 49 | public void keyPressed(int key, char c) { 50 | switch (key) { 51 | case Input.KEY_UP: 52 | this.player.setDy(-1); 53 | break; 54 | case Input.KEY_LEFT: 55 | this.player.setDx(-1); 56 | break; 57 | case Input.KEY_DOWN: 58 | this.player.setDy(1); 59 | break; 60 | case Input.KEY_RIGHT: 61 | this.player.setDx(1); 62 | break; 63 | } 64 | } 65 | 66 | @Override 67 | public void keyReleased(int key, char c) { 68 | switch (key) { 69 | case Input.KEY_UP: 70 | case Input.KEY_DOWN: 71 | this.player.setDy(0); 72 | break; 73 | case Input.KEY_LEFT: 74 | case Input.KEY_RIGHT: 75 | this.player.setDx(0); 76 | break; 77 | } 78 | } 79 | 80 | } 81 | -------------------------------------------------------------------------------- /src/main/java/lesson18/MapTriggerController.java: -------------------------------------------------------------------------------- 1 | package lesson18; 2 | 3 | import org.newdawn.slick.SlickException; 4 | 5 | /** 6 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 7 | * 8 | * @author Shionn, shionn@gmail.com http://shionn.org
9 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y- 10 | */ 11 | public class MapTriggerController { 12 | 13 | private Map map; 14 | private MapPlayer player; 15 | 16 | public MapTriggerController(Map map, MapPlayer player) { 17 | this.map = map; 18 | this.player = player; 19 | } 20 | 21 | public void update() throws SlickException { 22 | this.player.setOnStair(false); 23 | for (int objectID = 0; objectID < this.map.getObjectCount(); objectID++) { 24 | if (isInTrigger(objectID)) { 25 | if ("teleport".equals(this.map.getObjectType(objectID))) { 26 | this.teleport(objectID); 27 | } else if ("stair".equals(this.map.getObjectType(objectID))) { 28 | this.player.setOnStair(true); 29 | } else if ("change-map".equals(this.map.getObjectType(objectID))) { 30 | this.changeMap(objectID); 31 | } 32 | } 33 | } 34 | } 35 | 36 | private boolean isInTrigger(int id) { 37 | return this.player.getX() > this.map.getObjectX(id) 38 | && this.player.getX() < this.map.getObjectX(id) + this.map.getObjectWidth(id) 39 | && this.player.getY() > this.map.getObjectY(id) 40 | && this.player.getY() < this.map.getObjectY(id) + this.map.getObjectHeight(id); 41 | } 42 | 43 | private void teleport(int objectID) { 44 | this.player.setX(Float.parseFloat(this.map.getObjectProperty(objectID, "dest-x", 45 | Float.toString(this.player.getX())))); 46 | this.player.setY(Float.parseFloat(this.map.getObjectProperty(objectID, "dest-y", 47 | Float.toString(this.player.getY())))); 48 | } 49 | 50 | private void changeMap(int objectID) throws SlickException { 51 | this.teleport(objectID); 52 | String newMap = this.map.getObjectProperty(objectID, "dest-map", "undefined"); 53 | if (!"undefined".equals(newMap)) { 54 | this.map.changeMap("map/" + newMap); 55 | } 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/lesson19/BattleCommand.java: -------------------------------------------------------------------------------- 1 | package lesson19; 2 | 3 | import org.newdawn.slick.command.Command; 4 | 5 | public enum BattleCommand implements Command { 6 | ATTACK, DEFEND, FLEE, NONE 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/lesson19/BattleEnnemy.java: -------------------------------------------------------------------------------- 1 | package lesson19; 2 | 3 | import org.newdawn.slick.Color; 4 | import org.newdawn.slick.Font; 5 | import org.newdawn.slick.GameContainer; 6 | import org.newdawn.slick.Graphics; 7 | import org.newdawn.slick.Image; 8 | import org.newdawn.slick.SlickException; 9 | import org.newdawn.slick.geom.Vector2f; 10 | 11 | import shionn.slick.animation.AnimationListener; 12 | import shionn.slick.animation.BezierPath; 13 | import shionn.slick.animation.PathAnimation; 14 | 15 | /** 16 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 17 | * 18 | * @author Shionn, shionn@gmail.com http://shionn.org
19 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y+ 20 | */ 21 | public class BattleEnnemy { 22 | private int pv = 12; 23 | private Image ennemy; 24 | private PathAnimation animation; 25 | 26 | public void init() throws SlickException { 27 | this.ennemy = new Image("battle/gobelin.png").getScaledCopy(2); 28 | this.animation = new PathAnimation(new BezierPath(0, 0, -400, 1, 50, 20, 0, 0), 2000); 29 | } 30 | 31 | public void addAnimationListener(AnimationListener assignDamage, AnimationListener endAttack) { 32 | this.animation.addListener(1000, assignDamage); 33 | this.animation.addListener(2000, endAttack); 34 | } 35 | 36 | public void reset() { 37 | this.pv = 12; 38 | } 39 | 40 | public void render(GameContainer container, Graphics g) { 41 | Vector2f p = animation.currentLocation(); 42 | this.ennemy.drawCentered(p.x + container.getWidth() * 3 / 4, p.y + container.getHeight() 43 | / 2); 44 | Font font = g.getFont(); 45 | String text = Integer.toString(pv); 46 | font.drawString(container.getWidth() * 3 / 4 - font.getWidth(text) / 2, 47 | container.getHeight() / 2 - ennemy.getHeight() / 2 - font.getLineHeight(), text, 48 | Color.white); 49 | } 50 | 51 | public void update(int delta) { 52 | animation.update(delta); 53 | } 54 | 55 | public void startAttack() { 56 | animation.start(); 57 | } 58 | 59 | public void setPv(int pv) { 60 | this.pv = pv; 61 | } 62 | 63 | public int getPv() { 64 | return pv; 65 | } 66 | 67 | } 68 | -------------------------------------------------------------------------------- /src/main/java/lesson19/BattleGame.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 3 | * 4 | * @author Shionn, shionn@gmail.com http://shionn.org
5 | * GCS d- s+:+ a- C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- !y- 6 | */ 7 | package lesson19; 8 | 9 | import org.newdawn.slick.AppGameContainer; 10 | import org.newdawn.slick.GameContainer; 11 | import org.newdawn.slick.SlickException; 12 | import org.newdawn.slick.state.StateBasedGame; 13 | 14 | /** 15 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 16 | * 17 | * -Djava.library.path=target/natives 18 | * 19 | * @author Shionn, shionn@gmail.com http://shionn.org
20 | * GCS d- s+:+ a C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y+ 21 | */ 22 | public class BattleGame extends StateBasedGame { 23 | 24 | public static void main(String[] args) throws SlickException { 25 | AppGameContainer app = new AppGameContainer(new BattleGame(), 800, 600, false); 26 | app.setVSync(true); 27 | app.setShowFPS(false); 28 | app.start(); 29 | } 30 | 31 | public BattleGame() { 32 | super("Lesson 19 :: BattleGame"); 33 | } 34 | 35 | /** 36 | * Ici il suffit d'ajouter nos boucles de jeux. La première ajoutèe sera celle qui sera utilisée 37 | * au début 38 | */ 39 | @Override 40 | public void initStatesList(GameContainer container) throws SlickException { 41 | addState(new MainScreenGameState()); 42 | addState(new MapGameState()); 43 | addState(new BattleGameState()); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/lesson19/BattleGameState.java: -------------------------------------------------------------------------------- 1 | package lesson19; 2 | 3 | import org.newdawn.slick.GameContainer; 4 | import org.newdawn.slick.Graphics; 5 | import org.newdawn.slick.Image; 6 | import org.newdawn.slick.Input; 7 | import org.newdawn.slick.SlickException; 8 | import org.newdawn.slick.command.InputProvider; 9 | import org.newdawn.slick.command.KeyControl; 10 | import org.newdawn.slick.state.BasicGameState; 11 | import org.newdawn.slick.state.StateBasedGame; 12 | 13 | /** 14 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 15 | * 16 | * @author Shionn, shionn@gmail.com http://shionn.org
17 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y+ 18 | */ 19 | public class BattleGameState extends BasicGameState { 20 | 21 | public static final int ID = 3; 22 | private Image background; 23 | private BattleEnnemy ennemy = new BattleEnnemy(); 24 | private BattlePlayer player = new BattlePlayer(); 25 | 26 | @Override 27 | public void init(GameContainer container, StateBasedGame game) throws SlickException { 28 | this.background = new Image("background/battle.png"); 29 | this.ennemy.init(); 30 | this.player.init(); 31 | 32 | InputProvider provider = new InputProvider(container.getInput()); 33 | provider.bindCommand(new KeyControl(Input.KEY_A), BattleCommand.ATTACK); 34 | provider.bindCommand(new KeyControl(Input.KEY_D), BattleCommand.DEFEND); 35 | provider.bindCommand(new KeyControl(Input.KEY_F), BattleCommand.FLEE); 36 | provider.addListener(new BattleController(player, ennemy, game)); 37 | } 38 | 39 | @Override 40 | public void enter(GameContainer container, StateBasedGame game) throws SlickException { 41 | this.ennemy.reset(); 42 | } 43 | 44 | @Override 45 | public void render(GameContainer container, StateBasedGame game, Graphics g) 46 | throws SlickException { 47 | this.background.draw(0, 0, container.getWidth(), container.getHeight()); 48 | this.player.render(container, g); 49 | this.ennemy.render(container, g); 50 | } 51 | 52 | @Override 53 | public void update(GameContainer container, StateBasedGame game, int delta) 54 | throws SlickException { 55 | this.player.update(delta); 56 | this.ennemy.update(delta); 57 | } 58 | 59 | @Override 60 | public int getID() { 61 | return ID; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/lesson19/BattlePlayer.java: -------------------------------------------------------------------------------- 1 | package lesson19; 2 | 3 | import org.newdawn.slick.Color; 4 | import org.newdawn.slick.Font; 5 | import org.newdawn.slick.GameContainer; 6 | import org.newdawn.slick.Graphics; 7 | import org.newdawn.slick.Image; 8 | import org.newdawn.slick.SlickException; 9 | import org.newdawn.slick.geom.Vector2f; 10 | 11 | import shionn.slick.animation.AnimationListener; 12 | import shionn.slick.animation.BezierPath; 13 | import shionn.slick.animation.PathAnimation; 14 | 15 | /** 16 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 17 | * 18 | * @author Shionn, shionn@gmail.com http://shionn.org
19 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y+ 20 | */ 21 | public class BattlePlayer { 22 | 23 | private int pv = 50; 24 | private Image hero; 25 | private PathAnimation animation; 26 | 27 | public void init() throws SlickException { 28 | this.hero = new Image("battle/hero.png").getScaledCopy(2); 29 | this.animation = new PathAnimation(new BezierPath(0, 0, 400, 1, -50, 20, 0, 0), 2000); 30 | } 31 | 32 | public void addAnimationListener(AnimationListener assignDamage, AnimationListener endAttack) { 33 | this.animation.addListener(1000, assignDamage); 34 | this.animation.addListener(2000, endAttack); 35 | } 36 | 37 | public void render(GameContainer container, Graphics g) { 38 | Vector2f p = animation.currentLocation(); 39 | hero.drawCentered(p.x + container.getWidth() * 1 / 4, p.y + container.getHeight() / 2); 40 | Font font = g.getFont(); 41 | String text = Integer.toString(pv); 42 | font.drawString(container.getWidth() * 1 / 4 - font.getWidth(text) / 2, 43 | container.getHeight() / 2 - hero.getHeight() / 2 - font.getLineHeight(), text, 44 | Color.white); 45 | } 46 | 47 | public void update(int delta) { 48 | animation.update(delta); 49 | } 50 | 51 | public void startAttack() { 52 | animation.start(); 53 | } 54 | 55 | public int getPv() { 56 | return pv; 57 | } 58 | 59 | public void setPv(int pv) { 60 | this.pv = pv; 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/lesson19/MainScreenGameState.java: -------------------------------------------------------------------------------- 1 | package lesson19; 2 | 3 | import org.newdawn.slick.GameContainer; 4 | import org.newdawn.slick.Graphics; 5 | import org.newdawn.slick.Image; 6 | import org.newdawn.slick.SlickException; 7 | import org.newdawn.slick.state.BasicGameState; 8 | import org.newdawn.slick.state.StateBasedGame; 9 | 10 | /** 11 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 12 | * 13 | * @author Shionn, shionn@gmail.com http://shionn.org
14 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y+ 15 | */ 16 | public class MainScreenGameState extends BasicGameState { 17 | 18 | public static final int ID = 1; 19 | private Image background; 20 | private StateBasedGame game; 21 | 22 | @Override 23 | public void init(GameContainer container, StateBasedGame game) throws SlickException { 24 | this.game = game; 25 | this.background = new Image("background/forest.png"); 26 | } 27 | 28 | /** 29 | * Contenons nous d'afficher l'image de fond. . 30 | */ 31 | @Override 32 | public void render(GameContainer container, StateBasedGame game, Graphics g) 33 | throws SlickException { 34 | background.draw(0, 0, container.getWidth(), container.getHeight()); 35 | g.drawString("Appuyer sur une touche", 300, 300); 36 | } 37 | 38 | @Override 39 | public void update(GameContainer container, StateBasedGame game, int delta) 40 | throws SlickException { 41 | } 42 | 43 | /** 44 | * Passer à l'ecran de jeu à l'appui de n'importe quel touche. 45 | */ 46 | @Override 47 | public void keyReleased(int key, char c) { 48 | game.enterState(MapGameState.ID); 49 | } 50 | 51 | /** 52 | * L'identifiant permet d'identifier les différentes boucles, pour passer de l'une à l'autre. 53 | */ 54 | @Override 55 | public int getID() { 56 | return ID; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/lesson19/Map.java: -------------------------------------------------------------------------------- 1 | package lesson19; 2 | 3 | import org.newdawn.slick.Color; 4 | import org.newdawn.slick.Image; 5 | import org.newdawn.slick.SlickException; 6 | import org.newdawn.slick.tiled.TiledMap; 7 | 8 | /** 9 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 10 | * 11 | * @author Shionn, shionn@gmail.com http://shionn.org
12 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y- 13 | */ 14 | public class Map { 15 | private TiledMap tiledMap; 16 | 17 | public void init() throws SlickException { 18 | this.tiledMap = new TiledMap("map/exemple-change-map.tmx"); 19 | } 20 | 21 | public void renderBackground() { 22 | this.tiledMap.render(0, 0, 0); 23 | this.tiledMap.render(0, 0, 1); 24 | this.tiledMap.render(0, 0, 2); 25 | } 26 | 27 | public void renderForeground() { 28 | this.tiledMap.render(0, 0, 3); 29 | this.tiledMap.render(0, 0, 4); 30 | } 31 | 32 | public boolean isCollision(float x, float y) { 33 | int tileW = this.tiledMap.getTileWidth(); 34 | int tileH = this.tiledMap.getTileHeight(); 35 | int logicLayer = this.tiledMap.getLayerIndex("logic"); 36 | Image tile = this.tiledMap.getTileImage((int) x / tileW, (int) y / tileH, logicLayer); 37 | boolean collision = tile != null; 38 | if (collision) { 39 | Color color = tile.getColor((int) x % tileW, (int) y % tileH); 40 | collision = color.getAlpha() > 0; 41 | } 42 | return collision; 43 | } 44 | 45 | public void changeMap(String file) throws SlickException { 46 | this.tiledMap = new TiledMap(file); 47 | } 48 | 49 | public int getObjectCount() { 50 | return this.tiledMap.getObjectCount(0); 51 | } 52 | 53 | public String getObjectType(int objectID) { 54 | return this.tiledMap.getObjectType(0, objectID); 55 | } 56 | 57 | public float getObjectX(int objectID) { 58 | return this.tiledMap.getObjectX(0, objectID); 59 | } 60 | 61 | public float getObjectY(int objectID) { 62 | return this.tiledMap.getObjectY(0, objectID); 63 | } 64 | 65 | public float getObjectWidth(int objectID) { 66 | return this.tiledMap.getObjectWidth(0, objectID); 67 | } 68 | 69 | public float getObjectHeight(int objectID) { 70 | return this.tiledMap.getObjectHeight(0, objectID); 71 | } 72 | 73 | public String getObjectProperty(int objectID, String propertyName, String def) { 74 | return this.tiledMap.getObjectProperty(0, objectID, propertyName, def); 75 | } 76 | 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/lesson19/MapCamera.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package lesson19; 5 | 6 | import org.newdawn.slick.GameContainer; 7 | import org.newdawn.slick.Graphics; 8 | 9 | /** 10 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 11 | * 12 | * @author Shionn, shionn@gmail.com http://shionn.org
13 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y- 14 | */ 15 | public class MapCamera { 16 | 17 | private MapPlayer player; 18 | private float xCamera, yCamera; 19 | 20 | public MapCamera(MapPlayer player) { 21 | this.player = player; 22 | this.xCamera = player.getX(); 23 | this.yCamera = player.getY(); 24 | } 25 | 26 | public void place(GameContainer container, Graphics g) { 27 | g.translate(container.getWidth() / 2 - (int) this.xCamera, container.getHeight() / 2 28 | - (int) this.yCamera); 29 | } 30 | 31 | public void update(GameContainer container) { 32 | int w = container.getWidth() / 4; 33 | if (this.player.getX() > this.xCamera + w) { 34 | this.xCamera = this.player.getX() - w; 35 | } else if (this.player.getX() < this.xCamera - w) { 36 | this.xCamera = this.player.getX() + w; 37 | } 38 | int h = container.getHeight() / 4; 39 | if (this.player.getY() > this.yCamera + h) { 40 | this.yCamera = this.player.getY() - h; 41 | } else if (this.player.getY() < this.yCamera - h) { 42 | this.yCamera = this.player.getY() + h; 43 | } 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/lesson19/MapGameState.java: -------------------------------------------------------------------------------- 1 | package lesson19; 2 | 3 | import org.newdawn.slick.GameContainer; 4 | import org.newdawn.slick.Graphics; 5 | import org.newdawn.slick.Input; 6 | import org.newdawn.slick.Music; 7 | import org.newdawn.slick.SlickException; 8 | import org.newdawn.slick.state.BasicGameState; 9 | import org.newdawn.slick.state.StateBasedGame; 10 | 11 | /** 12 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 13 | * 14 | * @author Shionn, shionn@gmail.com http://shionn.org
15 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y- 16 | */ 17 | public class MapGameState extends BasicGameState { 18 | public static final int ID = 2; 19 | 20 | private GameContainer container; 21 | private Map map = new Map(); 22 | private MapPlayer player = new MapPlayer(map); 23 | private MapTriggerController triggers = new MapTriggerController(map, player); 24 | private MapCamera camera = new MapCamera(player); 25 | private MapPlayerController controller = new MapPlayerController(player); 26 | private MapHud hud = new MapHud(); 27 | 28 | @Override 29 | public void init(GameContainer container, StateBasedGame game) throws SlickException { 30 | this.container = container; 31 | Music background = new Music("sound/lost-in-the-meadows.ogg"); 32 | background.loop(); 33 | this.map.init(); 34 | this.player.init(); 35 | this.hud.init(); 36 | this.controller.setInput(container.getInput()); 37 | container.getInput().addKeyListener(controller); 38 | } 39 | 40 | @Override 41 | public void render(GameContainer container, StateBasedGame game, Graphics g) 42 | throws SlickException { 43 | this.camera.place(container, g); 44 | this.map.renderBackground(); 45 | this.player.render(g); 46 | this.map.renderForeground(); 47 | this.hud.render(g); 48 | } 49 | 50 | @Override 51 | public void update(GameContainer container, StateBasedGame game, int delta) 52 | throws SlickException { 53 | this.controller.update(); 54 | this.triggers.update(); 55 | this.player.update(delta); 56 | this.camera.update(container); 57 | if (Math.random() < 0.005 && player.isMoving()) { 58 | game.enterState(BattleGameState.ID); 59 | } 60 | } 61 | 62 | @Override 63 | public void keyReleased(int key, char c) { 64 | if (Input.KEY_ESCAPE == key) { 65 | this.container.exit(); 66 | } 67 | } 68 | 69 | @Override 70 | public int getID() { 71 | return ID; 72 | } 73 | 74 | } 75 | -------------------------------------------------------------------------------- /src/main/java/lesson19/MapHud.java: -------------------------------------------------------------------------------- 1 | package lesson19; 2 | 3 | import org.newdawn.slick.Color; 4 | import org.newdawn.slick.Graphics; 5 | import org.newdawn.slick.Image; 6 | import org.newdawn.slick.SlickException; 7 | 8 | /** 9 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 10 | * 11 | * @author Shionn, shionn@gmail.com http://shionn.org
12 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y- 13 | */ 14 | public class MapHud { 15 | 16 | private static final int P_BAR_X = 10; 17 | private static final int P_BAR_Y = 10; 18 | private static final int BAR_X = 84 + P_BAR_X; 19 | private static final int LIFE_BAR_Y = 4 + P_BAR_Y; 20 | private static final int MANA_BAR_Y = 24 + P_BAR_Y; 21 | private static final int XP_BAR_Y = 44 + P_BAR_Y; 22 | private static final int BAR_WIDTH = 80; 23 | private static final int BAR_HEIGHT = 16; 24 | 25 | private static final Color LIFE_COLOR = new Color(255, 0, 0); 26 | private static final Color MANA_COLOR = new Color(0, 0, 255); 27 | private static final Color XP_COLOR = new Color(0, 255, 0); 28 | 29 | private Image playerbars; 30 | 31 | public void init() throws SlickException { 32 | this.playerbars = new Image("hud/player-bar.png"); 33 | } 34 | 35 | public void render(Graphics g) { 36 | g.resetTransform(); 37 | g.setColor(LIFE_COLOR); 38 | g.fillRect(BAR_X, LIFE_BAR_Y, .9f * BAR_WIDTH, BAR_HEIGHT); 39 | g.setColor(MANA_COLOR); 40 | g.fillRect(BAR_X, MANA_BAR_Y, .8f * BAR_WIDTH, BAR_HEIGHT); 41 | g.setColor(XP_COLOR); 42 | g.fillRect(BAR_X, XP_BAR_Y, .2f * BAR_WIDTH, BAR_HEIGHT); 43 | g.drawImage(playerbars, P_BAR_X, P_BAR_Y); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/lesson19/MapPlayerController.java: -------------------------------------------------------------------------------- 1 | package lesson19; 2 | 3 | import org.newdawn.slick.Input; 4 | import org.newdawn.slick.KeyListener; 5 | 6 | /** 7 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 8 | * 9 | * @author Shionn, shionn@gmail.com http://shionn.org
10 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y- 11 | */ 12 | public class MapPlayerController implements KeyListener { 13 | 14 | private MapPlayer player; 15 | private Input input; 16 | 17 | public MapPlayerController(MapPlayer player) { 18 | this.player = player; 19 | } 20 | 21 | public void update() { 22 | if (input.getControllerCount() > 0) { 23 | player.setDx(input.getAxisValue(0, 1)); 24 | player.setDy(input.getAxisValue(0, 2)); 25 | } 26 | } 27 | 28 | @Override 29 | public void setInput(Input input) { 30 | this.input = input; 31 | } 32 | 33 | @Override 34 | public boolean isAcceptingInput() { 35 | return true; 36 | } 37 | 38 | @Override 39 | public void inputEnded() { 40 | 41 | } 42 | 43 | @Override 44 | public void inputStarted() { 45 | 46 | } 47 | 48 | @Override 49 | public void keyPressed(int key, char c) { 50 | switch (key) { 51 | case Input.KEY_UP: 52 | this.player.setDy(-1); 53 | break; 54 | case Input.KEY_LEFT: 55 | this.player.setDx(-1); 56 | break; 57 | case Input.KEY_DOWN: 58 | this.player.setDy(1); 59 | break; 60 | case Input.KEY_RIGHT: 61 | this.player.setDx(1); 62 | break; 63 | } 64 | } 65 | 66 | @Override 67 | public void keyReleased(int key, char c) { 68 | switch (key) { 69 | case Input.KEY_UP: 70 | case Input.KEY_DOWN: 71 | this.player.setDy(0); 72 | break; 73 | case Input.KEY_LEFT: 74 | case Input.KEY_RIGHT: 75 | this.player.setDx(0); 76 | break; 77 | } 78 | } 79 | 80 | } 81 | -------------------------------------------------------------------------------- /src/main/java/lesson19/MapTriggerController.java: -------------------------------------------------------------------------------- 1 | package lesson19; 2 | 3 | import org.newdawn.slick.SlickException; 4 | 5 | /** 6 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 7 | * 8 | * @author Shionn, shionn@gmail.com http://shionn.org
9 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y- 10 | */ 11 | public class MapTriggerController { 12 | 13 | private Map map; 14 | private MapPlayer player; 15 | 16 | public MapTriggerController(Map map, MapPlayer player) { 17 | this.map = map; 18 | this.player = player; 19 | } 20 | 21 | public void update() throws SlickException { 22 | this.player.setOnStair(false); 23 | for (int objectID = 0; objectID < this.map.getObjectCount(); objectID++) { 24 | if (isInTrigger(objectID)) { 25 | if ("teleport".equals(this.map.getObjectType(objectID))) { 26 | this.teleport(objectID); 27 | } else if ("stair".equals(this.map.getObjectType(objectID))) { 28 | this.player.setOnStair(true); 29 | } else if ("change-map".equals(this.map.getObjectType(objectID))) { 30 | this.changeMap(objectID); 31 | } 32 | } 33 | } 34 | } 35 | 36 | private boolean isInTrigger(int id) { 37 | return this.player.getX() > this.map.getObjectX(id) 38 | && this.player.getX() < this.map.getObjectX(id) + this.map.getObjectWidth(id) 39 | && this.player.getY() > this.map.getObjectY(id) 40 | && this.player.getY() < this.map.getObjectY(id) + this.map.getObjectHeight(id); 41 | } 42 | 43 | private void teleport(int objectID) { 44 | this.player.setX(Float.parseFloat(this.map.getObjectProperty(objectID, "dest-x", 45 | Float.toString(this.player.getX())))); 46 | this.player.setY(Float.parseFloat(this.map.getObjectProperty(objectID, "dest-y", 47 | Float.toString(this.player.getY())))); 48 | } 49 | 50 | private void changeMap(int objectID) throws SlickException { 51 | this.teleport(objectID); 52 | String newMap = this.map.getObjectProperty(objectID, "dest-map", "undefined"); 53 | if (!"undefined".equals(newMap)) { 54 | this.map.changeMap("map/" + newMap); 55 | } 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/lesson20/BattleCommand.java: -------------------------------------------------------------------------------- 1 | package lesson20; 2 | 3 | import org.newdawn.slick.command.Command; 4 | 5 | public enum BattleCommand implements Command { 6 | ATTACK, DEFEND, FLEE, NONE 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/lesson20/BattleEnnemy.java: -------------------------------------------------------------------------------- 1 | package lesson20; 2 | 3 | import org.newdawn.slick.Color; 4 | import org.newdawn.slick.Font; 5 | import org.newdawn.slick.GameContainer; 6 | import org.newdawn.slick.Graphics; 7 | import org.newdawn.slick.Image; 8 | import org.newdawn.slick.SlickException; 9 | import org.newdawn.slick.geom.Vector2f; 10 | 11 | import shionn.slick.animation.AnimationListener; 12 | import shionn.slick.animation.BezierPath; 13 | import shionn.slick.animation.PathAnimation; 14 | 15 | /** 16 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 17 | * 18 | * @author Shionn, shionn@gmail.com http://shionn.org
19 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y+ 20 | */ 21 | public class BattleEnnemy { 22 | private int pv = 12; 23 | private Image ennemy; 24 | private PathAnimation animation; 25 | 26 | public void init() throws SlickException { 27 | this.ennemy = new Image("battle/gobelin.png").getScaledCopy(2); 28 | this.animation = new PathAnimation(new BezierPath(0, 0, -400, 1, 50, 20, 0, 0), 2000); 29 | } 30 | 31 | public void addAnimationListener(AnimationListener assignDamage, AnimationListener endAttack) { 32 | this.animation.addListener(1000, assignDamage); 33 | this.animation.addListener(2000, endAttack); 34 | } 35 | 36 | public void reset() { 37 | this.pv = 12; 38 | } 39 | 40 | public void render(GameContainer container, Graphics g) { 41 | Vector2f p = animation.currentLocation(); 42 | this.ennemy.drawCentered(p.x + container.getWidth() * 3 / 4, p.y + container.getHeight() 43 | / 2); 44 | Font font = g.getFont(); 45 | String text = Integer.toString(pv); 46 | font.drawString(container.getWidth() * 3 / 4 - font.getWidth(text) / 2, 47 | container.getHeight() / 2 - ennemy.getHeight() / 2 - font.getLineHeight(), text, 48 | Color.white); 49 | } 50 | 51 | public void update(int delta) { 52 | animation.update(delta); 53 | } 54 | 55 | public void startAttack() { 56 | animation.start(); 57 | } 58 | 59 | public void setPv(int pv) { 60 | this.pv = pv; 61 | } 62 | 63 | public int getPv() { 64 | return pv; 65 | } 66 | 67 | } 68 | -------------------------------------------------------------------------------- /src/main/java/lesson20/BattleGame.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 3 | * 4 | * @author Shionn, shionn@gmail.com http://shionn.org
5 | * GCS d- s+:+ a- C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- !y- 6 | */ 7 | package lesson20; 8 | 9 | import org.newdawn.slick.AppGameContainer; 10 | import org.newdawn.slick.GameContainer; 11 | import org.newdawn.slick.SlickException; 12 | import org.newdawn.slick.state.StateBasedGame; 13 | 14 | /** 15 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 16 | * 17 | * -Djava.library.path=target/natives 18 | * 19 | * @author Shionn, shionn@gmail.com http://shionn.org
20 | * GCS d- s+:+ a C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y+ 21 | */ 22 | public class BattleGame extends StateBasedGame { 23 | 24 | public static void main(String[] args) throws SlickException { 25 | AppGameContainer app = new AppGameContainer(new BattleGame(), 800, 600, false); 26 | app.setVSync(true); 27 | app.setShowFPS(false); 28 | app.start(); 29 | } 30 | 31 | public BattleGame() { 32 | super("Lesson 20 :: BattleGame"); 33 | } 34 | 35 | /** 36 | * Ici il suffit d'ajouter nos boucles de jeux. La première ajoutèe sera celle qui sera utilisée 37 | * au début 38 | */ 39 | @Override 40 | public void initStatesList(GameContainer container) throws SlickException { 41 | addState(new MainScreenGameState()); 42 | addState(new MapGameState()); 43 | addState(new BattleGameState()); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/lesson20/BattleGameState.java: -------------------------------------------------------------------------------- 1 | package lesson20; 2 | 3 | import org.newdawn.slick.GameContainer; 4 | import org.newdawn.slick.Graphics; 5 | import org.newdawn.slick.Image; 6 | import org.newdawn.slick.Input; 7 | import org.newdawn.slick.SlickException; 8 | import org.newdawn.slick.command.InputProvider; 9 | import org.newdawn.slick.command.KeyControl; 10 | import org.newdawn.slick.state.BasicGameState; 11 | import org.newdawn.slick.state.StateBasedGame; 12 | 13 | /** 14 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 15 | * 16 | * @author Shionn, shionn@gmail.com http://shionn.org
17 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y+ 18 | */ 19 | public class BattleGameState extends BasicGameState { 20 | 21 | public static final int ID = 3; 22 | private Image background; 23 | private BattleEnnemy ennemy = new BattleEnnemy(); 24 | private BattlePlayer player = new BattlePlayer(); 25 | private BattleHud hud; 26 | 27 | @Override 28 | public void init(GameContainer container, StateBasedGame game) throws SlickException { 29 | this.background = new Image("background/battle.png"); 30 | this.ennemy.init(); 31 | this.player.init(); 32 | 33 | InputProvider provider = new InputProvider(container.getInput()); 34 | provider.bindCommand(new KeyControl(Input.KEY_A), BattleCommand.ATTACK); 35 | provider.bindCommand(new KeyControl(Input.KEY_D), BattleCommand.DEFEND); 36 | provider.bindCommand(new KeyControl(Input.KEY_F), BattleCommand.FLEE); 37 | BattleController controller = new BattleController(player, ennemy, game); 38 | provider.addListener(controller); 39 | this.hud = new BattleHud(controller); 40 | this.hud.init(container); 41 | } 42 | 43 | @Override 44 | public void enter(GameContainer container, StateBasedGame game) throws SlickException { 45 | this.ennemy.reset(); 46 | } 47 | 48 | @Override 49 | public void render(GameContainer container, StateBasedGame game, Graphics g) 50 | throws SlickException { 51 | this.background.draw(0, 0, container.getWidth(), container.getHeight()); 52 | this.player.render(container, g); 53 | this.ennemy.render(container, g); 54 | this.hud.render(container, g); 55 | } 56 | 57 | @Override 58 | public void update(GameContainer container, StateBasedGame game, int delta) 59 | throws SlickException { 60 | this.player.update(delta); 61 | this.ennemy.update(delta); 62 | } 63 | 64 | @Override 65 | public int getID() { 66 | return ID; 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/main/java/lesson20/BattleHud.java: -------------------------------------------------------------------------------- 1 | package lesson20; 2 | 3 | import org.newdawn.slick.Color; 4 | import org.newdawn.slick.GameContainer; 5 | import org.newdawn.slick.Graphics; 6 | import org.newdawn.slick.Image; 7 | import org.newdawn.slick.SlickException; 8 | import org.newdawn.slick.gui.AbstractComponent; 9 | import org.newdawn.slick.gui.ComponentListener; 10 | import org.newdawn.slick.gui.MouseOverArea; 11 | 12 | import shionn.slick.ui.TextArea; 13 | import shionn.slick.ui.align.VerticalAlignement; 14 | 15 | /** 16 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 17 | * 18 | * @author Shionn, shionn@gmail.com http://shionn.org
19 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y+ 20 | */ 21 | public class BattleHud implements ComponentListener { 22 | 23 | private static final int Y_PADDING = 3; 24 | private static final int X_PADDING = 13; 25 | private static final int SPACE = 5; 26 | private BattleController controller; 27 | private MouseOverArea attackButton; 28 | private MouseOverArea fleeButton; 29 | private MouseOverArea defendButton; 30 | private TextArea log; 31 | 32 | public BattleHud(BattleController controller) { 33 | this.controller = controller; 34 | this.controller.setHud(this); 35 | } 36 | 37 | public void init(GameContainer container) throws SlickException { 38 | Image buttonImage = new Image("hud/button.png"); 39 | attackButton = new MouseOverArea(container, buttonImage, SPACE, container.getHeight() 40 | - (buttonImage.getHeight() + SPACE) * 3, this); 41 | defendButton = new MouseOverArea(container, buttonImage, SPACE, container.getHeight() 42 | - (buttonImage.getHeight() + SPACE) * 2, this); 43 | fleeButton = new MouseOverArea(container, buttonImage, SPACE, container.getHeight() 44 | - (buttonImage.getHeight() + SPACE) * 1, this); 45 | log = new TextArea(SPACE + attackButton.getWidth() + SPACE, attackButton.getY(), 46 | container.getWidth() - attackButton.getWidth() - SPACE * 3, buttonImage.getHeight() 47 | * 3 + SPACE * 2); 48 | log.setBottomUp(true); 49 | log.setDefaultFont(container.getDefaultFont()); 50 | } 51 | 52 | public void render(GameContainer container, Graphics g) { 53 | g.setColor(Color.black); 54 | attackButton.render(container, g); 55 | g.drawString("Attaquer", attackButton.getX() + X_PADDING, attackButton.getY() + Y_PADDING); 56 | defendButton.render(container, g); 57 | g.drawString("Defendre", defendButton.getX() + X_PADDING, defendButton.getY() + Y_PADDING); 58 | fleeButton.render(container, g); 59 | g.drawString("Fuire", fleeButton.getX() + X_PADDING, fleeButton.getY() + Y_PADDING); 60 | log.render(); 61 | } 62 | 63 | @Override 64 | public void componentActivated(AbstractComponent source) { 65 | if (source == attackButton) { 66 | controller.controlPressed(BattleCommand.ATTACK); 67 | } else if (source == defendButton) { 68 | controller.controlPressed(BattleCommand.DEFEND); 69 | } else if (source == fleeButton) { 70 | controller.controlPressed(BattleCommand.FLEE); 71 | } 72 | } 73 | 74 | public void addLog(String text) { 75 | log.addFirstText(text, VerticalAlignement.LEFT); 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/lesson20/BattlePlayer.java: -------------------------------------------------------------------------------- 1 | package lesson20; 2 | 3 | import org.newdawn.slick.Color; 4 | import org.newdawn.slick.Font; 5 | import org.newdawn.slick.GameContainer; 6 | import org.newdawn.slick.Graphics; 7 | import org.newdawn.slick.Image; 8 | import org.newdawn.slick.SlickException; 9 | import org.newdawn.slick.geom.Vector2f; 10 | 11 | import shionn.slick.animation.AnimationListener; 12 | import shionn.slick.animation.BezierPath; 13 | import shionn.slick.animation.PathAnimation; 14 | 15 | /** 16 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 17 | * 18 | * @author Shionn, shionn@gmail.com http://shionn.org
19 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y+ 20 | */ 21 | public class BattlePlayer { 22 | 23 | private int pv = 50; 24 | private Image hero; 25 | private PathAnimation animation; 26 | 27 | public void init() throws SlickException { 28 | this.hero = new Image("battle/hero.png").getScaledCopy(2); 29 | this.animation = new PathAnimation(new BezierPath(0, 0, 400, 1, -50, 20, 0, 0), 2000); 30 | } 31 | 32 | public void addAnimationListener(AnimationListener assignDamage, AnimationListener endAttack) { 33 | this.animation.addListener(1000, assignDamage); 34 | this.animation.addListener(2000, endAttack); 35 | } 36 | 37 | public void render(GameContainer container, Graphics g) { 38 | Vector2f p = animation.currentLocation(); 39 | hero.drawCentered(p.x + container.getWidth() * 1 / 4, p.y + container.getHeight() / 2); 40 | Font font = g.getFont(); 41 | String text = Integer.toString(pv); 42 | font.drawString(container.getWidth() * 1 / 4 - font.getWidth(text) / 2, 43 | container.getHeight() / 2 - hero.getHeight() / 2 - font.getLineHeight(), text, 44 | Color.white); 45 | } 46 | 47 | public void update(int delta) { 48 | animation.update(delta); 49 | } 50 | 51 | public void startAttack() { 52 | animation.start(); 53 | } 54 | 55 | public int getPv() { 56 | return pv; 57 | } 58 | 59 | public void setPv(int pv) { 60 | this.pv = pv; 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/lesson20/MainScreenGameState.java: -------------------------------------------------------------------------------- 1 | package lesson20; 2 | 3 | import org.newdawn.slick.GameContainer; 4 | import org.newdawn.slick.Graphics; 5 | import org.newdawn.slick.Image; 6 | import org.newdawn.slick.SlickException; 7 | import org.newdawn.slick.state.BasicGameState; 8 | import org.newdawn.slick.state.StateBasedGame; 9 | 10 | /** 11 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 12 | * 13 | * @author Shionn, shionn@gmail.com http://shionn.org
14 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y+ 15 | */ 16 | public class MainScreenGameState extends BasicGameState { 17 | 18 | public static final int ID = 1; 19 | private Image background; 20 | private StateBasedGame game; 21 | 22 | @Override 23 | public void init(GameContainer container, StateBasedGame game) throws SlickException { 24 | this.game = game; 25 | this.background = new Image("background/forest.png"); 26 | } 27 | 28 | /** 29 | * Contenons nous d'afficher l'image de fond. . 30 | */ 31 | @Override 32 | public void render(GameContainer container, StateBasedGame game, Graphics g) 33 | throws SlickException { 34 | background.draw(0, 0, container.getWidth(), container.getHeight()); 35 | g.drawString("Appuyer sur une touche", 300, 300); 36 | } 37 | 38 | @Override 39 | public void update(GameContainer container, StateBasedGame game, int delta) 40 | throws SlickException { 41 | } 42 | 43 | /** 44 | * Passer à l'ecran de jeu à l'appui de n'importe quel touche. 45 | */ 46 | @Override 47 | public void keyReleased(int key, char c) { 48 | game.enterState(MapGameState.ID); 49 | } 50 | 51 | /** 52 | * L'identifiant permet d'identifier les différentes boucles, pour passer de l'une à l'autre. 53 | */ 54 | @Override 55 | public int getID() { 56 | return ID; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/lesson20/Map.java: -------------------------------------------------------------------------------- 1 | package lesson20; 2 | 3 | import org.newdawn.slick.Color; 4 | import org.newdawn.slick.Image; 5 | import org.newdawn.slick.SlickException; 6 | import org.newdawn.slick.tiled.TiledMap; 7 | 8 | /** 9 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 10 | * 11 | * @author Shionn, shionn@gmail.com http://shionn.org
12 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y- 13 | */ 14 | public class Map { 15 | private TiledMap tiledMap; 16 | 17 | public void init() throws SlickException { 18 | this.tiledMap = new TiledMap("map/exemple-change-map.tmx"); 19 | } 20 | 21 | public void renderBackground() { 22 | this.tiledMap.render(0, 0, 0); 23 | this.tiledMap.render(0, 0, 1); 24 | this.tiledMap.render(0, 0, 2); 25 | } 26 | 27 | public void renderForeground() { 28 | this.tiledMap.render(0, 0, 3); 29 | this.tiledMap.render(0, 0, 4); 30 | } 31 | 32 | public boolean isCollision(float x, float y) { 33 | int tileW = this.tiledMap.getTileWidth(); 34 | int tileH = this.tiledMap.getTileHeight(); 35 | int logicLayer = this.tiledMap.getLayerIndex("logic"); 36 | Image tile = this.tiledMap.getTileImage((int) x / tileW, (int) y / tileH, logicLayer); 37 | boolean collision = tile != null; 38 | if (collision) { 39 | Color color = tile.getColor((int) x % tileW, (int) y % tileH); 40 | collision = color.getAlpha() > 0; 41 | } 42 | return collision; 43 | } 44 | 45 | public void changeMap(String file) throws SlickException { 46 | this.tiledMap = new TiledMap(file); 47 | } 48 | 49 | public int getObjectCount() { 50 | return this.tiledMap.getObjectCount(0); 51 | } 52 | 53 | public String getObjectType(int objectID) { 54 | return this.tiledMap.getObjectType(0, objectID); 55 | } 56 | 57 | public float getObjectX(int objectID) { 58 | return this.tiledMap.getObjectX(0, objectID); 59 | } 60 | 61 | public float getObjectY(int objectID) { 62 | return this.tiledMap.getObjectY(0, objectID); 63 | } 64 | 65 | public float getObjectWidth(int objectID) { 66 | return this.tiledMap.getObjectWidth(0, objectID); 67 | } 68 | 69 | public float getObjectHeight(int objectID) { 70 | return this.tiledMap.getObjectHeight(0, objectID); 71 | } 72 | 73 | public String getObjectProperty(int objectID, String propertyName, String def) { 74 | return this.tiledMap.getObjectProperty(0, objectID, propertyName, def); 75 | } 76 | 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/lesson20/MapCamera.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package lesson20; 5 | 6 | import org.newdawn.slick.GameContainer; 7 | import org.newdawn.slick.Graphics; 8 | 9 | /** 10 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 11 | * 12 | * @author Shionn, shionn@gmail.com http://shionn.org
13 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y- 14 | */ 15 | public class MapCamera { 16 | 17 | private MapPlayer player; 18 | private float xCamera, yCamera; 19 | 20 | public MapCamera(MapPlayer player) { 21 | this.player = player; 22 | this.xCamera = player.getX(); 23 | this.yCamera = player.getY(); 24 | } 25 | 26 | public void place(GameContainer container, Graphics g) { 27 | g.translate(container.getWidth() / 2 - (int) this.xCamera, container.getHeight() / 2 28 | - (int) this.yCamera); 29 | } 30 | 31 | public void update(GameContainer container) { 32 | int w = container.getWidth() / 4; 33 | if (this.player.getX() > this.xCamera + w) { 34 | this.xCamera = this.player.getX() - w; 35 | } else if (this.player.getX() < this.xCamera - w) { 36 | this.xCamera = this.player.getX() + w; 37 | } 38 | int h = container.getHeight() / 4; 39 | if (this.player.getY() > this.yCamera + h) { 40 | this.yCamera = this.player.getY() - h; 41 | } else if (this.player.getY() < this.yCamera - h) { 42 | this.yCamera = this.player.getY() + h; 43 | } 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/lesson20/MapGameState.java: -------------------------------------------------------------------------------- 1 | package lesson20; 2 | 3 | import org.newdawn.slick.GameContainer; 4 | import org.newdawn.slick.Graphics; 5 | import org.newdawn.slick.Input; 6 | import org.newdawn.slick.Music; 7 | import org.newdawn.slick.SlickException; 8 | import org.newdawn.slick.state.BasicGameState; 9 | import org.newdawn.slick.state.StateBasedGame; 10 | 11 | /** 12 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 13 | * 14 | * @author Shionn, shionn@gmail.com http://shionn.org
15 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y- 16 | */ 17 | public class MapGameState extends BasicGameState { 18 | public static final int ID = 2; 19 | 20 | private GameContainer container; 21 | private Map map = new Map(); 22 | private MapPlayer player = new MapPlayer(map); 23 | private MapTriggerController triggers = new MapTriggerController(map, player); 24 | private MapCamera camera = new MapCamera(player); 25 | private MapPlayerController controller = new MapPlayerController(player); 26 | private MapHud hud = new MapHud(); 27 | 28 | @Override 29 | public void init(GameContainer container, StateBasedGame game) throws SlickException { 30 | this.container = container; 31 | Music background = new Music("sound/lost-in-the-meadows.ogg"); 32 | background.loop(); 33 | this.map.init(); 34 | this.player.init(); 35 | this.hud.init(); 36 | this.controller.setInput(container.getInput()); 37 | container.getInput().addKeyListener(controller); 38 | } 39 | 40 | @Override 41 | public void render(GameContainer container, StateBasedGame game, Graphics g) 42 | throws SlickException { 43 | this.camera.place(container, g); 44 | this.map.renderBackground(); 45 | this.player.render(g); 46 | this.map.renderForeground(); 47 | this.hud.render(g); 48 | } 49 | 50 | @Override 51 | public void update(GameContainer container, StateBasedGame game, int delta) 52 | throws SlickException { 53 | this.controller.update(); 54 | this.triggers.update(); 55 | this.player.update(delta); 56 | this.camera.update(container); 57 | if (Math.random() < 0.005 && player.isMoving()) { 58 | game.enterState(BattleGameState.ID); 59 | } 60 | } 61 | 62 | @Override 63 | public void keyReleased(int key, char c) { 64 | if (Input.KEY_ESCAPE == key) { 65 | this.container.exit(); 66 | } 67 | } 68 | 69 | @Override 70 | public int getID() { 71 | return ID; 72 | } 73 | 74 | } 75 | -------------------------------------------------------------------------------- /src/main/java/lesson20/MapHud.java: -------------------------------------------------------------------------------- 1 | package lesson20; 2 | 3 | import org.newdawn.slick.Color; 4 | import org.newdawn.slick.Graphics; 5 | import org.newdawn.slick.Image; 6 | import org.newdawn.slick.SlickException; 7 | 8 | /** 9 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 10 | * 11 | * @author Shionn, shionn@gmail.com http://shionn.org
12 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y- 13 | */ 14 | public class MapHud { 15 | 16 | private static final int P_BAR_X = 10; 17 | private static final int P_BAR_Y = 10; 18 | private static final int BAR_X = 84 + P_BAR_X; 19 | private static final int LIFE_BAR_Y = 4 + P_BAR_Y; 20 | private static final int MANA_BAR_Y = 24 + P_BAR_Y; 21 | private static final int XP_BAR_Y = 44 + P_BAR_Y; 22 | private static final int BAR_WIDTH = 80; 23 | private static final int BAR_HEIGHT = 16; 24 | 25 | private static final Color LIFE_COLOR = new Color(255, 0, 0); 26 | private static final Color MANA_COLOR = new Color(0, 0, 255); 27 | private static final Color XP_COLOR = new Color(0, 255, 0); 28 | 29 | private Image playerbars; 30 | 31 | public void init() throws SlickException { 32 | this.playerbars = new Image("hud/player-bar.png"); 33 | } 34 | 35 | public void render(Graphics g) { 36 | g.resetTransform(); 37 | g.setColor(LIFE_COLOR); 38 | g.fillRect(BAR_X, LIFE_BAR_Y, .9f * BAR_WIDTH, BAR_HEIGHT); 39 | g.setColor(MANA_COLOR); 40 | g.fillRect(BAR_X, MANA_BAR_Y, .8f * BAR_WIDTH, BAR_HEIGHT); 41 | g.setColor(XP_COLOR); 42 | g.fillRect(BAR_X, XP_BAR_Y, .2f * BAR_WIDTH, BAR_HEIGHT); 43 | g.drawImage(playerbars, P_BAR_X, P_BAR_Y); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/lesson20/MapPlayerController.java: -------------------------------------------------------------------------------- 1 | package lesson20; 2 | 3 | import org.newdawn.slick.Input; 4 | import org.newdawn.slick.KeyListener; 5 | 6 | /** 7 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 8 | * 9 | * @author Shionn, shionn@gmail.com http://shionn.org
10 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y- 11 | */ 12 | public class MapPlayerController implements KeyListener { 13 | 14 | private MapPlayer player; 15 | private Input input; 16 | 17 | public MapPlayerController(MapPlayer player) { 18 | this.player = player; 19 | } 20 | 21 | public void update() { 22 | if (input.getControllerCount() > 0) { 23 | player.setDx(input.getAxisValue(0, 1)); 24 | player.setDy(input.getAxisValue(0, 2)); 25 | } 26 | } 27 | 28 | @Override 29 | public void setInput(Input input) { 30 | this.input = input; 31 | } 32 | 33 | @Override 34 | public boolean isAcceptingInput() { 35 | return true; 36 | } 37 | 38 | @Override 39 | public void inputEnded() { 40 | 41 | } 42 | 43 | @Override 44 | public void inputStarted() { 45 | 46 | } 47 | 48 | @Override 49 | public void keyPressed(int key, char c) { 50 | switch (key) { 51 | case Input.KEY_UP: 52 | this.player.setDy(-1); 53 | break; 54 | case Input.KEY_LEFT: 55 | this.player.setDx(-1); 56 | break; 57 | case Input.KEY_DOWN: 58 | this.player.setDy(1); 59 | break; 60 | case Input.KEY_RIGHT: 61 | this.player.setDx(1); 62 | break; 63 | } 64 | } 65 | 66 | @Override 67 | public void keyReleased(int key, char c) { 68 | switch (key) { 69 | case Input.KEY_UP: 70 | case Input.KEY_DOWN: 71 | this.player.setDy(0); 72 | break; 73 | case Input.KEY_LEFT: 74 | case Input.KEY_RIGHT: 75 | this.player.setDx(0); 76 | break; 77 | } 78 | } 79 | 80 | } 81 | -------------------------------------------------------------------------------- /src/main/java/lesson20/MapTriggerController.java: -------------------------------------------------------------------------------- 1 | package lesson20; 2 | 3 | import org.newdawn.slick.SlickException; 4 | 5 | /** 6 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 7 | * 8 | * @author Shionn, shionn@gmail.com http://shionn.org
9 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y- 10 | */ 11 | public class MapTriggerController { 12 | 13 | private Map map; 14 | private MapPlayer player; 15 | 16 | public MapTriggerController(Map map, MapPlayer player) { 17 | this.map = map; 18 | this.player = player; 19 | } 20 | 21 | public void update() throws SlickException { 22 | this.player.setOnStair(false); 23 | for (int objectID = 0; objectID < this.map.getObjectCount(); objectID++) { 24 | if (isInTrigger(objectID)) { 25 | if ("teleport".equals(this.map.getObjectType(objectID))) { 26 | this.teleport(objectID); 27 | } else if ("stair".equals(this.map.getObjectType(objectID))) { 28 | this.player.setOnStair(true); 29 | } else if ("change-map".equals(this.map.getObjectType(objectID))) { 30 | this.changeMap(objectID); 31 | } 32 | } 33 | } 34 | } 35 | 36 | private boolean isInTrigger(int id) { 37 | return this.player.getX() > this.map.getObjectX(id) 38 | && this.player.getX() < this.map.getObjectX(id) + this.map.getObjectWidth(id) 39 | && this.player.getY() > this.map.getObjectY(id) 40 | && this.player.getY() < this.map.getObjectY(id) + this.map.getObjectHeight(id); 41 | } 42 | 43 | private void teleport(int objectID) { 44 | this.player.setX(Float.parseFloat(this.map.getObjectProperty(objectID, "dest-x", 45 | Float.toString(this.player.getX())))); 46 | this.player.setY(Float.parseFloat(this.map.getObjectProperty(objectID, "dest-y", 47 | Float.toString(this.player.getY())))); 48 | } 49 | 50 | private void changeMap(int objectID) throws SlickException { 51 | this.teleport(objectID); 52 | String newMap = this.map.getObjectProperty(objectID, "dest-map", "undefined"); 53 | if (!"undefined".equals(newMap)) { 54 | this.map.changeMap("map/" + newMap); 55 | } 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/lesson21/BattleCommand.java: -------------------------------------------------------------------------------- 1 | package lesson21; 2 | 3 | import org.newdawn.slick.command.Command; 4 | 5 | public enum BattleCommand implements Command { 6 | ATTACK, DEFEND, FLEE, NONE 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/lesson21/BattleEnnemy.java: -------------------------------------------------------------------------------- 1 | package lesson21; 2 | 3 | import org.newdawn.slick.Color; 4 | import org.newdawn.slick.Font; 5 | import org.newdawn.slick.GameContainer; 6 | import org.newdawn.slick.Graphics; 7 | import org.newdawn.slick.Image; 8 | import org.newdawn.slick.SlickException; 9 | import org.newdawn.slick.geom.Vector2f; 10 | 11 | import shionn.slick.animation.AnimationListener; 12 | import shionn.slick.animation.BezierPath; 13 | import shionn.slick.animation.PathAnimation; 14 | 15 | /** 16 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 17 | * 18 | * @author Shionn, shionn@gmail.com http://shionn.org
19 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y+ 20 | */ 21 | public class BattleEnnemy { 22 | private int pv = 12; 23 | private Image ennemy; 24 | private PathAnimation animation; 25 | 26 | public void init() throws SlickException { 27 | this.ennemy = new Image("battle/gobelin.png").getScaledCopy(2); 28 | this.animation = new PathAnimation(new BezierPath(0, 0, -400, 1, 50, 20, 0, 0), 2000); 29 | } 30 | 31 | public void addAnimationListener(AnimationListener assignDamage, AnimationListener endAttack) { 32 | this.animation.addListener(1000, assignDamage); 33 | this.animation.addListener(2000, endAttack); 34 | } 35 | 36 | public void reset() { 37 | this.pv = 12; 38 | } 39 | 40 | public void render(GameContainer container, Graphics g) { 41 | Vector2f p = animation.currentLocation(); 42 | this.ennemy.drawCentered(p.x + container.getWidth() * 3 / 4, p.y + container.getHeight() 43 | / 2); 44 | Font font = g.getFont(); 45 | String text = Integer.toString(pv); 46 | font.drawString(container.getWidth() * 3 / 4 - font.getWidth(text) / 2, 47 | container.getHeight() / 2 - ennemy.getHeight() / 2 - font.getLineHeight(), text, 48 | Color.white); 49 | } 50 | 51 | public void update(int delta) { 52 | animation.update(delta); 53 | } 54 | 55 | public void startAttack() { 56 | animation.start(); 57 | } 58 | 59 | public void setPv(int pv) { 60 | this.pv = pv; 61 | } 62 | 63 | public int getPv() { 64 | return pv; 65 | } 66 | 67 | } 68 | -------------------------------------------------------------------------------- /src/main/java/lesson21/BattleGame.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 3 | * 4 | * @author Shionn, shionn@gmail.com http://shionn.org
5 | * GCS d- s+:+ a- C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- !y- 6 | */ 7 | package lesson21; 8 | 9 | import org.newdawn.slick.AppGameContainer; 10 | import org.newdawn.slick.GameContainer; 11 | import org.newdawn.slick.SlickException; 12 | import org.newdawn.slick.state.StateBasedGame; 13 | 14 | /** 15 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 16 | * 17 | * -Djava.library.path=target/natives 18 | * 19 | * @author Shionn, shionn@gmail.com http://shionn.org
20 | * GCS d- s+:+ a C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y+ 21 | */ 22 | public class BattleGame extends StateBasedGame { 23 | 24 | public static void main(String[] args) throws SlickException { 25 | AppGameContainer app = new AppGameContainer(new BattleGame(), 800, 600, false); 26 | app.setVSync(true); 27 | app.setShowFPS(false); 28 | app.start(); 29 | } 30 | 31 | public BattleGame() { 32 | super("Lesson 21 :: BattleGame"); 33 | } 34 | 35 | /** 36 | * Ici il suffit d'ajouter nos boucles de jeux. La première ajoutèe sera celle qui sera utilisée 37 | * au début 38 | */ 39 | @Override 40 | public void initStatesList(GameContainer container) throws SlickException { 41 | addState(new MainScreenGameState()); 42 | addState(new MapGameState()); 43 | addState(new BattleGameState()); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/lesson21/BattleGameState.java: -------------------------------------------------------------------------------- 1 | package lesson21; 2 | 3 | import org.newdawn.slick.GameContainer; 4 | import org.newdawn.slick.Graphics; 5 | import org.newdawn.slick.Image; 6 | import org.newdawn.slick.Input; 7 | import org.newdawn.slick.Music; 8 | import org.newdawn.slick.SlickException; 9 | import org.newdawn.slick.command.InputProvider; 10 | import org.newdawn.slick.command.KeyControl; 11 | import org.newdawn.slick.state.BasicGameState; 12 | import org.newdawn.slick.state.StateBasedGame; 13 | 14 | /** 15 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 16 | * 17 | * @author Shionn, shionn@gmail.com http://shionn.org
18 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y+ 19 | */ 20 | public class BattleGameState extends BasicGameState { 21 | 22 | public static final int ID = 3; 23 | private Image background; 24 | private BattleEnnemy ennemy = new BattleEnnemy(); 25 | private BattlePlayer player = new BattlePlayer(); 26 | private BattleHud hud; 27 | private Music music; 28 | 29 | @Override 30 | public void init(GameContainer container, StateBasedGame game) throws SlickException { 31 | this.background = new Image("background/battle.png"); 32 | this.ennemy.init(); 33 | this.player.init(); 34 | 35 | InputProvider provider = new InputProvider(container.getInput()); 36 | provider.bindCommand(new KeyControl(Input.KEY_A), BattleCommand.ATTACK); 37 | provider.bindCommand(new KeyControl(Input.KEY_D), BattleCommand.DEFEND); 38 | provider.bindCommand(new KeyControl(Input.KEY_F), BattleCommand.FLEE); 39 | BattleController controller = new BattleController(player, ennemy, game); 40 | provider.addListener(controller); 41 | this.hud = new BattleHud(controller); 42 | this.hud.init(container); 43 | this.music = new Music("sound/the-last-encounter-short-loop.ogg"); 44 | } 45 | 46 | @Override 47 | public void enter(GameContainer container, StateBasedGame game) throws SlickException { 48 | this.music.loop(1, .2f); 49 | this.ennemy.reset(); 50 | } 51 | 52 | @Override 53 | public void leave(GameContainer container, StateBasedGame game) throws SlickException { 54 | this.music.stop(); 55 | } 56 | 57 | @Override 58 | public void render(GameContainer container, StateBasedGame game, Graphics g) 59 | throws SlickException { 60 | this.background.draw(0, 0, container.getWidth(), container.getHeight()); 61 | this.player.render(container, g); 62 | this.ennemy.render(container, g); 63 | this.hud.render(container, g); 64 | } 65 | 66 | @Override 67 | public void update(GameContainer container, StateBasedGame game, int delta) 68 | throws SlickException { 69 | this.player.update(delta); 70 | this.ennemy.update(delta); 71 | } 72 | 73 | @Override 74 | public int getID() { 75 | return ID; 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/lesson21/BattleHud.java: -------------------------------------------------------------------------------- 1 | package lesson21; 2 | 3 | import org.newdawn.slick.Color; 4 | import org.newdawn.slick.GameContainer; 5 | import org.newdawn.slick.Graphics; 6 | import org.newdawn.slick.Image; 7 | import org.newdawn.slick.SlickException; 8 | import org.newdawn.slick.gui.AbstractComponent; 9 | import org.newdawn.slick.gui.ComponentListener; 10 | import org.newdawn.slick.gui.MouseOverArea; 11 | 12 | import shionn.slick.ui.TextArea; 13 | import shionn.slick.ui.align.VerticalAlignement; 14 | 15 | /** 16 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 17 | * 18 | * @author Shionn, shionn@gmail.com http://shionn.org
19 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y+ 20 | */ 21 | public class BattleHud implements ComponentListener { 22 | 23 | private static final int Y_PADDING = 3; 24 | private static final int X_PADDING = 13; 25 | private static final int SPACE = 5; 26 | private BattleController controller; 27 | private MouseOverArea attackButton; 28 | private MouseOverArea fleeButton; 29 | private MouseOverArea defendButton; 30 | private TextArea log; 31 | 32 | public BattleHud(BattleController controller) { 33 | this.controller = controller; 34 | this.controller.setHud(this); 35 | } 36 | 37 | public void init(GameContainer container) throws SlickException { 38 | Image buttonImage = new Image("hud/button.png"); 39 | attackButton = new MouseOverArea(container, buttonImage, SPACE, container.getHeight() 40 | - (buttonImage.getHeight() + SPACE) * 3, this); 41 | defendButton = new MouseOverArea(container, buttonImage, SPACE, container.getHeight() 42 | - (buttonImage.getHeight() + SPACE) * 2, this); 43 | fleeButton = new MouseOverArea(container, buttonImage, SPACE, container.getHeight() 44 | - (buttonImage.getHeight() + SPACE) * 1, this); 45 | log = new TextArea(SPACE + attackButton.getWidth() + SPACE, attackButton.getY(), 46 | container.getWidth() - attackButton.getWidth() - SPACE * 3, buttonImage.getHeight() 47 | * 3 + SPACE * 2); 48 | log.setBottomUp(true); 49 | log.setDefaultFont(container.getDefaultFont()); 50 | } 51 | 52 | public void render(GameContainer container, Graphics g) { 53 | g.setColor(Color.black); 54 | attackButton.render(container, g); 55 | g.drawString("Attaquer", attackButton.getX() + X_PADDING, attackButton.getY() + Y_PADDING); 56 | defendButton.render(container, g); 57 | g.drawString("Defendre", defendButton.getX() + X_PADDING, defendButton.getY() + Y_PADDING); 58 | fleeButton.render(container, g); 59 | g.drawString("Fuire", fleeButton.getX() + X_PADDING, fleeButton.getY() + Y_PADDING); 60 | log.render(); 61 | } 62 | 63 | @Override 64 | public void componentActivated(AbstractComponent source) { 65 | if (source == attackButton) { 66 | controller.controlPressed(BattleCommand.ATTACK); 67 | } else if (source == defendButton) { 68 | controller.controlPressed(BattleCommand.DEFEND); 69 | } else if (source == fleeButton) { 70 | controller.controlPressed(BattleCommand.FLEE); 71 | } 72 | } 73 | 74 | public void addLog(String text) { 75 | log.addFirstText(text, VerticalAlignement.LEFT); 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/lesson21/BattlePlayer.java: -------------------------------------------------------------------------------- 1 | package lesson21; 2 | 3 | import org.newdawn.slick.Color; 4 | import org.newdawn.slick.Font; 5 | import org.newdawn.slick.GameContainer; 6 | import org.newdawn.slick.Graphics; 7 | import org.newdawn.slick.Image; 8 | import org.newdawn.slick.SlickException; 9 | import org.newdawn.slick.geom.Vector2f; 10 | 11 | import shionn.slick.animation.AnimationListener; 12 | import shionn.slick.animation.BezierPath; 13 | import shionn.slick.animation.PathAnimation; 14 | 15 | /** 16 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 17 | * 18 | * @author Shionn, shionn@gmail.com http://shionn.org
19 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y+ 20 | */ 21 | public class BattlePlayer { 22 | 23 | private int pv = 50; 24 | private Image hero; 25 | private PathAnimation animation; 26 | 27 | public void init() throws SlickException { 28 | this.hero = new Image("battle/hero.png").getScaledCopy(2); 29 | this.animation = new PathAnimation(new BezierPath(0, 0, 400, 1, -50, 20, 0, 0), 2000); 30 | } 31 | 32 | public void addAnimationListener(AnimationListener assignDamage, AnimationListener endAttack) { 33 | this.animation.addListener(1000, assignDamage); 34 | this.animation.addListener(2000, endAttack); 35 | } 36 | 37 | public void render(GameContainer container, Graphics g) { 38 | Vector2f p = animation.currentLocation(); 39 | hero.drawCentered(p.x + container.getWidth() * 1 / 4, p.y + container.getHeight() / 2); 40 | Font font = g.getFont(); 41 | String text = Integer.toString(pv); 42 | font.drawString(container.getWidth() * 1 / 4 - font.getWidth(text) / 2, 43 | container.getHeight() / 2 - hero.getHeight() / 2 - font.getLineHeight(), text, 44 | Color.white); 45 | } 46 | 47 | public void update(int delta) { 48 | animation.update(delta); 49 | } 50 | 51 | public void startAttack() { 52 | animation.start(); 53 | } 54 | 55 | public int getPv() { 56 | return pv; 57 | } 58 | 59 | public void setPv(int pv) { 60 | this.pv = pv; 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/lesson21/MainScreenGameState.java: -------------------------------------------------------------------------------- 1 | package lesson21; 2 | 3 | import org.newdawn.slick.GameContainer; 4 | import org.newdawn.slick.Graphics; 5 | import org.newdawn.slick.Image; 6 | import org.newdawn.slick.SlickException; 7 | import org.newdawn.slick.state.BasicGameState; 8 | import org.newdawn.slick.state.StateBasedGame; 9 | 10 | /** 11 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 12 | * 13 | * @author Shionn, shionn@gmail.com http://shionn.org
14 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y+ 15 | */ 16 | public class MainScreenGameState extends BasicGameState { 17 | 18 | public static final int ID = 1; 19 | private Image background; 20 | private StateBasedGame game; 21 | 22 | @Override 23 | public void init(GameContainer container, StateBasedGame game) throws SlickException { 24 | this.game = game; 25 | this.background = new Image("background/forest.png"); 26 | } 27 | 28 | /** 29 | * Contenons nous d'afficher l'image de fond. . 30 | */ 31 | @Override 32 | public void render(GameContainer container, StateBasedGame game, Graphics g) 33 | throws SlickException { 34 | background.draw(0, 0, container.getWidth(), container.getHeight()); 35 | g.drawString("Appuyer sur une touche", 300, 300); 36 | } 37 | 38 | @Override 39 | public void update(GameContainer container, StateBasedGame game, int delta) 40 | throws SlickException { 41 | } 42 | 43 | /** 44 | * Passer à l'ecran de jeu à l'appui de n'importe quel touche. 45 | */ 46 | @Override 47 | public void keyReleased(int key, char c) { 48 | game.enterState(MapGameState.ID); 49 | } 50 | 51 | /** 52 | * L'identifiant permet d'identifier les différentes boucles, pour passer de l'une à l'autre. 53 | */ 54 | @Override 55 | public int getID() { 56 | return ID; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/lesson21/Map.java: -------------------------------------------------------------------------------- 1 | package lesson21; 2 | 3 | import org.newdawn.slick.Color; 4 | import org.newdawn.slick.Image; 5 | import org.newdawn.slick.SlickException; 6 | import org.newdawn.slick.tiled.TiledMap; 7 | 8 | /** 9 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 10 | * 11 | * @author Shionn, shionn@gmail.com http://shionn.org
12 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y- 13 | */ 14 | public class Map { 15 | private TiledMap tiledMap; 16 | 17 | public void init() throws SlickException { 18 | this.tiledMap = new TiledMap("map/exemple-change-map.tmx"); 19 | } 20 | 21 | public void renderBackground() { 22 | this.tiledMap.render(0, 0, 0); 23 | this.tiledMap.render(0, 0, 1); 24 | this.tiledMap.render(0, 0, 2); 25 | } 26 | 27 | public void renderForeground() { 28 | this.tiledMap.render(0, 0, 3); 29 | this.tiledMap.render(0, 0, 4); 30 | } 31 | 32 | public boolean isCollision(float x, float y) { 33 | int tileW = this.tiledMap.getTileWidth(); 34 | int tileH = this.tiledMap.getTileHeight(); 35 | int logicLayer = this.tiledMap.getLayerIndex("logic"); 36 | Image tile = this.tiledMap.getTileImage((int) x / tileW, (int) y / tileH, logicLayer); 37 | boolean collision = tile != null; 38 | if (collision) { 39 | Color color = tile.getColor((int) x % tileW, (int) y % tileH); 40 | collision = color.getAlpha() > 0; 41 | } 42 | return collision; 43 | } 44 | 45 | public void changeMap(String file) throws SlickException { 46 | this.tiledMap = new TiledMap(file); 47 | } 48 | 49 | public int getObjectCount() { 50 | return this.tiledMap.getObjectCount(0); 51 | } 52 | 53 | public String getObjectType(int objectID) { 54 | return this.tiledMap.getObjectType(0, objectID); 55 | } 56 | 57 | public float getObjectX(int objectID) { 58 | return this.tiledMap.getObjectX(0, objectID); 59 | } 60 | 61 | public float getObjectY(int objectID) { 62 | return this.tiledMap.getObjectY(0, objectID); 63 | } 64 | 65 | public float getObjectWidth(int objectID) { 66 | return this.tiledMap.getObjectWidth(0, objectID); 67 | } 68 | 69 | public float getObjectHeight(int objectID) { 70 | return this.tiledMap.getObjectHeight(0, objectID); 71 | } 72 | 73 | public String getObjectProperty(int objectID, String propertyName, String def) { 74 | return this.tiledMap.getObjectProperty(0, objectID, propertyName, def); 75 | } 76 | 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/lesson21/MapCamera.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package lesson21; 5 | 6 | import org.newdawn.slick.GameContainer; 7 | import org.newdawn.slick.Graphics; 8 | 9 | /** 10 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 11 | * 12 | * @author Shionn, shionn@gmail.com http://shionn.org
13 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y- 14 | */ 15 | public class MapCamera { 16 | 17 | private MapPlayer player; 18 | private float xCamera, yCamera; 19 | 20 | public MapCamera(MapPlayer player) { 21 | this.player = player; 22 | this.xCamera = player.getX(); 23 | this.yCamera = player.getY(); 24 | } 25 | 26 | public void place(GameContainer container, Graphics g) { 27 | g.translate(container.getWidth() / 2 - (int) this.xCamera, container.getHeight() / 2 28 | - (int) this.yCamera); 29 | } 30 | 31 | public void update(GameContainer container) { 32 | int w = container.getWidth() / 4; 33 | if (this.player.getX() > this.xCamera + w) { 34 | this.xCamera = this.player.getX() - w; 35 | } else if (this.player.getX() < this.xCamera - w) { 36 | this.xCamera = this.player.getX() + w; 37 | } 38 | int h = container.getHeight() / 4; 39 | if (this.player.getY() > this.yCamera + h) { 40 | this.yCamera = this.player.getY() - h; 41 | } else if (this.player.getY() < this.yCamera - h) { 42 | this.yCamera = this.player.getY() + h; 43 | } 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/lesson21/MapGameState.java: -------------------------------------------------------------------------------- 1 | package lesson21; 2 | 3 | import org.newdawn.slick.GameContainer; 4 | import org.newdawn.slick.Graphics; 5 | import org.newdawn.slick.Input; 6 | import org.newdawn.slick.Music; 7 | import org.newdawn.slick.SlickException; 8 | import org.newdawn.slick.state.BasicGameState; 9 | import org.newdawn.slick.state.StateBasedGame; 10 | 11 | /** 12 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 13 | * 14 | * @author Shionn, shionn@gmail.com http://shionn.org
15 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y- 16 | */ 17 | public class MapGameState extends BasicGameState { 18 | public static final int ID = 2; 19 | 20 | private GameContainer container; 21 | private Map map = new Map(); 22 | private MapPlayer player = new MapPlayer(map); 23 | private MapTriggerController triggers = new MapTriggerController(map, player); 24 | private MapCamera camera = new MapCamera(player); 25 | private MapPlayerController controller = new MapPlayerController(player); 26 | private MapHud hud = new MapHud(); 27 | 28 | private Music background; 29 | 30 | @Override 31 | public void init(GameContainer container, StateBasedGame game) throws SlickException { 32 | this.container = container; 33 | this.background = new Music("sound/lost-in-the-meadows.ogg"); 34 | this.map.init(); 35 | this.player.init(); 36 | this.hud.init(); 37 | this.controller.setInput(container.getInput()); 38 | container.getInput().addKeyListener(controller); 39 | } 40 | 41 | @Override 42 | public void enter(GameContainer container, StateBasedGame game) throws SlickException { 43 | background.loop(); 44 | } 45 | 46 | @Override 47 | public void leave(GameContainer container, StateBasedGame game) throws SlickException { 48 | background.stop(); 49 | } 50 | 51 | @Override 52 | public void render(GameContainer container, StateBasedGame game, Graphics g) 53 | throws SlickException { 54 | this.camera.place(container, g); 55 | this.map.renderBackground(); 56 | this.player.render(g); 57 | this.map.renderForeground(); 58 | this.hud.render(g); 59 | } 60 | 61 | @Override 62 | public void update(GameContainer container, StateBasedGame game, int delta) 63 | throws SlickException { 64 | this.controller.update(); 65 | this.triggers.update(); 66 | this.player.update(delta); 67 | this.camera.update(container); 68 | if (Math.random() < 0.005 && player.isMoving()) { 69 | game.enterState(BattleGameState.ID); 70 | } 71 | } 72 | 73 | @Override 74 | public void keyReleased(int key, char c) { 75 | if (Input.KEY_ESCAPE == key) { 76 | this.container.exit(); 77 | } 78 | } 79 | 80 | @Override 81 | public int getID() { 82 | return ID; 83 | } 84 | 85 | } 86 | -------------------------------------------------------------------------------- /src/main/java/lesson21/MapHud.java: -------------------------------------------------------------------------------- 1 | package lesson21; 2 | 3 | import org.newdawn.slick.Color; 4 | import org.newdawn.slick.Graphics; 5 | import org.newdawn.slick.Image; 6 | import org.newdawn.slick.SlickException; 7 | 8 | /** 9 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 10 | * 11 | * @author Shionn, shionn@gmail.com http://shionn.org
12 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y- 13 | */ 14 | public class MapHud { 15 | 16 | private static final int P_BAR_X = 10; 17 | private static final int P_BAR_Y = 10; 18 | private static final int BAR_X = 84 + P_BAR_X; 19 | private static final int LIFE_BAR_Y = 4 + P_BAR_Y; 20 | private static final int MANA_BAR_Y = 24 + P_BAR_Y; 21 | private static final int XP_BAR_Y = 44 + P_BAR_Y; 22 | private static final int BAR_WIDTH = 80; 23 | private static final int BAR_HEIGHT = 16; 24 | 25 | private static final Color LIFE_COLOR = new Color(255, 0, 0); 26 | private static final Color MANA_COLOR = new Color(0, 0, 255); 27 | private static final Color XP_COLOR = new Color(0, 255, 0); 28 | 29 | private Image playerbars; 30 | 31 | public void init() throws SlickException { 32 | this.playerbars = new Image("hud/player-bar.png"); 33 | } 34 | 35 | public void render(Graphics g) { 36 | g.resetTransform(); 37 | g.setColor(LIFE_COLOR); 38 | g.fillRect(BAR_X, LIFE_BAR_Y, .9f * BAR_WIDTH, BAR_HEIGHT); 39 | g.setColor(MANA_COLOR); 40 | g.fillRect(BAR_X, MANA_BAR_Y, .8f * BAR_WIDTH, BAR_HEIGHT); 41 | g.setColor(XP_COLOR); 42 | g.fillRect(BAR_X, XP_BAR_Y, .2f * BAR_WIDTH, BAR_HEIGHT); 43 | g.drawImage(playerbars, P_BAR_X, P_BAR_Y); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/lesson21/MapPlayerController.java: -------------------------------------------------------------------------------- 1 | package lesson21; 2 | 3 | import org.newdawn.slick.Input; 4 | import org.newdawn.slick.KeyListener; 5 | 6 | /** 7 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 8 | * 9 | * @author Shionn, shionn@gmail.com http://shionn.org
10 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y- 11 | */ 12 | public class MapPlayerController implements KeyListener { 13 | 14 | private MapPlayer player; 15 | private Input input; 16 | 17 | public MapPlayerController(MapPlayer player) { 18 | this.player = player; 19 | } 20 | 21 | public void update() { 22 | if (input.getControllerCount() > 0) { 23 | player.setDx(input.getAxisValue(0, 1)); 24 | player.setDy(input.getAxisValue(0, 2)); 25 | } 26 | } 27 | 28 | @Override 29 | public void setInput(Input input) { 30 | this.input = input; 31 | } 32 | 33 | @Override 34 | public boolean isAcceptingInput() { 35 | return true; 36 | } 37 | 38 | @Override 39 | public void inputEnded() { 40 | 41 | } 42 | 43 | @Override 44 | public void inputStarted() { 45 | 46 | } 47 | 48 | @Override 49 | public void keyPressed(int key, char c) { 50 | switch (key) { 51 | case Input.KEY_UP: 52 | this.player.setDy(-1); 53 | break; 54 | case Input.KEY_LEFT: 55 | this.player.setDx(-1); 56 | break; 57 | case Input.KEY_DOWN: 58 | this.player.setDy(1); 59 | break; 60 | case Input.KEY_RIGHT: 61 | this.player.setDx(1); 62 | break; 63 | } 64 | } 65 | 66 | @Override 67 | public void keyReleased(int key, char c) { 68 | switch (key) { 69 | case Input.KEY_UP: 70 | case Input.KEY_DOWN: 71 | this.player.setDy(0); 72 | break; 73 | case Input.KEY_LEFT: 74 | case Input.KEY_RIGHT: 75 | this.player.setDx(0); 76 | break; 77 | } 78 | } 79 | 80 | } 81 | -------------------------------------------------------------------------------- /src/main/java/lesson21/MapTriggerController.java: -------------------------------------------------------------------------------- 1 | package lesson21; 2 | 3 | import org.newdawn.slick.SlickException; 4 | 5 | /** 6 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 7 | * 8 | * @author Shionn, shionn@gmail.com http://shionn.org
9 | * GCS d- s+:+ a+ C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y- 10 | */ 11 | public class MapTriggerController { 12 | 13 | private Map map; 14 | private MapPlayer player; 15 | 16 | public MapTriggerController(Map map, MapPlayer player) { 17 | this.map = map; 18 | this.player = player; 19 | } 20 | 21 | public void update() throws SlickException { 22 | this.player.setOnStair(false); 23 | for (int objectID = 0; objectID < this.map.getObjectCount(); objectID++) { 24 | if (isInTrigger(objectID)) { 25 | if ("teleport".equals(this.map.getObjectType(objectID))) { 26 | this.teleport(objectID); 27 | } else if ("stair".equals(this.map.getObjectType(objectID))) { 28 | this.player.setOnStair(true); 29 | } else if ("change-map".equals(this.map.getObjectType(objectID))) { 30 | this.changeMap(objectID); 31 | } 32 | } 33 | } 34 | } 35 | 36 | private boolean isInTrigger(int id) { 37 | return this.player.getX() > this.map.getObjectX(id) 38 | && this.player.getX() < this.map.getObjectX(id) + this.map.getObjectWidth(id) 39 | && this.player.getY() > this.map.getObjectY(id) 40 | && this.player.getY() < this.map.getObjectY(id) + this.map.getObjectHeight(id); 41 | } 42 | 43 | private void teleport(int objectID) { 44 | this.player.setX(Float.parseFloat(this.map.getObjectProperty(objectID, "dest-x", 45 | Float.toString(this.player.getX())))); 46 | this.player.setY(Float.parseFloat(this.map.getObjectProperty(objectID, "dest-y", 47 | Float.toString(this.player.getY())))); 48 | } 49 | 50 | private void changeMap(int objectID) throws SlickException { 51 | this.teleport(objectID); 52 | String newMap = this.map.getObjectProperty(objectID, "dest-map", "undefined"); 53 | if (!"undefined".equals(newMap)) { 54 | this.map.changeMap("map/" + newMap); 55 | } 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/question/TextFieldGame.java: -------------------------------------------------------------------------------- 1 | package question; 2 | 3 | import org.newdawn.slick.AppGameContainer; 4 | import org.newdawn.slick.BasicGame; 5 | import org.newdawn.slick.Font; 6 | import org.newdawn.slick.GameContainer; 7 | import org.newdawn.slick.Graphics; 8 | import org.newdawn.slick.Input; 9 | import org.newdawn.slick.SlickException; 10 | import org.newdawn.slick.gui.TextField; 11 | 12 | /** 13 | * Code sous licence GPLv3 (http://www.gnu.org/licenses/gpl.html) 14 | * 15 | * -Djava.library.path=target/natives 16 | * 17 | * @author Shionn, shionn@gmail.com http://shionn.org
18 | * GCS d- s+:+ a C++ UL/M P L+ E--- W++ N K- w-- M+ t+ 5 X R+ !tv b+ D+ G- e+++ h+ r- y+ 19 | */ 20 | public class TextFieldGame extends BasicGame { 21 | 22 | private GameContainer container; 23 | private TextField textField; 24 | 25 | public static void main(String[] args) throws SlickException { 26 | new AppGameContainer(new TextFieldGame(), 800, 600, false).start(); 27 | } 28 | 29 | public TextFieldGame() { 30 | super("TextFieldGame"); 31 | } 32 | 33 | @Override 34 | public void init(GameContainer container) throws SlickException { 35 | this.container = container; 36 | Font font = container.getDefaultFont(); 37 | this.textField = new TextField(container, font, 50, 50, 200, font.getLineHeight() + 5); 38 | } 39 | 40 | @Override 41 | public void render(GameContainer container, Graphics g) throws SlickException { 42 | this.textField.render(container, g); 43 | } 44 | 45 | @Override 46 | public void update(GameContainer container, int delta) throws SlickException { 47 | System.out.println(this.textField.getText()); 48 | } 49 | 50 | @Override 51 | public void keyReleased(int key, char c) { 52 | if (Input.KEY_ESCAPE == key) { 53 | container.exit(); 54 | } 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /src/main/resources/background/battle-alternatif.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shionn/SlickBlogTutorial/40752fe081617b1c92999f298293d597a3797a70/src/main/resources/background/battle-alternatif.png -------------------------------------------------------------------------------- /src/main/resources/background/battle.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shionn/SlickBlogTutorial/40752fe081617b1c92999f298293d597a3797a70/src/main/resources/background/battle.png -------------------------------------------------------------------------------- /src/main/resources/background/forest.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shionn/SlickBlogTutorial/40752fe081617b1c92999f298293d597a3797a70/src/main/resources/background/forest.png -------------------------------------------------------------------------------- /src/main/resources/battle/gobelin.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shionn/SlickBlogTutorial/40752fe081617b1c92999f298293d597a3797a70/src/main/resources/battle/gobelin.png -------------------------------------------------------------------------------- /src/main/resources/battle/hero.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shionn/SlickBlogTutorial/40752fe081617b1c92999f298293d597a3797a70/src/main/resources/battle/hero.png -------------------------------------------------------------------------------- /src/main/resources/contribution: -------------------------------------------------------------------------------- 1 | Tileset et personnage : http://opengameart.org/content/liberated-pixel-cup-lpc-base-assets-sprites-map-tiles 2 | Tileset du desert : http://opengameart.org/content/whispers-of-avalon-desert-tileset 3 | Tileset du hud : http://opengameart.org/content/golden-ui-bigger-than-ever-edition 4 | Musique de fond : http://opengameart.org/content/lost-in-the-meadows 5 | Musique de combat : http://opengameart.org/content/rpg-battle-theme-the-last-encounter-0 6 | Fanfare de victoire : http://opengameart.org/content/lively-meadow-victory-fanfare-and-song 7 | -------------------------------------------------------------------------------- /src/main/resources/hud/button.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shionn/SlickBlogTutorial/40752fe081617b1c92999f298293d597a3797a70/src/main/resources/hud/button.png -------------------------------------------------------------------------------- /src/main/resources/hud/player-bar.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shionn/SlickBlogTutorial/40752fe081617b1c92999f298293d597a3797a70/src/main/resources/hud/player-bar.png -------------------------------------------------------------------------------- /src/main/resources/map/tuiles/barrel.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shionn/SlickBlogTutorial/40752fe081617b1c92999f298293d597a3797a70/src/main/resources/map/tuiles/barrel.png -------------------------------------------------------------------------------- /src/main/resources/map/tuiles/brackish.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shionn/SlickBlogTutorial/40752fe081617b1c92999f298293d597a3797a70/src/main/resources/map/tuiles/brackish.png -------------------------------------------------------------------------------- /src/main/resources/map/tuiles/bridges.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shionn/SlickBlogTutorial/40752fe081617b1c92999f298293d597a3797a70/src/main/resources/map/tuiles/bridges.png -------------------------------------------------------------------------------- /src/main/resources/map/tuiles/buckets.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shionn/SlickBlogTutorial/40752fe081617b1c92999f298293d597a3797a70/src/main/resources/map/tuiles/buckets.png -------------------------------------------------------------------------------- /src/main/resources/map/tuiles/cabinets.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shionn/SlickBlogTutorial/40752fe081617b1c92999f298293d597a3797a70/src/main/resources/map/tuiles/cabinets.png -------------------------------------------------------------------------------- /src/main/resources/map/tuiles/castle_lightsources.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shionn/SlickBlogTutorial/40752fe081617b1c92999f298293d597a3797a70/src/main/resources/map/tuiles/castle_lightsources.png -------------------------------------------------------------------------------- /src/main/resources/map/tuiles/castle_outside.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shionn/SlickBlogTutorial/40752fe081617b1c92999f298293d597a3797a70/src/main/resources/map/tuiles/castle_outside.png -------------------------------------------------------------------------------- /src/main/resources/map/tuiles/castlefloors.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shionn/SlickBlogTutorial/40752fe081617b1c92999f298293d597a3797a70/src/main/resources/map/tuiles/castlefloors.png -------------------------------------------------------------------------------- /src/main/resources/map/tuiles/castlefloors_outside.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shionn/SlickBlogTutorial/40752fe081617b1c92999f298293d597a3797a70/src/main/resources/map/tuiles/castlefloors_outside.png -------------------------------------------------------------------------------- /src/main/resources/map/tuiles/castlewalls.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shionn/SlickBlogTutorial/40752fe081617b1c92999f298293d597a3797a70/src/main/resources/map/tuiles/castlewalls.png -------------------------------------------------------------------------------- /src/main/resources/map/tuiles/cement.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shionn/SlickBlogTutorial/40752fe081617b1c92999f298293d597a3797a70/src/main/resources/map/tuiles/cement.png -------------------------------------------------------------------------------- /src/main/resources/map/tuiles/cementstair.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shionn/SlickBlogTutorial/40752fe081617b1c92999f298293d597a3797a70/src/main/resources/map/tuiles/cementstair.png -------------------------------------------------------------------------------- /src/main/resources/map/tuiles/chests.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shionn/SlickBlogTutorial/40752fe081617b1c92999f298293d597a3797a70/src/main/resources/map/tuiles/chests.png -------------------------------------------------------------------------------- /src/main/resources/map/tuiles/country.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shionn/SlickBlogTutorial/40752fe081617b1c92999f298293d597a3797a70/src/main/resources/map/tuiles/country.png -------------------------------------------------------------------------------- /src/main/resources/map/tuiles/cup.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shionn/SlickBlogTutorial/40752fe081617b1c92999f298293d597a3797a70/src/main/resources/map/tuiles/cup.png -------------------------------------------------------------------------------- /src/main/resources/map/tuiles/desert.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shionn/SlickBlogTutorial/40752fe081617b1c92999f298293d597a3797a70/src/main/resources/map/tuiles/desert.png -------------------------------------------------------------------------------- /src/main/resources/map/tuiles/dirt.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shionn/SlickBlogTutorial/40752fe081617b1c92999f298293d597a3797a70/src/main/resources/map/tuiles/dirt.png -------------------------------------------------------------------------------- /src/main/resources/map/tuiles/dirt2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shionn/SlickBlogTutorial/40752fe081617b1c92999f298293d597a3797a70/src/main/resources/map/tuiles/dirt2.png -------------------------------------------------------------------------------- /src/main/resources/map/tuiles/dungeon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shionn/SlickBlogTutorial/40752fe081617b1c92999f298293d597a3797a70/src/main/resources/map/tuiles/dungeon.png -------------------------------------------------------------------------------- /src/main/resources/map/tuiles/grass.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shionn/SlickBlogTutorial/40752fe081617b1c92999f298293d597a3797a70/src/main/resources/map/tuiles/grass.png -------------------------------------------------------------------------------- /src/main/resources/map/tuiles/grassalt.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shionn/SlickBlogTutorial/40752fe081617b1c92999f298293d597a3797a70/src/main/resources/map/tuiles/grassalt.png -------------------------------------------------------------------------------- /src/main/resources/map/tuiles/hole.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shionn/SlickBlogTutorial/40752fe081617b1c92999f298293d597a3797a70/src/main/resources/map/tuiles/hole.png -------------------------------------------------------------------------------- /src/main/resources/map/tuiles/holek.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shionn/SlickBlogTutorial/40752fe081617b1c92999f298293d597a3797a70/src/main/resources/map/tuiles/holek.png -------------------------------------------------------------------------------- /src/main/resources/map/tuiles/holemid.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shionn/SlickBlogTutorial/40752fe081617b1c92999f298293d597a3797a70/src/main/resources/map/tuiles/holemid.png -------------------------------------------------------------------------------- /src/main/resources/map/tuiles/house.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shionn/SlickBlogTutorial/40752fe081617b1c92999f298293d597a3797a70/src/main/resources/map/tuiles/house.png -------------------------------------------------------------------------------- /src/main/resources/map/tuiles/inside.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shionn/SlickBlogTutorial/40752fe081617b1c92999f298293d597a3797a70/src/main/resources/map/tuiles/inside.png -------------------------------------------------------------------------------- /src/main/resources/map/tuiles/kitchen.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shionn/SlickBlogTutorial/40752fe081617b1c92999f298293d597a3797a70/src/main/resources/map/tuiles/kitchen.png -------------------------------------------------------------------------------- /src/main/resources/map/tuiles/lava.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shionn/SlickBlogTutorial/40752fe081617b1c92999f298293d597a3797a70/src/main/resources/map/tuiles/lava.png -------------------------------------------------------------------------------- /src/main/resources/map/tuiles/lavarock.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shionn/SlickBlogTutorial/40752fe081617b1c92999f298293d597a3797a70/src/main/resources/map/tuiles/lavarock.png -------------------------------------------------------------------------------- /src/main/resources/map/tuiles/logic.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shionn/SlickBlogTutorial/40752fe081617b1c92999f298293d597a3797a70/src/main/resources/map/tuiles/logic.png -------------------------------------------------------------------------------- /src/main/resources/map/tuiles/mountains.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shionn/SlickBlogTutorial/40752fe081617b1c92999f298293d597a3797a70/src/main/resources/map/tuiles/mountains.png -------------------------------------------------------------------------------- /src/main/resources/map/tuiles/rock.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shionn/SlickBlogTutorial/40752fe081617b1c92999f298293d597a3797a70/src/main/resources/map/tuiles/rock.png -------------------------------------------------------------------------------- /src/main/resources/map/tuiles/signs.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shionn/SlickBlogTutorial/40752fe081617b1c92999f298293d597a3797a70/src/main/resources/map/tuiles/signs.png -------------------------------------------------------------------------------- /src/main/resources/map/tuiles/stairs.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shionn/SlickBlogTutorial/40752fe081617b1c92999f298293d597a3797a70/src/main/resources/map/tuiles/stairs.png -------------------------------------------------------------------------------- /src/main/resources/map/tuiles/treetop.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shionn/SlickBlogTutorial/40752fe081617b1c92999f298293d597a3797a70/src/main/resources/map/tuiles/treetop.png -------------------------------------------------------------------------------- /src/main/resources/map/tuiles/trunk.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shionn/SlickBlogTutorial/40752fe081617b1c92999f298293d597a3797a70/src/main/resources/map/tuiles/trunk.png -------------------------------------------------------------------------------- /src/main/resources/map/tuiles/victoria.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shionn/SlickBlogTutorial/40752fe081617b1c92999f298293d597a3797a70/src/main/resources/map/tuiles/victoria.png -------------------------------------------------------------------------------- /src/main/resources/map/tuiles/water.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shionn/SlickBlogTutorial/40752fe081617b1c92999f298293d597a3797a70/src/main/resources/map/tuiles/water.png -------------------------------------------------------------------------------- /src/main/resources/map/tuiles/waterfall.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shionn/SlickBlogTutorial/40752fe081617b1c92999f298293d597a3797a70/src/main/resources/map/tuiles/waterfall.png -------------------------------------------------------------------------------- /src/main/resources/map/tuiles/watergrass.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shionn/SlickBlogTutorial/40752fe081617b1c92999f298293d597a3797a70/src/main/resources/map/tuiles/watergrass.png -------------------------------------------------------------------------------- /src/main/resources/sound/lively-meadow-victory-fanfare.ogg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shionn/SlickBlogTutorial/40752fe081617b1c92999f298293d597a3797a70/src/main/resources/sound/lively-meadow-victory-fanfare.ogg -------------------------------------------------------------------------------- /src/main/resources/sound/lost-in-the-meadows.ogg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shionn/SlickBlogTutorial/40752fe081617b1c92999f298293d597a3797a70/src/main/resources/sound/lost-in-the-meadows.ogg -------------------------------------------------------------------------------- /src/main/resources/sound/the-last-encounter-short-loop.ogg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shionn/SlickBlogTutorial/40752fe081617b1c92999f298293d597a3797a70/src/main/resources/sound/the-last-encounter-short-loop.ogg -------------------------------------------------------------------------------- /src/main/resources/sprites/character.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shionn/SlickBlogTutorial/40752fe081617b1c92999f298293d597a3797a70/src/main/resources/sprites/character.png -------------------------------------------------------------------------------- /src/main/resources/start.bat: -------------------------------------------------------------------------------- 1 | java -Djava.library.path=natives -jar SlickBlogTutorial.jar -------------------------------------------------------------------------------- /src/main/resources/start.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | java -Djava.library.path=natives -jar SlickBlogTutorial.jar 3 | --------------------------------------------------------------------------------