Commit e65553a3 authored by Lukas Marik's avatar Lukas Marik

#940 move object to correct position when wall position is bad

parent 43b6a21e
......@@ -149,7 +149,7 @@ public class GameObject extends ObjectModel {
}
}
public void initEdgePoint(WallObjectDescriptor descriptor){
private void initEdgePoint(WallObjectDescriptor descriptor){
float minWidth = objectImage.getDrawable().getMinWidth();
float minHeight = objectImage.getDrawable().getMinHeight();
......@@ -269,7 +269,8 @@ public class GameObject extends ObjectModel {
public void moveToPosition(boolean anim, float xPosition, float yPosition, Runnable finishAction) {
if (anim) {
addAction(sequence(moveTo(xPosition, yPosition, DROP_ANIM_DURATION, CARD_INTERPOLATION), run(finishAction)));
if(finishAction != null) addAction(sequence(moveTo(xPosition, yPosition, DROP_ANIM_DURATION, CARD_INTERPOLATION), run(finishAction)));
else addAction(sequence(moveTo(xPosition, yPosition, DROP_ANIM_DURATION, CARD_INTERPOLATION)));
} else {
setPosition(xPosition, yPosition);
}
......
......@@ -18,9 +18,11 @@
package cz.nic.tablexia.game.games.protocol.model;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Group;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import java.util.ArrayList;
import java.util.Collections;
......@@ -40,6 +42,7 @@ import cz.nic.tablexia.game.games.protocol.gameobjects.furniture.FurnitureType;
import cz.nic.tablexia.game.games.protocol.gameobjects.wall.WallType;
import cz.nic.tablexia.game.games.protocol.utils.ObjectsPositionComparator;
import cz.nic.tablexia.game.games.protocol.utils.PositionTransfer;
import cz.nic.tablexia.loader.application.ApplicationAtlasManager;
import cz.nic.tablexia.util.Log;
/**
......@@ -49,6 +52,7 @@ import cz.nic.tablexia.util.Log;
public class RoomGroup extends Group {
public static final int MIN_OBJECTS_DISTANCE = 60;
private static final float WALL_PADDING = 5f;
private int countObjects = 0;
private ProtocolGame protocolGame;
......@@ -277,6 +281,8 @@ public class RoomGroup extends Group {
controlObjectsPosition(changedObject);
}
Image lastIntersect; //TODO smazat po otestovaní a refactoringu
private void controlWallPosition(GameObject gameObject, WallType wallType){
Vector2 lBObject = new Vector2(gameObject.getX() + gameObject.getlBPoint().x, gameObject.getY() + gameObject.getlBPoint().y);
Vector2 lTObject = new Vector2(gameObject.getX() + gameObject.getlTPoint().x, gameObject.getY() + gameObject.getlTPoint().y);
......@@ -296,26 +302,124 @@ public class RoomGroup extends Group {
boolean checkRBPoints = checkPointBetweenTwoLines(rTWall, rBWall, lBWall, rBObject);
boolean checkRTPoints = checkPointBetweenTwoLines(lTWall, rTWall, rBWall, rTObject);
Log.info("WALL_CHECK", "LB ON WALL: " + checkLBPoints + "\n" +
"LT ON WALL: " + checkLTPoints + "\n" +
"RB ON WALL: " + checkRBPoints + "\n" +
"RT ON WALL: " + checkRTPoints);
if(!(checkLBPoints && checkLTPoints && checkRBPoints && checkRTPoints)){
//kontrola kam posunout
int pointsCNT =(checkLBPoints ? 0 : 1) + (checkLTPoints ? 0 : 1) + (checkRBPoints ? 0 : 1) + (checkRTPoints ? 0 : 1);
Log.info("WALL_CHECK", "POINTS CNT: " + pointsCNT);
int pointsCNT = (checkLBPoints ? 0 : 1) + (checkLTPoints ? 0 : 1) + (checkRBPoints ? 0 : 1) + (checkRTPoints ? 0 : 1);
if(pointsCNT == 3){
boolean useLB = !checkLBPoints && checkRTPoints;
boolean useLT = !checkLTPoints && checkRBPoints;
boolean useRB = !checkRBPoints && checkLTPoints;
boolean useRT = !checkRTPoints && checkLBPoints;
Vector2 wall = useLB ? lBWall : useLT ? lTWall : useRB ? rBWall : rTWall;
Vector2 object = useLB ? lBObject : useLT ? lTObject : useRB ? rBObject : rTObject;
float correctX = gameObject.getX() + (wall.x - object.x) + WALL_PADDING * ((useRT || useRB) ? -1 : 1);
float correctY = gameObject.getY() + (wall.y - object.y) + WALL_PADDING * ((useLT || useRT) ? -1 : 1);
gameObject.moveToPosition(true, correctX, correctY, null);
}else if(pointsCNT == 2) {
boolean bottom = checkLTPoints && checkRTPoints;
boolean right = checkLBPoints && checkLTPoints;
boolean left = checkRBPoints && checkRTPoints;
boolean top = checkLBPoints && checkRBPoints;
Vector2[] wallPoints = new Vector2[2];
Vector2[] objectPoints = new Vector2[2];
float paddingX = WALL_PADDING;
float paddingY = WALL_PADDING;
wallPoints[0] = top ? lTWall : right ? rBWall : lBWall;
wallPoints[1] = bottom ? rBWall : left ? lTWall : rTWall;
Vector2 check;
if(top){
objectPoints[0] = wallType == WallType.LEFT_WALL ? rTObject : lTObject;
objectPoints[1] = wallType == WallType.LEFT_WALL ? rBObject : lBObject;
paddingX *= wallType == WallType.LEFT_WALL ? 1 : -1;
paddingY *= wallType == WallType.LEFT_WALL ? 1 : -1;
check = wallType == WallType.LEFT_WALL ? rTObject : lTObject;
}else if (bottom){
objectPoints[0] = wallType == WallType.LEFT_WALL ? rTObject : lTObject;
objectPoints[1] = wallType == WallType.LEFT_WALL ? rBObject : lBObject;
paddingX *= wallType == WallType.LEFT_WALL ? 1 : -1;
paddingY *= wallType == WallType.LEFT_WALL ? 1 : -1;
check = wallType == WallType.LEFT_WALL ? rBObject : lBObject;
}else if(right){
objectPoints[0] = wallType == WallType.RIGHT_WALL ? lTObject : lBObject;
objectPoints[1] = wallType == WallType.RIGHT_WALL ? rTObject : rBObject;
paddingX = 0;
paddingY = 0;
check = wallType == WallType.RIGHT_WALL ? rTObject : rBObject;
}else {
objectPoints[0] = wallType == WallType.LEFT_WALL ? lTObject : lBObject;
objectPoints[1] = wallType == WallType.LEFT_WALL ? rTObject : rBObject;
paddingX = 0;
paddingY = 0;
check= wallType == WallType.LEFT_WALL ? lTObject : lBObject;
}
Log.info("WALL_CHECK", "TOP: " + top + "\n LEFT: " + left + "\n RIGHT: " + right + "\n BOTTOM: " + bottom);
Vector2 intersect = intersectVerticalLines(wallPoints[0], wallPoints[1], objectPoints[0], objectPoints[1]);
if(intersect == null)
return;
float correctX = gameObject.getX() - (check.x - intersect.x);
float correctY = gameObject.getY() - (check.y - intersect.y);
gameObject.moveToPosition(true, correctX, correctY, null);
//TODO smazat po otestovaní a refactoringu
if(lastIntersect == null){
lastIntersect = new Image(ApplicationAtlasManager.getInstance().getColorTexture(Color.RED));
addActor(lastIntersect);
}
lastIntersect.setBounds(intersect.x - 2.5f, intersect.y - 2.5f, 5f, 5f);
}
}
}
private Vector2 intersectVerticalLines(Vector2 p1, Vector2 p2, Vector2 q1, Vector2 q2){
float f1 = -(p2.y - p1.y) / (p1.x - p2.x);
float f2 = -(q2.y - q1.y) / (q1.x - q2.x);
float g1 = p1.y - f1 * p1.x;
float g2 = q1.y - f2 * q1.x;
if(f1 == f2)
return null;
if(p1.x == p2.x) return new Vector2(p1.x, f2 * p1.x + g2);
else if(q1.x == q2.x) return new Vector2(q1.x, f1 * q1.x + g1);
else{
Vector2 inter = new Vector2((g2-g1)/(f1 -f2),0);
inter.y = f1 * inter.x + g1;
return inter;
}
}
private boolean checkPointBetweenTwoLines(Vector2 A, Vector2 B, Vector2 C, Vector2 point){
float[] u = new float[]{-B.y + A.y, B.x - A.x, 0}; //line AB
float[] v = new float[]{-C.y + B.y, C.x - B.x, 0}; //line BX
float[] u = new float[]{-B.y + A.y, B.x - A.x, 0}; //general form line AB
float[] v = new float[]{-C.y + B.y, C.x - B.x, 0}; //general form line BX
u[2] = -u[0]*A.x-u[1]*A.y;
v[2] = -v[0]*B.x-v[1]*B.y;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment