Mam ogromny problem z fragmentem kodu i jestem załamany. Nawet nie wiem czy jest sens wrzucać tutaj bo to za dużo chyba powiązań w kodzie i tłumaczenia żeby ktoś ogarnął na szybko. Ale jestem w kropce, piszę grę sam więc muszę tak zapytać bo chyba sam sobie z tym nie poradzę. Ale jeśli ktoś by mi z tym pomógł to z przyjemnością postawiłbym browara.
Otóż sprawa wygląda tak, że jeśli nasza postać (chibi1) idzie poziomo lub pionowo po mapie to nie ma problemu. Problem jest kiedy chcemy iść na skos. Wtedy postać przegapia określone pole i idzie o jedno pole w bok dalej.
public class GameSurface extends SurfaceView implements SurfaceHolder.Callback {
int movingVectorX, movingVectorY;
double stepX, stepY, stepsX, stepsY, stepsXSkos, stepsYSkos;
List<Integer> pathX = new ArrayList<>(), pathY = new ArrayList<>(), movingVectorsX = new ArrayList<>(), movingVectorsY = new ArrayList<>();
@Override
public void draw(Canvas canvas) {
super.draw(canvas);
(...)
movementControl(canvas);
}
(...)
private void movementControl(Canvas canvas) {
stepX = Math.abs(aa - chibi1.getX() - (chibi1.getWidth() / 2));
stepY = Math.abs(bb - chibi1.getY() - (chibi1.getHeight() / 2));
//nie zmieniają się wektory, to źle
if (movingVectorX != 0 && movingVectorY == 0) {
stepsX = stepX / cellWidth;
stepX = 0;
}
if (movingVectorX == 0 && movingVectorY != 0)
stepsY = stepY / cellHeight;
if (movingVectorY != 0 && movingVectorX != 0) {
stepsXSkos = (stepX / (cellWidth*5));
stepsYSkos = (stepY / (cellHeight*4));
}
steps = (int) (stepsX + stepsY + stepsXSkos + stepsYSkos);
if (pathX.size() > 1 && pathY.size() > 1) {
if ((chibi1.getX() + (chibi1.getWidth() / 2) < pathX.get(steps)) && !drawerOn)
movingVectorX = 1;
if ((chibi1.getX() + (chibi1.getWidth() / 2) > pathX.get(steps)) && !drawerOn)
movingVectorX = -1;
if ((chibi1.getY() + (chibi1.getHeight() / 2) < pathY.get(steps)) && !drawerOn)
movingVectorY = 1;
if ((chibi1.getY() + (chibi1.getHeight() / 2) > pathY.get(steps)) && !drawerOn)
movingVectorY = -1;
}
if (steps < (pathY.size() - 1)) {
chibi1.setMovingVector(movingVectorsX.get(steps), movingVectorsY.get(steps));
}
if (steps >= (pathY.size() - 1)) {
if (chibi1 != null) chibi1.setMovingVector(0, 0);
if (selectedNode != enemyNode)
if (chibi1 != null)
chibi1.setX((cellWidth * ((chibi1.getX() / cellWidth))) + (cellWidth / 2) - (chibi1.getWidth() / 2));
if (selectedNode != enemyNode)
if (chibi1 != null)
chibi1.setY(((cellHeight) * ((chibi1.getY() / cellHeight))) + (cellHeight / 2) - (chibi1.getHeight() / 2));
movingVectorY = 0;
movingVectorX = 0;
steps = 0;
}
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
setValuesOnTapUp(e);
(...)
return true;
}
void setValuesOnTapUp(MotionEvent e) {
gs.aa = gs.chibi1.getX() + (gs.chibi1.getWidth() / 2);
gs.bb = gs.chibi1.getY() + (gs.chibi1.getHeight() / 2);
(...)
}
Obstawiam, że problem jest tutaj w metodzie movementControl():
if (movingVectorY != 0 && movingVectorX != 0) {
stepsXSkos = (stepX / (cellWidth*5));
stepsYSkos = (stepY / (cellHeight*4));
}
te steps skos są źle wyliczane bo nie wiem jaki wzór użyć do ich obliczenia...