1 Replies - 230 Views - Last Post: 16 May 2014 - 01:55 AM Rate Topic: -----

#1 SupremeGrandRuler  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 10-May 14

Calculating movement help! (Advance Wars / FF Tactics like movemen

Posted 15 May 2014 - 07:28 PM

Alright, so I'm having problems with lines 11 - 55.
I'm trying to calculate the possible tiles the clicked unit can be moved on and display it to the player.

The field is a 9 (columns) by 5 (rows) grid. A unit with can move tiles horizontally or vertically based on its movement.
Example: (unit = o; possible tiles = x) (assuming movement of 2).

[ ][ ][ ][ ][x][ ][ ][ ][ ]
[ ][ ][ ][x][x][x][ ][ ][ ]
[ ][ ][x][x][o][x][x][ ][ ]
[ ][ ][ ][x][x][x][][ ][ ]
[ ][ ][ ][ ][x][ ][ ][ ][ ]

I tried to calculate movement by finding out what column the unit is in, subtracting the column it wants to go to, multiplying the difference by *-1 if the difference is less than 0. Then, doing the same with rows and adding the difference of columns to the difference of rows and comparing it to the unit's movement.
Unfortunately, with my code, it seems to move in any directions.


	@Override
	public void mouseClicked(MouseEvent e) {
		if (Main.cards.size() > 0) {
			mainloop: for (int i = 0; i < Main.cards.size(); i++) {
				Card c = (Card) Main.cards.get(i);
				int startingColumn = 0;
				int startingRow = 0;
				int endingColumn = 0;
				int endingRow = 0;

				// If card is not selected:
				if (Card.cardSelected != c || Card.cardSelected == null) {
					if (new Rectangle(c.x, c.y, Main.maxTileSize, Main.maxTileSize).contains(e.getX(), e.getY())) {
						Card.cardSelected = c;

						// Calculate valid moves:
						validfromloop: for (int j = 0; j < Main.BOARD_COLUMNS; j++) {
							for (int k = 0; k < Main.BOARD_ROWS; k++) {
								if (AssetLoader.TILE[j][k].contains(Card.cardSelected.x, Card.cardSelected.y)) {
									startingColumn = j;
									startingRow = k;

									System.out.println("VALID--Starting column: " + startingColumn + " Starting Row: " + startingRow);

									break validfromloop;
								}
							}
						}

						System.out.println("validtoloop");
						validtoloop: for (int j = 0; j < Main.BOARD_COLUMNS; j++) {
							for (int k = 0; k < Main.BOARD_ROWS; k++) {
								if (startingColumn - j < 0) {
									endingColumn = (startingColumn - j) * -1;

									System.out.println("Ending column: " + endingColumn);
								}
								if (startingRow - k < 0) {
									endingRow = (startingRow - k) * -1;

									System.out.println("Ending row: " + endingRow);
									System.out.println("---------------------");
								}
								if (endingColumn + endingRow <= Card.cardSelected.mvt) {
									AssetLoader.VALID_MOVE[j][k] = true;
									
									System.out.println("Final: " + endingColumn + endingRow);
								}
							}
						}

						Main.frame.repaint();

						System.out.println("Card selected: " + Card.cardSelected);

						break mainloop;

					}
					// If card is selected:
				} else if (Card.cardSelected == c && Card.cardSelected != null) {

					for (int l = 0; l < Main.BOARD_COLUMNS; l++) {
						for (int m = 0; m < Main.BOARD_ROWS; m++) {
							AssetLoader.VALID_MOVE[l][m] = false;
						}
					}

					fromloop: for (int j = 0; j < Main.BOARD_COLUMNS; j++) {
						for (int k = 0; k < Main.BOARD_ROWS; k++) {

							System.out.println("loop1");

							if (AssetLoader.TILE[j][k].contains(Card.cardSelected.x, Card.cardSelected.y)) {
								startingColumn = j;
								startingRow = k;

								System.out.println("loop1a");

								break fromloop;
							}
						}
					}

					toloop: for (int j = 0; j < Main.BOARD_COLUMNS; j++) {
						for (int k = 0; k < Main.BOARD_ROWS; k++) {
							if (AssetLoader.TILE[j][k].contains(e.getX(), e.getY())) {
								endingColumn = j;
								endingRow = k;

								System.out.println("loop2");

								if ((startingColumn + startingRow) - (endingColumn + endingRow) <= Card.cardSelected.mvt && (startingColumn + startingRow) - (endingColumn + endingRow) >= -Card.cardSelected.mvt) {
									Card.cardSelected.x = (int) AssetLoader.TILE[j][k].getX();
									Card.cardSelected.y = (int) AssetLoader.TILE[j][k].getY();
									Card.cardSelected = null;
									;

									Main.frame.repaint();

									// System.out.println("Card selected: " +
									// c.cardSelected + " Selection: " +
									// Card.classCardSelected);
									System.out.println("Card x / y: " + c.x + " / " + c.y);
									System.out.println("Click x / y: " + e.getX() + " / " + e.getY());
									System.out.println("Unit moving from " + startingColumn + startingRow + " to " + endingColumn + endingRow);

									break mainloop;
								} else {
									Card.cardSelected = null;

									Main.frame.repaint();

									System.out.println("Unit cannot move so far.");

									break toloop;
								}
							}
						}
					}
				}
			}
		}
	}


Is This A Good Question/Topic? 0
  • +

Replies To: Calculating movement help! (Advance Wars / FF Tactics like movemen

#2 SupremeGrandRuler  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 10-May 14

Re: Calculating movement help! (Advance Wars / FF Tactics like movemen

Posted 16 May 2014 - 01:55 AM

Solved.
Figured the answer out. It's because the the 'to' column is only subtracted from the 'from' column if the result is less than 0. Otherwise, no operation takes place. Same with rows.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1