diff --git a/core/src/com/trs/game/model/Model.java b/core/src/com/trs/game/model/Model.java index ea62bee..ef693a4 100644 --- a/core/src/com/trs/game/model/Model.java +++ b/core/src/com/trs/game/model/Model.java @@ -20,10 +20,10 @@ public class Model { } public void timerStep(){ - monster.move(); + monster.move(walls); for(Projectile projectile : projectiles){ - projectile.move(); + projectile.move(walls); } } diff --git a/core/src/com/trs/game/model/Monster.java b/core/src/com/trs/game/model/Monster.java index d3405c8..4482f7f 100644 --- a/core/src/com/trs/game/model/Monster.java +++ b/core/src/com/trs/game/model/Monster.java @@ -4,6 +4,8 @@ import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.glutils.ShapeRenderer; import com.trs.game.StaticMath; +import java.util.ArrayList; + public class Monster { private final int SPEED = 3; @@ -42,7 +44,7 @@ public class Monster { renderer.end(); } - public void move(){ + public void move(ArrayList walls){ checkTarget(); this.xPos += this.movementX; diff --git a/core/src/com/trs/game/model/Projectile.java b/core/src/com/trs/game/model/Projectile.java index f888a85..eeac25e 100644 --- a/core/src/com/trs/game/model/Projectile.java +++ b/core/src/com/trs/game/model/Projectile.java @@ -1,8 +1,14 @@ package com.trs.game.model; import com.badlogic.gdx.math.Circle; +import com.badlogic.gdx.math.Intersector; +import com.badlogic.gdx.math.Polygon; +import com.badlogic.gdx.math.Rectangle; import com.trs.game.StaticMath; +import java.lang.reflect.Array; +import java.util.ArrayList; + public class Projectile { private final int SPEED = 3; @@ -22,15 +28,40 @@ public class Projectile { movementY = Math.sin(angle) * SPEED; } - public void collision(){ - } - public void move(){ + public void move(ArrayList walls){ + checkCollision(walls); + circle.x += this.movementX; circle.y += this.movementY; } + private void checkCollision(ArrayList walls){ + for(Wall wall : walls){ + if(Intersector.overlaps(circle, wall.getRect())){ + + circle.x -= movementX; + circle.y -= movementY; + + collision(wall); + break; + } + } + } + + private void collision(Wall wall){ + System.out.println("Collision"); + + double alpha = Math.atan(movementY/movementX); + double delta = (Math.PI / 2) - Math.toRadians(wall.getRotation()); + double beta = +((Math.PI / 2) - alpha - delta); + double phi = beta + Math.toRadians(wall.getRotation()); + + movementX = Math.cos(phi) * SPEED; + movementY = Math.sin(phi) * SPEED; + } + public int getxPos() { return (int) circle.x; }