CalvinMT / LITIengine_test

MIT License
0 stars 0 forks source link

So now that I have a sprite working, how do I get collision between the map and the player? Or collision with another object? #3

Open Dmartin42 opened 5 years ago

Dmartin42 commented 5 years ago

Also, I used your rotation system and was wondering if there was a way to put an x and a y on my player. Attached is my source code. `public class mainLoader {

public static List <Player> listPlayers = new ArrayList <Player> ();

public static void main(String[] args) {
    // TODO Auto-generated method stub

    Resources.setEncoding(Resources.ENCODING_UTF_8);
    Game.setInfo("gameInfo.xml");

    Game.init();
    Game.getRenderEngine().setBaseRenderScale((float) 1.5);
    Game.getScreenManager().setResolution(Resolution.custom(1280, 720, "1280*720"));
    Game.getScreenManager().addScreen(new mainMenu());
    Game.getScreenManager().addScreen(mainGameScreen.getInstance());
    Game.getScreenManager().displayScreen(mainMenu.NAME);
    Game.start();
    System.out.println("Game version is: " + Game.getInfo().getVersion());
    System.out.println("Currently active screen: " + Game.getScreenManager().getCurrentScreen().getName());
}

} package Screens;

import java.awt.Image;

import javax.swing.ImageIcon;

import de.gurkenlabs.litiengine.Game; import de.gurkenlabs.litiengine.gui.ImageComponent; import de.gurkenlabs.litiengine.gui.screens.Screen;

public class mainMenu extends Screen{ public static final String NAME = "Menu"; public mainMenu() { super("Menu"); } public void prepare() { super.prepare(); } protected void initializeComponents () { Image imageCursorMain = new ImageIcon("resources/cursor_white_164.png").getImage(); Game.getScreenManager().getRenderComponent().setCursor(imageCursorMain, 0, 0); final double screenCenterX = Game.getScreenManager().getResolution().getWidth() / 2.0; final double screenCenterY = Game.getScreenManager().getResolution().getHeight() / 2.0; ImageComponent start = new ImageComponent(screenCenterX-150, screenCenterY-50, 300, 100, null, "Start Game", null); ImageComponent exit = new ImageComponent(start.getX(), start.getY()+start.getHeight(), 300, 100, null, "Exit Game", null);

    start.onClicked(e -> {
        Image imageCursor = new ImageIcon("resources/cursor_invisible_1.png").getImage();
        Game.getScreenManager().getRenderComponent().setCursor(imageCursor, 0, 0);
        Game.getScreenManager().displayScreen(mainGameScreen.NAME);
    });

    exit.onClicked(e -> System.exit(0));

    this.getComponents().add(start);
    this.getComponents().add(exit);
}

} package Screens;

import java.awt.Graphics2D; import java.awt.geom.Point2D; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap;

import Engine.MoreRotaion; import Entities.Player; import de.gurkenlabs.litiengine.Game; import de.gurkenlabs.litiengine.environment.EntitySpawner; import de.gurkenlabs.litiengine.environment.Environment; import de.gurkenlabs.litiengine.environment.tilemap.IMap; import de.gurkenlabs.litiengine.gui.ImageComponent; import de.gurkenlabs.litiengine.gui.screens.Screen;

public class mainGameScreen extends Screen{ public static final String NAME = "Game"; private static mainGameScreen instance; //private EntitySpawner ennemySpawner; protected mainGameScreen() { super("Game"); } public static mainGameScreen getInstance() { if(instance==null) instance = new mainGameScreen(); return instance; } public void prepare () { super.prepare();

    // Initialize Game
    final double startX = 10;
    final double startY = 100;

    Game.load("Resources\\maps\\tmap.litidata");
    Environment myEnv = new Environment("TestMap.tmx");
    Game.loadEnvironment(myEnv);
    Game.getCamera().setClampToMap(true);
    Player player = new Player(new Point2D.Double(startX, startY));
    Game.getEnvironment().add(player);

}
public void render (final Graphics2D graphics2D) {
    if (Game.getEnvironment() != null) {
        Game.getEnvironment().render(graphics2D);
    }

    super.render(graphics2D);
}

} package Engine;

public enum MoreRotaion { NONE, ROTATE_45, ROTATE_90, ROTATE_135, ROTATE_180, ROTATE_225, ROTATE_270, ROTATE_315;

  public double getRadians() {
    int value;
    switch (this) {
        case ROTATE_45:
          value = 45;
          break;
        case ROTATE_90:
          value = 90;
          break;
        case ROTATE_135:
          value = 135;
          break;
        case ROTATE_180:
          value = 180;
          break;
        case ROTATE_225:
          value = 225;
          break;
        case ROTATE_270:
          value = 270;
          break;
        case ROTATE_315:
          value = 315;
          break;
        default:
          value = 0;
          break;
    }

    return Math.toRadians(value);
  }

} package Entities;

import java.awt.event.KeyEvent; import java.awt.geom.Point2D;

import javax.swing.ImageIcon;

import Engine.MoreRotaion; import Engine.mainLoader; import Screens.mainMenu; import de.gurkenlabs.litiengine.Direction; import de.gurkenlabs.litiengine.Game; import de.gurkenlabs.litiengine.entities.Creature; import de.gurkenlabs.litiengine.entities.IMobileEntity; import de.gurkenlabs.litiengine.environment.tilemap.MapLoader; import de.gurkenlabs.litiengine.graphics.animation.EntityAnimationController; import de.gurkenlabs.litiengine.graphics.animation.PropAnimationController; import de.gurkenlabs.litiengine.input.Input; import de.gurkenlabs.litiengine.input.KeyboardEntityController;

public class Player extends Creature{ private MoreRotaion rotation;

public Player (Point2D position) {
    this.setName("player1");
    this.setTeam(1);
    this.setLocation(position.getX() - (46/2), position.getY() - (46/2));
    this.rotation = MoreRotaion.NONE;
    mainLoader.listPlayers.add(this);
    this.initialize();
}

public void initialize () {
    KeyboardEntityController <Player> keyboardController = new KeyboardEntityController<Player>(this, KeyEvent.VK_UP, KeyEvent.VK_DOWN, KeyEvent.VK_LEFT, KeyEvent.VK_RIGHT);
    this.addController(keyboardController);
    this.setController(EntityAnimationController.class, new PlayerAnimationController(this));       
    this.initializeInputs();
}

private void initializeInputs () {
    Input.keyboard().onKeyPressed(KeyEvent.VK_UP, (key) -> this.changeDirection(Direction.UP));
    Input.keyboard().onKeyPressed(KeyEvent.VK_DOWN, (key) -> this.changeDirection(Direction.DOWN));
    Input.keyboard().onKeyPressed(KeyEvent.VK_LEFT, (key) -> this.changeDirection(Direction.LEFT));
    Input.keyboard().onKeyPressed(KeyEvent.VK_RIGHT, (key) -> this.changeDirection(Direction.RIGHT));

// Input.keyboard().onKeyTyped(KeyEvent.VK_SPACE, (key) -> { // this.fire(this); // });

    Input.keyboard().onKeyTyped(KeyEvent.VK_ESCAPE, (key) -> {
        System.exit(0);
        Game.getScreenManager().displayScreen(mainMenu.NAME);
        Game.getScreenManager().getRenderComponent().setCursor(new ImageIcon("resources/cursor_white_64.png").getImage(), 0, 0);
        try {
            //TODO - Finalizes this instance of InGameScreen
            //Game.getScreenManager().getCurrentScreen()..finalize();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    });
}

private void changeDirection (Direction direction) {
    switch (direction) {
        case UP:
            if (Input.keyboard().isPressed(KeyEvent.VK_LEFT)) {
                this.getAnimationController().playAnimation("player_left");
                this.rotation = MoreRotaion.ROTATE_270;
            }
            else if (Input.keyboard().isPressed(KeyEvent.VK_RIGHT)) {
                this.getAnimationController().playAnimation("player_right");
                this.rotation = MoreRotaion.ROTATE_45;
            }
            else {
                this.getAnimationController().playAnimation("player_top");
                this.rotation = MoreRotaion.NONE;
            }
            break;
        case DOWN:
            if (Input.keyboard().isPressed(KeyEvent.VK_LEFT)) {
                this.getAnimationController().playAnimation("player_left");
                this.rotation = MoreRotaion.ROTATE_225;
            }
            else if (Input.keyboard().isPressed(KeyEvent.VK_RIGHT)) {
                this.getAnimationController().playAnimation("player_right");
                this.rotation = MoreRotaion.ROTATE_135;
            }
            else {
                this.getAnimationController().playAnimation("player_bottom");
                this.rotation = MoreRotaion.ROTATE_180;
            }
            break;
        case LEFT:
            if (Input.keyboard().isPressed(KeyEvent.VK_UP)) {
                this.getAnimationController().playAnimation("player_left");
                this.rotation = MoreRotaion.ROTATE_315;
            }
            else if (Input.keyboard().isPressed(KeyEvent.VK_DOWN)) {
                this.getAnimationController().playAnimation("player_back");
                this.rotation = MoreRotaion.ROTATE_225;
            }
            else {
                this.getAnimationController().playAnimation("player_left");
                this.rotation = MoreRotaion.ROTATE_270;
            }
            break;
        case RIGHT:
            if (Input.keyboard().isPressed(KeyEvent.VK_UP)) {
                this.getAnimationController().playAnimation("player_front");
                this.rotation = MoreRotaion.ROTATE_45;
            }
            else if (Input.keyboard().isPressed(KeyEvent.VK_DOWN)) {
                this.getAnimationController().playAnimation("player_right");
                this.rotation = MoreRotaion.ROTATE_135;
            }
            else {
                this.getAnimationController().playAnimation("player_right");
                this.rotation = MoreRotaion.ROTATE_90;
            }
            break;
        default:
            break;
    }
    this.getAnimationController().update();
}

// private void fire (Player player) { // FirePlayer firePlayer = new FirePlayer(player); // listFired.add(firePlayer); // Game.getEnvironment().add(firePlayer); // }

public boolean canBeKilledBy (final IMobileEntity otherEntity) {
    if (otherEntity instanceof Player) {
        return false;
    }
    return true;
}

public MoreRotaion getRotation () {
    return this.rotation;
}

} `

Dmartin42 commented 5 years ago

I also have the player animation controller but I decided not to include it.

Dmartin42 commented 5 years ago

Have you created collisions?

CalvinMT commented 5 years ago

Collisions are working for me. I managed them threw the map files (i.e.: ".tmx" and ".litidata"). I use Tiled to open the ".tmx" file, and utiLITI to open the ".litidata" file.

It is possible to use X and Y inputs for controller support if that is what you're asking for. Please follow this link: https://forum.litiengine.com/d/11-gamepad-inputs