forked from IF-LK-2020/zulda
collision
This commit is contained in:
parent
213f44f114
commit
2280b25d26
|
@ -27,4 +27,8 @@ public class Main extends Game {
|
|||
public void tasteReagieren(int code) {
|
||||
|
||||
}
|
||||
|
||||
public World getWorld() {
|
||||
return world;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,11 +11,11 @@ public class Map extends Knoten {
|
|||
for (int x = 0; x < map.length; x++) {
|
||||
for (int y = 0; y < map[0].length; y++) {
|
||||
int id = 0;
|
||||
if (y == 1) {
|
||||
id = 2;
|
||||
if (y == 1 || (x == 7 && y == 7)) {
|
||||
id = Tile.WALL_BOTTOM;
|
||||
}
|
||||
if (y == 0 || y == map[0].length - 1 || x == 0 || x == map.length - 1) {
|
||||
id = 1;
|
||||
if (y == 0 || y == map[0].length - 1 || x == 0 || x == map.length - 1 || (x == 7 && y == 6)) {
|
||||
id = Tile.WALL_TOP;
|
||||
}
|
||||
map[x][y] = new Tile(id);
|
||||
map[x][y].setX(x * Tile.getSize());
|
||||
|
@ -24,4 +24,15 @@ public class Map extends Knoten {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Tile getTile(int x, int y) {
|
||||
if (x >= 0 && x < map.length && y >= 0 && y < map[0].length) {
|
||||
return map[x][y];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public int[][] getCollisionMap(){
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package main;
|
||||
|
||||
import ea.ActionFigur;
|
||||
import ea.Rechteck;
|
||||
import ea.Taste;
|
||||
import ea.Ticker;
|
||||
|
||||
|
@ -11,8 +12,10 @@ public class Player extends ActionFigur implements Ticker {
|
|||
private boolean side;
|
||||
|
||||
private float velX, velY;
|
||||
private int oldPosX, oldPosY;
|
||||
private float accelleration = 0.15f;
|
||||
private float friction = 0.2f;
|
||||
private Rechteck hitBox = new Rechteck(0, 0, 0, 0);
|
||||
|
||||
public Player(Main main) {
|
||||
super(loader.getPlayer(0), "idle_left");
|
||||
|
@ -26,10 +29,18 @@ public class Player extends ActionFigur implements Ticker {
|
|||
neuerZustand(loader.getPlayer(6), "swipe_left");
|
||||
neuerZustand(loader.getPlayer(7), "swipe_right");
|
||||
neuerZustand(loader.getPlayer(8), "die");
|
||||
|
||||
loader.getPlayer(0).animationsGeschwindigkeitSetzen(200);
|
||||
loader.getPlayer(1).animationsGeschwindigkeitSetzen(200);
|
||||
loader.getPlayer(2).animationsGeschwindigkeitSetzen(50);
|
||||
loader.getPlayer(3).animationsGeschwindigkeitSetzen(50);
|
||||
|
||||
positionSetzen(Tile.getSize() * 2, Tile.getSize() * 2);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void tick() {
|
||||
|
||||
if (!((aktuellesVerhalten() == "strike_left" || aktuellesVerhalten() == "strike_right")
|
||||
&& aktuelleFigur().aktuellesBild() < aktuelleFigur().animation().length - 1)) {
|
||||
|
||||
|
@ -52,21 +63,123 @@ public class Player extends ActionFigur implements Ticker {
|
|||
setY(getY() + velY);
|
||||
velX *= (1f - friction);
|
||||
velY *= (1f - friction);
|
||||
if (velX < 0.2f && velX > -0.2f) {
|
||||
velX = 0f;
|
||||
}
|
||||
if (velY < 0.2f && velY > -0.2f) {
|
||||
velY = 0f;
|
||||
}
|
||||
|
||||
hitBox.positionSetzen(positionX() + 103, positionY() + 35);
|
||||
hitBox.breiteSetzen(50);
|
||||
hitBox.hoeheSetzen(80);
|
||||
|
||||
final int xOffset = hitBox.positionX() - positionX();
|
||||
final int yOffset = hitBox.positionY() - positionY();
|
||||
int tileX, tileY;
|
||||
Tile t;
|
||||
// collision left
|
||||
if (hitBox.positionX() - oldPosX < 0) {
|
||||
tileX = hitBox.positionX() / Tile.getSize();
|
||||
tileY = hitBox.positionY() / Tile.getSize();
|
||||
t = main.getWorld().getCurrentMap().getTile(tileX, tileY);
|
||||
if (t != null && t.isCollidable()) {
|
||||
velX = 0;
|
||||
int x = t.getRight() - xOffset;
|
||||
oldPosX = x;
|
||||
positionSetzen(x, positionY());
|
||||
}
|
||||
tileX = hitBox.positionX() / Tile.getSize();
|
||||
tileY = (int) (hitBox.positionY() + hitBox.getHoehe()) / Tile.getSize();
|
||||
t = main.getWorld().getCurrentMap().getTile(tileX, tileY);
|
||||
if (t != null && t.isCollidable()) {
|
||||
velX = 0;
|
||||
int x = t.getRight() - xOffset;
|
||||
oldPosX = x;
|
||||
positionSetzen(x, positionY());
|
||||
}
|
||||
}
|
||||
// collision right
|
||||
if (hitBox.positionX() - oldPosX > 0) {
|
||||
tileX = (int) (hitBox.positionX() + hitBox.getBreite()) / Tile.getSize();
|
||||
tileY = hitBox.positionY() / Tile.getSize();
|
||||
t = main.getWorld().getCurrentMap().getTile(tileX, tileY);
|
||||
if (t != null && t.isCollidable()) {
|
||||
velX = 0;
|
||||
int x = t.getLeft() - xOffset - (int) hitBox.getBreite();
|
||||
oldPosX = x;
|
||||
positionSetzen(x, positionY());
|
||||
}
|
||||
tileX = (int) (hitBox.positionX() + hitBox.getBreite()) / Tile.getSize();
|
||||
tileY = (int) (hitBox.positionY() + hitBox.getHoehe()) / Tile.getSize();
|
||||
t = main.getWorld().getCurrentMap().getTile(tileX, tileY);
|
||||
if (t != null && t.isCollidable()) {
|
||||
velX = 0;
|
||||
int x = t.getLeft() - xOffset - (int) hitBox.getBreite();
|
||||
oldPosX = x;
|
||||
positionSetzen(x, positionY());
|
||||
}
|
||||
}
|
||||
// collision top
|
||||
if (hitBox.positionY() - oldPosY < 0) {
|
||||
tileX = hitBox.positionX() / Tile.getSize();
|
||||
tileY = hitBox.positionY() / Tile.getSize();
|
||||
t = main.getWorld().getCurrentMap().getTile(tileX, tileY);
|
||||
if (t != null && t.isCollidable()) {
|
||||
velY = 0;
|
||||
int y = t.getBottom() - yOffset;
|
||||
oldPosY = y;
|
||||
positionSetzen(positionX(), y);
|
||||
}
|
||||
tileX = (int) (hitBox.positionX() + hitBox.getBreite()) / Tile.getSize();
|
||||
tileY = hitBox.positionY() / Tile.getSize();
|
||||
t = main.getWorld().getCurrentMap().getTile(tileX, tileY);
|
||||
if (t != null && t.isCollidable()) {
|
||||
velY = 0;
|
||||
int y = t.getBottom() - yOffset;
|
||||
oldPosY = y;
|
||||
positionSetzen(positionX(), y);
|
||||
}
|
||||
}
|
||||
// collision bottom
|
||||
if (hitBox.positionY() - oldPosY > 0) {
|
||||
tileX = hitBox.positionX() / Tile.getSize();
|
||||
tileY = (int) (hitBox.positionY() + hitBox.getHoehe()) / Tile.getSize();
|
||||
t = main.getWorld().getCurrentMap().getTile(tileX, tileY);
|
||||
if (t != null && t.isCollidable()) {
|
||||
velY = 0;
|
||||
int y = t.getTop() - (int) hitBox.getHoehe() - yOffset;
|
||||
oldPosY = y;
|
||||
positionSetzen(positionX(), y);
|
||||
}
|
||||
tileX = (int) (hitBox.positionX() + hitBox.getBreite()) / Tile.getSize();
|
||||
tileY = (int) (hitBox.positionY() + hitBox.getHoehe()) / Tile.getSize();
|
||||
t = main.getWorld().getCurrentMap().getTile(tileX, tileY);
|
||||
if (t != null && t.isCollidable()) {
|
||||
velY = 0;
|
||||
int y = t.getTop() - (int) hitBox.getHoehe() - yOffset;
|
||||
oldPosY = y;
|
||||
positionSetzen(positionX(), y);
|
||||
}
|
||||
}
|
||||
|
||||
oldPosX = hitBox.positionX();
|
||||
oldPosY = hitBox.positionY();
|
||||
|
||||
if (!((aktuellesVerhalten() == "swipe_left" || aktuellesVerhalten() == "swipe_right")
|
||||
&& aktuelleFigur().aktuellesBild() < aktuelleFigur().animation().length - 1)) {
|
||||
if (velX > 0.2f || velY > 0.2f || velX < -0.2f || velY < -0.2f) {
|
||||
if (side) {
|
||||
zustandSetzen("walk_right");
|
||||
} else {
|
||||
zustandSetzen("walk_left");
|
||||
}
|
||||
} else {
|
||||
if (velX == 0 && velY == 0) {
|
||||
if (side) {
|
||||
zustandSetzen("idle_right");
|
||||
} else {
|
||||
zustandSetzen("idle_left");
|
||||
}
|
||||
} else {
|
||||
if (side) {
|
||||
zustandSetzen("walk_right");
|
||||
} else {
|
||||
zustandSetzen("walk_left");
|
||||
}
|
||||
}
|
||||
|
||||
if (main.tasteGedrueckt(Taste.LEERTASTE)) {
|
||||
|
@ -86,5 +199,4 @@ public class Player extends ActionFigur implements Ticker {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -17,8 +17,10 @@ public class Tile extends Knoten {
|
|||
public static final int WALL_BOTTOM = 2;
|
||||
|
||||
private Bild img;
|
||||
private int id;
|
||||
|
||||
public Tile(int id) {
|
||||
this.id = id;
|
||||
try {
|
||||
BufferedImage buff = ImageIO.read(Tile.class.getResourceAsStream(getPathFromId(id)));
|
||||
if (id == GRASS) {
|
||||
|
@ -53,4 +55,24 @@ public class Tile extends Knoten {
|
|||
public static int getSize() {
|
||||
return 16 * World.SCALE;
|
||||
}
|
||||
|
||||
public boolean isCollidable() {
|
||||
return id == WALL_TOP;
|
||||
}
|
||||
|
||||
public int getTop() {
|
||||
return positionY();
|
||||
}
|
||||
|
||||
public int getBottom() {
|
||||
return positionY() + getSize();
|
||||
}
|
||||
|
||||
public int getLeft() {
|
||||
return positionX();
|
||||
}
|
||||
|
||||
public int getRight() {
|
||||
return positionX() + getSize();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,4 +14,8 @@ public class World extends Knoten {
|
|||
add(currentMap);
|
||||
add(player);
|
||||
}
|
||||
|
||||
public Map getCurrentMap() {
|
||||
return currentMap;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue