From 2cc92d9014d433bdf12e09ccca64aefbbca93331 Mon Sep 17 00:00:00 2001 From: Jonathan Hager Date: Fri, 25 Sep 2020 21:14:29 +0200 Subject: [PATCH] lots and lots of changes lol --- core/src/com/trs/game/StaticMath.java | 36 ++++++++++++++++++ core/src/com/trs/game/model/Monster.java | 41 +++++++++++++++++++++ core/src/com/trs/game/model/Projectile.java | 29 +++++++++++++++ 3 files changed, 106 insertions(+) diff --git a/core/src/com/trs/game/StaticMath.java b/core/src/com/trs/game/StaticMath.java index 7f595f0..fd86503 100644 --- a/core/src/com/trs/game/StaticMath.java +++ b/core/src/com/trs/game/StaticMath.java @@ -1,4 +1,40 @@ package com.trs.game; public class StaticMath { + public static double calculateAngle(int xPos1, int yPos1, int xPos2, int yPos2){ + float deltaX = xPos2 - xPos1; + float deltaY = yPos2 - yPos1; + + double angle; + if(deltaY == 0){ + if(deltaX < 0){ + angle = Math.PI; + } + else{ + angle = 0; + } + } + else if(deltaX == 0 && deltaY >= 0){ + angle = Math.PI / 2; + } + else if(deltaX == 0 && deltaY < 0){ + angle = Math.PI / -2; + } + else{ + angle = Math.abs(Math.atan(deltaY / deltaX)); + + if(deltaX < 0 && deltaY < 0){ + angle = Math.PI + angle; + } + else if(deltaX < 0 && deltaY > 0){ + angle = Math.PI - angle; + } + else if(deltaX > 0 && deltaY < 0){ + angle = 2*Math.PI - angle; + } + } + + return angle; + } + } diff --git a/core/src/com/trs/game/model/Monster.java b/core/src/com/trs/game/model/Monster.java index 3fa806c..af9a2db 100644 --- a/core/src/com/trs/game/model/Monster.java +++ b/core/src/com/trs/game/model/Monster.java @@ -1,4 +1,45 @@ package com.trs.game.model; +import com.trs.game.StaticMath; + public class Monster { + + private final int SPEED = 3; + + private int xPos; + private int yPos; + private double movementX; + private double movementY; + private int xPosTarget; + private int yPosTarget; + + public Monster(int xPos, int yPos){ + this.xPos = xPos; + this.yPos = yPos; + } + + public void move(){ + this.xPos += this.movementX; + this.yPos += this.movementY; + } + + private void checkTarget(){ + if(this.xPos == this.xPosTarget && this.yPos == this.yPosTarget){ + this.generateNewTarget(); + } + } + + private void generateNewTarget(){ + // TODO: generate new target coordinates + + calculateMovement(); + } + + private void calculateMovement(){ + double angle = StaticMath.calculateAngle(this.xPos, this.yPos, this.xPosTarget, this.yPosTarget); + + movementX = Math.cos(angle) * SPEED; + movementY = Math.sin(angle) * SPEED; + } + } diff --git a/core/src/com/trs/game/model/Projectile.java b/core/src/com/trs/game/model/Projectile.java index 477d359..89f2ebe 100644 --- a/core/src/com/trs/game/model/Projectile.java +++ b/core/src/com/trs/game/model/Projectile.java @@ -1,4 +1,33 @@ package com.trs.game.model; +import com.trs.game.StaticMath; + public class Projectile { + + private final int SPEED = 3; + + private int xPos; + private int yPos; + private double movementX; + private double movementY; + + public Projectile(int xPos, int yPos, int xPosMonster, int yPosMonster){ + this.xPos = xPos; + this.yPos = yPos; + + // calculating values for movementX and movementY + double angle = StaticMath.calculateAngle(this.xPos, this.yPos, xPosMonster, yPosMonster); + + movementX = Math.cos(angle) * SPEED; + movementY = Math.sin(angle) * SPEED; + } + + public void collision(){ + + } + + public void move(){ + this.xPos += this.movementX; + this.yPos += this.movementY; + } }