5 Replies - 1752 Views - Last Post: 26 March 2012 - 10:29 AM Rate Topic: -----

#1 jc310xc  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 14-January 11

First TD in Java - creep movement

Posted 18 March 2012 - 12:14 PM

Hello,

I'm in the process of making my first tower defense game in java. I've got four classes so far: Main, Board, Level1, and Enemy.

I'm looking to start fairly small - just one level to begin with - so I have my Main class instantiating a Board, which in turn instantiates my Level1 class. Main handles my events. my Board class defines the board as an array of grid squares. Board also holds a couple of two-dimensional arrays:

isPath, which holds boolean values given by the Level1 class that describe where the path runs.
holdsTower, which holds boolean values for whether a tower is held on a given grid square.
board, which holds Rectangles of the dimensions of each square on the grid.

The way my events currently work are as follows:
When the mouse is pressed, I get the x and y coordinates of the mouse's position, check them against the rectangles array, which returns the index of the square clicked or -1, -1 if the position is outside of all of the squares.

I check against isPath to see if the square clicked is part of the path, if it is, it doesn't become a tower. if it isn't, I check to see if it already holds a tower. I remove the tower if it does hold a tower, and add the tower if it doesn't hold one.

For my Enemy class, I specify several variables relating to the creep: moveSpeed, maxHealth, currentHealth, xPosition, yPosition, atEnd, and direction. the Enemy implements Runnable, and has a method run, that keeps the creep moving so long as health is greater than 0 and the creep is not atEnd, or at the end of the maze.

My question is, how would I get the creep to follow the path? I can get the creep to start where the beginning of the maze is, but I'm sort of at a loss at how to implement a way to get the creep to turn where it should turn. My current idea is to have Level1 keep an array of the coordinates of each turn in the maze, but i'm not positive how I would do that.

Also, does the setup I've explained thus far sound like a decent start to the code for a TD? I don't have much experience programming games at all, so I wasn't positive how I should begin to code this. I know there are a few more things I'll need to have the basics for the game down, such as a tower class and a bullet class, but I just want to handle creeps at the moment. Eventually I want to implement other types of creeps and towers, of course, which will extend my basic tower and enemy classes. Is there anything I've missed that I need to handle so far?

Thanks a ton for reading and for any advice!

Is This A Good Question/Topic? 0
  • +

Replies To: First TD in Java - creep movement

#2 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 1
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: First TD in Java - creep movement

Posted 18 March 2012 - 03:43 PM

Need code! :)
Was This Post Helpful? 0
  • +
  • -

#3 jc310xc  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 14-January 11

Re: First TD in Java - creep movement

Posted 18 March 2012 - 06:21 PM

Like I said, this is my first attempt at a game, so while the code seems fairly neat to me, I'm sure it looks jumbled to everyone else, haha.

I decided to go for my idea of making the array of waypoints, which worked nicely to keep my creeps moving along the path, but now I'm having issues broadening this to even just two creeps at once. it slows up incredibly with more than one creep on the board at a time.

A couple other issues i'm having - I want the creeps' moveSpeed to be a pixel/second value. The formula I believe to be correct is (elapsedTimeSinceLastMove)/1000 * moveSpeed, but for some reason that value always returns zero, I suspect that because elapsedTimeSinceLastMove is usually a very low value, dividing it by 1000 yields a value so close to zero that the program just interprets it as zero.

My last problem is that the creep stops at the last turn it needs to make and doesn't continue to the end of the maze and off of the screen. I've looked all over the code, can't find the bug.

Main.java
package TDResources;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;

import TDEnemies.Enemy;

@SuppressWarnings("serial")
public class Main extends JFrame{
	//double buffering image
	Image dbImage;
	Graphics dbg;
	
	//dimensions for the window
	int screenWidth = 1000;
	int screenHeight = 800;
	Dimension screenDimension = new Dimension(screenWidth, screenHeight);
	
	//create a new Board, which gets level information from Level1, and sets up the path.
	static Board board = new Board();
	
	//scaffolding/debug message initializations
	String xMess = "xIndex = ";
	String yMess = "yIndex = ";
	String holdsTowerMess = "holdsTower = ";
	
	//mouse's x and y positions in the window
	int mouseX;
	int mouseY;
	
	//earlier attempt at an array of enemies. it didn't quite work...
	//static Enemy[] creep = new Enemy[10];
	//static Thread[] enemy = new Thread[10];
	
	//my single test creep. loads the starting x and y positions from Level1
	static Enemy creep = new Enemy(.1,100,board.level1.getStartingX(),board.level1.getStartingY());
	
	//set up all of the initial things. title, window size, etc. event listener, as well.
	public Main(){
		this.setTitle("Pete's First TD game!");
		this.setSize(screenDimension);
		this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
        this.addMouseListener(new MouseListener());
        
        /*My attempt to "stagger" the array of enemies so they didn't run the maze at once.
        long lastTime = System.currentTimeMillis();
		
		for(int i=0; i<creep.length;i++){
			long curTime = System.currentTimeMillis();
			while(curTime - lastTime <= 1000){
				curTime = System.currentTimeMillis();
			}
			creep[i] = new Enemy(.00002,100,board.level1.getStartingX(),board.level1.getStartingY());
			enemy[i] = new Thread(creep[i]);
			enemy[i].start();
			lastTime = curTime;
		}*/
        
        //starting up my test creep thread.
        Thread enemy = new Thread(creep);
        enemy.start();
	}
	
	//main method calls Main to begin everything
	public static void main(String[] args){
		@SuppressWarnings("unused")
		Main main = new Main();
	}

	//event handling
	public class MouseListener extends MouseAdapter{
		
		//later on mousePressed and mouseReleased will make sure the user is over the same thing when
		//the mouse is pressed and released.
		public void mousePressed(MouseEvent e){
			//Not quite sure why, but my board is off 50 pixels. I believe it's because of
			//the 50x50 offset when I paint the double buffer image.
			mouseX = e.getX() - 50;
			mouseY = e.getY() - 50;

			//scaffolding/debug code
			xMess = "xIndex = " + board.getXIndex(mouseX, mouseY);
			yMess = "yindex = " + board.getYIndex(mouseX,mouseY);
			
			//force a repaint for the user's input
			repaint();
		}
		
		public void mouseReleased(MouseEvent e){
			mouseX = e.getX() - 50;
			mouseY = e.getY() - 50;
			
			//find what square the user clicked 
			int xIndex = board.getXIndex(mouseX, mouseY);
			int yIndex = board.getYIndex(mouseX, mouseY);
			
			//click detection, check to see if the user's input was on a grid square
			if(xIndex != -1 && yIndex != -1){
				//if the board isn't a path, and it has indexes other than (-1,-1),
				//a tower can be placed.
				if(!board.getIsPath(xIndex, yIndex)){
					//check to see if there's a tower there already
					if(board.getHoldsTower(xIndex, yIndex)){
						//if there is, remove the tower.
						board.setHoldsTower(xIndex,yIndex,false);
					}else{
						//if not, place a tower.
						board.setHoldsTower(xIndex, yIndex, true);
					}
				}
				//scaffolding/debug
				holdsTowerMess = "holdsTower = " + board.getHoldsTower(xIndex, yIndex);
			}
			
			//scaffolding/debug
			xMess = "yTop = " + board.getYTop(xIndex, yIndex);
			yMess = "yTop = " + board.getYTop(xIndex,yIndex);
			
			repaint();
		}
	}
	
	@Override
	public void paint(Graphics g){
		//double buffering
        dbImage = createImage(getWidth(), getHeight());
        dbg = dbImage.getGraphics();
        draw(dbg);
        g.drawImage(dbImage, 50, 50, this);
    }
    public void draw(Graphics g){
    	//draw the rectangles representing empty tiles, towers, and path tiles
    	for(int i = 0; i < board.board.length; i++){
			for(int j = 0; j < board.board[i].length; j++){
				//get the individual square's information
				int xTop = board.getXTop(i,j);
				int yTop = board.getYTop(i,j);
				
				int width = board.getSquareWidth();
				int height = board.getSquareHeight();
				
				//check what type of square this is:
				//tower squares are colored red
				if(board.getHoldsTower(i,j)){
					g.setColor(Color.red);
					g.fillRect(xTop, yTop, width, height);
				//path squares are brown.
				}else if(board.getIsPath(i,j)){
					g.setColor(new Color(255,204,102));
					g.fillRect(xTop, yTop, width, height);
				//otherwise, it's an empty field square, which is green.
				}else{
					g.setColor(Color.green);
					g.fillRect(xTop, yTop, width, height);
				}
			}
		}
    	//display the grid and other information.
    	g.setColor(Color.BLACK);
    	g.drawLine(board.getXTop(0,0), board.getYTop(0,0), board.getXTop(0,0), board.boardHeight+board.getYTop(0,0));
    	g.drawLine(board.getXTop(0,0), board.getYTop(0,0), board.boardWidth+board.getXTop(0,0), board.getYTop(0,0));
    	for(int i=0; i<board.board.length; i++){
			int xLevel = (i+2)*board.board[i][0].width;
			g.drawLine(xLevel, board.getYTop(0,0), xLevel, board.boardWidth+board.getYTop(0,0));
		}
		
		for(int i=0; i<board.board[0].length; i++){
			int yLevel = (i+2)*board.board[0][i].height;
			g.drawLine(board.getXTop(0,0), yLevel, board.boardHeight+board.getXTop(0,0), yLevel);
		}
		
		//draw scaffolding/debug messages
		g.drawString(xMess, 710, 200);
		g.drawString("creep x = "+ creep.getXPosition(), 710, 225);
		g.drawString("creep y = "+ creep.getYPosition(), 710, 250);
		g.drawString(yMess,710,400);
		g.drawString(holdsTowerMess, 710, 300);
		g.drawString("curTime - lastTime = " + (creep.getCurTime() - creep.getLastTime()), 710, 325);
		g.drawString("curTime = " + creep.getCurTime(), 710, 350);
		g.drawString("direction = " + creep.getDirection(), 710, 375);
		g.drawString("current time = " + System.currentTimeMillis(), 710,425);
		g.drawString("numTurns = " + creep.getNumTurns(),710,450);
		
		//Draw enemies. Get each enemy's location, and draw it.
		g.setColor(Color.blue);
		/*for(int i = 0; i< creep.length;i++){
			g.fillOval((int)creep[i].getXPosition(),(int)creep[i].getYPosition(), 15, 15);
		}*/
		g.fillOval((int)creep.getXPosition(),(int)creep.getYPosition(),15,15);
        
        repaint();
    }
}



Board.java
package TDResources;

import TDLevels.Level1;

import java.awt.*;

public class Board {
	
	//Assuming a board height/width of 600 px.
	int boardWidth = 600;
	int boardHeight = 600;
	
	//create a new level, load its information
	Level1 level1 = new Level1();
	
	int boardRows = level1.getGridWidth();
	int boardCols = level1.getGridHeight();
	
	//width of the squares is the width/height of the board divided by the rows/columns of the grid
	int gridSquareWidth = boardWidth/boardRows;
	int gridSquareHeight = boardHeight/boardCols;
	
	//2-d arrays that hold the virtual representation of the board
	Rectangle board[][] = new Rectangle[boardRows][boardCols];
	boolean holdsTower[][] = new boolean[boardRows][boardCols];
	boolean isPath[][] = new boolean[boardRows][boardCols];

	public Board(){
		//get initial coords of the top left square
		int xTop = gridSquareWidth;
		int yTop = gridSquareHeight;
		
		//traverse the array, assigning each square its position on the grid, initializing the
		//holdsTower and isPath arrays along the way.
		for(int i=0;i<board.length;i++){
			for(int j=0;j<board[i].length;j++){
				board[i][j] = new Rectangle(xTop,yTop,gridSquareWidth,gridSquareHeight);
				holdsTower[i][j] = false;
				isPath[i][j] = level1.getPath(i,j);
				//if the squares go outside of the grid, reset the X position and increment the y
				//to begin a new row
				if(xTop >= boardWidth){
					xTop = gridSquareWidth;
					yTop += gridSquareHeight;
				}else{
					xTop += gridSquareWidth;
				}
			}
		}
	}
	
	//setters/getters
	public void setHoldsTower(int x,int y,boolean value){
		holdsTower[x][y] = value;
	}
	
	public boolean getHoldsTower(int x, int y){
		return holdsTower[x][y];
	}
	
	//getX/YIndex methods return -1 if the mouse position is outside of the bounds of the array
	public int getXIndex(int x, int y){
		for(int i=0;i<board.length;i++){
			for(int j=0; j<board[i].length;j++){
				if(board[i][j].x <= x && board[i][j].x + board[i][j].width >= x && board[i][j].y <= y && board[i][j].y + board[i][j].height >= y){
					return i;
				}
			}
		}
		return -1;
	}
	
	public int getYIndex(int x, int y){
		for(int i=0;i<board.length;i++){
			for(int j=0; j<board[i].length;j++){
				if(board[i][j].x <= x && board[i][j].x + board[i][j].width >= x && board[i][j].y <= y && board[i][j].y + board[i][j].height >= y){
					return j;
				}
			}
		}
		return -1;
	}
	
	public int getYTop(int x, int y){
		return board[x][y].y;
	}
	
	public int getXTop(int x, int y){
		return board[x][y].x;
	}
	
	public int getSquareWidth(){
		return gridSquareWidth;
	}
	
	public int getSquareHeight(){
		return gridSquareHeight;
	}
	
	public boolean getIsPath(int x, int y){
		return isPath[x][y];
	}
}



Level1.java
package TDLevels;

public class Level1 {
	//specify how many squares in the grid
	int gridWidth = 20;
	int gridHeight = 20;
	//specify the number of turns this level makes
	int numTurns = 20;
	//specify the starting (x,y) coords
	int startingX = -108;
	int startingY = 308;
	//the path the creeps walk
	boolean[][] path = new boolean[gridWidth][gridHeight];
	//the coordinates of each waypoint
	int[] xTurns = new int[numTurns];
	int[] yTurns = new int[numTurns];
	//the direction to turn after arriving at each waypoint.
	int[] direction = new int[numTurns];
	
	//constructor defines which tiles are path tiles
	public Level1(){
		path[0][0] = false;
		path[0][1] = false;
		path[0][2] = false;
		path[0][3] = false;
		path[0][4] = false;
		path[0][5] = false;
		path[0][6] = false;
		path[0][7] = false;
		path[0][8] = false;
		path[0][9] = false;
		path[0][10] = false;
		path[0][11] = false;
		path[0][12] = false;
		path[0][13] = false;
		path[0][14] = false;
		path[0][15] = false;
		path[0][16] = false;
		path[0][17] = false;
		path[0][18] = false;
		path[0][19] = false;
		
		path[1][0] = false;
		path[1][1] = false;
		path[1][2] = false;
		path[1][3] = false;
		path[1][4] = false;
		path[1][5] = false;
		path[1][6] = false;
		path[1][7] = false;
		path[1][8] = false;
		path[1][9] = false;
		path[1][10] = false;
		path[1][11] = false;
		path[1][12] = false;
		path[1][13] = false;
		path[1][14] = false;
		path[1][15] = false;
		path[1][16] = false;
		path[1][17] = false;
		path[1][18] = false;
		path[1][19] = false;
		
		path[2][0] = false;
		path[2][1] = false;
		path[2][2] = true;
		path[2][3] = true;
		path[2][4] = true;
		path[2][5] = true;
		path[2][6] = true;
		path[2][7] = true;
		path[2][8] = true;
		path[2][9] = true;
		path[2][10] = true;
		path[2][11] = false;
		path[2][12] = false;
		path[2][13] = true;
		path[2][14] = true;
		path[2][15] = true;
		path[2][16] = true;
		path[2][17] = true;
		path[2][18] = true;
		path[2][19] = false;
		
		path[3][0] = false;
		path[3][1] = false;
		path[3][2] = true;
		path[3][3] = false;
		path[3][4] = false;
		path[3][5] = false;
		path[3][6] = false;
		path[3][7] = false;
		path[3][8] = false;
		path[3][9] = false;
		path[3][10] = true;
		path[3][11] = false;
		path[3][12] = false;
		path[3][13] = true;
		path[3][14] = false;
		path[3][15] = false;
		path[3][16] = false;
		path[3][17] = false;
		path[3][18] = true;
		path[3][19] = false;
		
		path[4][0] = false;
		path[4][1] = false;
		path[4][2] = true;
		path[4][3] = false;
		path[4][4] = false;
		path[4][5] = false;
		path[4][6] = false;
		path[4][7] = false;
		path[4][8] = false;
		path[4][9] = false;
		path[4][10] = true;
		path[4][11] = false;
		path[4][12] = false;
		path[4][13] = true;
		path[4][14] = false;
		path[4][15] = false;
		path[4][16] = false;
		path[4][17] = false;
		path[4][18] = true;
		path[4][19] = false;
		
		path[5][0] = false;
		path[5][1] = false;
		path[5][2] = true;
		path[5][3] = false;
		path[5][4] = false;
		path[5][5] = false;
		path[5][6] = false;
		path[5][7] = false;
		path[5][8] = false;
		path[5][9] = false;
		path[5][10] = true;
		path[5][11] = false;
		path[5][12] = false;
		path[5][13] = true;
		path[5][14] = false;
		path[5][15] = false;
		path[5][16] = false;
		path[5][17] = false;
		path[5][18] = true;
		path[5][19] = false;
		
		path[6][0] = false;
		path[6][1] = false;
		path[6][2] = true;
		path[6][3] = false;
		path[6][4] = false;
		path[6][5] = false;
		path[6][6] = false;
		path[6][7] = false;
		path[6][8] = false;
		path[6][9] = false;
		path[6][10] = true;
		path[6][11] = false;
		path[6][12] = false;
		path[6][13] = true;
		path[6][14] = false;
		path[6][15] = false;
		path[6][16] = false;
		path[6][17] = false;
		path[6][18] = true;
		path[6][19] = false;
		
		path[7][0] = false;
		path[7][1] = false;
		path[7][2] = true;
		path[7][3] = false;
		path[7][4] = false;
		path[7][5] = false;
		path[7][6] = false;
		path[7][7] = false;
		path[7][8] = false;
		path[7][9] = false;
		path[7][10] = true;
		path[7][11] = false;
		path[7][12] = false;
		path[7][13] = true;
		path[7][14] = false;
		path[7][15] = false;
		path[7][16] = false;
		path[7][17] = false;
		path[7][18] = true;
		path[7][19] = false;
		
		path[8][0] = false;
		path[8][1] = false;
		path[8][2] = true;
		path[8][3] = false;
		path[8][4] = false;
		path[8][5] = false;
		path[8][6] = false;
		path[8][7] = false;
		path[8][8] = false;
		path[8][9] = false;
		path[8][10] = true;
		path[8][11] = false;
		path[8][12] = false;
		path[8][13] = true;
		path[8][14] = false;
		path[8][15] = false;
		path[8][16] = false;
		path[8][17] = false;
		path[8][18] = true;
		path[8][19] = false;
		
		path[9][0] = true;
		path[9][1] = true;
		path[9][2] = true;
		path[9][3] = false;
		path[9][4] = false;
		path[9][5] = false;
		path[9][6] = false;
		path[9][7] = false;
		path[9][8] = false;
		path[9][9] = false;
		path[9][10] = true;
		path[9][11] = false;
		path[9][12] = false;
		path[9][13] = true;
		path[9][14] = false;
		path[9][15] = false;
		path[9][16] = false;
		path[9][17] = false;
		path[9][18] = true;
		path[9][19] = false;
		
		path[10][0] = false;
		path[10][1] = false;
		path[10][2] = false;
		path[10][3] = false;
		path[10][4] = false;
		path[10][5] = false;
		path[10][6] = false;
		path[10][7] = false;
		path[10][8] = false;
		path[10][9] = false;
		path[10][10] = true;
		path[10][11] = false;
		path[10][12] = false;
		path[10][13] = true;
		path[10][14] = false;
		path[10][15] = false;
		path[10][16] = true;
		path[10][17] = true;
		path[10][18] = true;
		path[10][19] = false;
		
		path[11][0] = false;
		path[11][1] = false;
		path[11][2] = false;
		path[11][3] = false;
		path[11][4] = false;
		path[11][5] = false;
		path[11][6] = false;
		path[11][7] = false;
		path[11][8] = false;
		path[11][9] = false;
		path[11][10] = true;
		path[11][11] = false;
		path[11][12] = false;
		path[11][13] = true;
		path[11][14] = false;
		path[11][15] = false;
		path[11][16] = true;
		path[11][17] = false;
		path[11][18] = false;
		path[11][19] = false;
		
		path[12][0] = false;
		path[12][1] = false;
		path[12][2] = true;
		path[12][3] = true;
		path[12][4] = true;
		path[12][5] = true;
		path[12][6] = true;
		path[12][7] = true;
		path[12][8] = true;
		path[12][9] = true;
		path[12][10] = true;
		path[12][11] = false;
		path[12][12] = false;
		path[12][13] = true;
		path[12][14] = false;
		path[12][15] = false;
		path[12][16] = true;
		path[12][17] = false;
		path[12][18] = false;
		path[12][19] = false;
		
		path[13][0] = false;
		path[13][1] = false;
		path[13][2] = true;
		path[13][3] = false;
		path[13][4] = false;
		path[13][5] = false;
		path[13][6] = false;
		path[13][7] = false;
		path[13][8] = false;
		path[13][9] = false;
		path[13][10] = false;
		path[13][11] = false;
		path[13][12] = false;
		path[13][13] = true;
		path[13][14] = false;
		path[13][15] = false;
		path[13][16] = true;
		path[13][17] = false;
		path[13][18] = false;
		path[13][19] = false;
		
		path[14][0] = false;
		path[14][1] = false;
		path[14][2] = true;
		path[14][3] = false;
		path[14][4] = false;
		path[14][5] = true;
		path[14][6] = true;
		path[14][7] = true;
		path[14][8] = false;
		path[14][9] = true;
		path[14][10] = true;
		path[14][11] = true;
		path[14][12] = false;
		path[14][13] = true;
		path[14][14] = false;
		path[14][15] = false;
		path[14][16] = true;
		path[14][17] = false;
		path[14][18] = false;
		path[14][19] = false;
		
		path[15][0] = false;
		path[15][1] = false;
		path[15][2] = true;
		path[15][3] = false;
		path[15][4] = false;
		path[15][5] = true;
		path[15][6] = false;
		path[15][7] = true;
		path[15][8] = false;
		path[15][9] = true;
		path[15][10] = false;
		path[15][11] = true;
		path[15][12] = false;
		path[15][13] = true;
		path[15][14] = false;
		path[15][15] = false;
		path[15][16] = true;
		path[15][17] = false;
		path[15][18] = false;
		path[15][19] = false;
		
		path[16][0] = false;
		path[16][1] = false;
		path[16][2] = true;
		path[16][3] = false;
		path[16][4] = false;
		path[16][5] = true;
		path[16][6] = false;
		path[16][7] = true;
		path[16][8] = false;
		path[16][9] = true;
		path[16][10] = false;
		path[16][11] = true;
		path[16][12] = false;
		path[16][13] = true;
		path[16][14] = false;
		path[16][15] = false;
		path[16][16] = true;
		path[16][17] = false;
		path[16][18] = false;
		path[16][19] = false;
		
		path[17][0] = false;
		path[17][1] = false;
		path[17][2] = true;
		path[17][3] = true;
		path[17][4] = true;
		path[17][5] = true;
		path[17][6] = false;
		path[17][7] = true;
		path[17][8] = true;
		path[17][9] = true;
		path[17][10] = false;
		path[17][11] = true;
		path[17][12] = true;
		path[17][13] = true;
		path[17][14] = false;
		path[17][15] = false;
		path[17][16] = true;
		path[17][17] = true;
		path[17][18] = true;
		path[17][19] = true;
		
		path[18][0] = false;
		path[18][1] = false;
		path[18][2] = false;
		path[18][3] = false;
		path[18][4] = false;
		path[18][5] = false;
		path[18][6] = false;
		path[18][7] = false;
		path[18][8] = false;
		path[18][9] = false;
		path[18][10] = false;
		path[18][11] = false;
		path[18][12] = false;
		path[18][13] = false;
		path[18][14] = false;
		path[18][15] = false;
		path[18][16] = false;
		path[18][17] = false;
		path[18][18] = false;
		path[18][19] = false;
		
		path[19][0] = false;
		path[19][1] = false;
		path[19][2] = false;
		path[19][3] = false;
		path[19][4] = false;
		path[19][5] = false;
		path[19][6] = false;
		path[19][7] = false;
		path[19][8] = false;
		path[19][9] = false;
		path[19][10] = false;
		path[19][11] = false;
		path[19][12] = false;
		path[19][13] = false;
		path[19][14] = false;
		path[19][15] = false;
		path[19][16] = false;
		path[19][17] = false;
		path[19][18] = false;
		path[19][19] = false;
		
		//The xTurns and yTurns array operate under the assumption that the grid is 600px x 600px.
		//initialize the waypoint/direction arrays
		xTurns[0] = 98;
		yTurns[0] = 308;
		direction[0] = 2;
		
		xTurns[1] = 98;
		yTurns[1] = 98;
		direction[1] = 1;
		
		xTurns[2] = 338;
		yTurns[2] = 98;
		direction[2] = 4;
		
		xTurns[3] = 338;
		yTurns[3] = 398;
		direction[3] = 3;
		
		xTurns[4] = 98;
		yTurns[4] = 398;
		direction[4] = 4;
		
		xTurns[5] = 98;
		yTurns[5] = 548;
		direction[5] = 1;
		
		xTurns[6] = 188;
		yTurns[6] = 548;
		direction[6] = 2;
		
		xTurns[7] = 188;
		yTurns[7] = 458;
		direction[7] = 1;
		
		xTurns[8] = 248;
		yTurns[8] = 458;
		direction[8] = 4;
		
		xTurns[9] = 248;
		yTurns[9] = 548;
		direction[9] = 1;
		
		xTurns[10] = 308;
		yTurns[10] = 548;
		direction[10] = 2;
		
		xTurns[11] = 308;
		yTurns[11] = 458;
		direction[11] = 1;
		
		xTurns[12] = 368;
		yTurns[12] = 458;
		direction[12] = 4;
		
		xTurns[13] = 368;
		yTurns[13] = 548;
		direction[13] = 1;
		
		xTurns[14] = 428;
		yTurns[14] = 548;
		direction[14] = 2;
		
		xTurns[15] = 428;
		yTurns[15] = 98;
		direction[15] = 1;
		
		xTurns[16] = 578;
		yTurns[16] = 98;
		direction[16] = 4;
		
		xTurns[17] = 578;
		yTurns[17] = 338;
		direction[17] = 3;
		
		xTurns[18] = 518;
		yTurns[18] = 338;
		direction[18] = 4;
		
		xTurns[19] = 518;
		yTurns[19] = 548;
		direction[19] = 1;
	}
	
	//getters
	
	public int getGridWidth(){
		return gridWidth;
	}
	
	public int getGridHeight(){
		return gridHeight;
	}
	
	public boolean getPath(int x, int y){
		return path[x][y];
	}
	
	public int getXTurns(int x){
		return xTurns[x];
	}
	
	public int getYTurns(int x){
		return yTurns[x];
	}
	
	public int getDirection(int x){
		return direction[x];
	}
	
	public int getNumTurns(){
		return numTurns;
	}
	
	public int getStartingX(){
		return startingX;
	}
	public int getStartingY(){
		return startingY;
	}
}



Enemy.java
package TDEnemies;

import TDLevels.Level1;

public class Enemy implements Runnable{
	//moveSpeed is pix/sec
	private double moveSpeed;
	//creep's max health
	private int maxHealth;
	//creep's current health
	private int health;
	//creep's x position
	private double xPosition;
	//creep's y position
	private double yPosition;
	//if the creep has reached the end of the path
	private boolean atEnd;
	//the creep's direction
	private int direction;
	//number of times this creep has turned
	private int numTurns;
	//current time, used in position calculations
	private long curTime;
	//last time measured before curTime, used in position calculations
	private long lastTime;
	//distance the next move is set to be
	private double distance;
	
	//get level1's information to check where to move to
	Level1 level1 = new Level1();
	
	//distance from end may need to be implemented in an "Enemy Handler" class.
	//private int distanceFromEnd;
	
	//initialize the variables
	public Enemy(double moveSpeed, int maxHealth, int xPosition, int yPosition){
		this.moveSpeed = moveSpeed;
		this.maxHealth = maxHealth;
		this.health = this.maxHealth;
		this.xPosition = xPosition;
		this.yPosition = yPosition;
		this.atEnd = false;
		this.direction = 1;
		this.numTurns = 0;
		this.lastTime = System.currentTimeMillis();
		this.curTime = System.currentTimeMillis();
	}
	
	//while not dead and not at the finish, move.
	public void run(){
		while(!atEnd && health > 0){
			move();
		}
	}
	
	public void move(){
		//I want move speed to be pix/second, so take the time elapsed since the last move, multiply
		//by moveSpeed, and divide by a thousand. Currently not working as i want it, so i removed
		//the problem for the moment, which is dividing by a thousand.
		curTime = System.currentTimeMillis();
		distance = (curTime - lastTime) * moveSpeed;
		lastTime = curTime;
		
		//direction 1 is right
		if(direction == 1){
			//if the creep reaches the waypoint, set the coords to the waypoint, change to the direction
			//the creep needs to face, and update the number of turns made.
			if(xPosition >= level1.getXTurns(numTurns) && yPosition >= level1.getYTurns(numTurns)){
				direction = level1.getDirection(numTurns);
				xPosition = level1.getXTurns(numTurns);
				yPosition = level1.getYTurns(numTurns);
				numTurns++;
			}else{
				xPosition += distance;				
			}
		//direction 2 is up
		}else if(direction == 2){
			//if the creep reaches the waypoint, set the coords to the waypoint, change to the direction
			//the creep needs to face, and update the number of turns made.
			if(xPosition >= level1.getXTurns(numTurns) && yPosition <= level1.getYTurns(numTurns)){				
				direction = level1.getDirection(numTurns);
				xPosition = level1.getXTurns(numTurns);
				yPosition = level1.getYTurns(numTurns);
				numTurns++;
			}else{
				yPosition -= distance;				
			}
		//direction 3 is left
		}else if(direction == 3){
			//if the creep reaches the waypoint, set the coords to the waypoint, change to the direction
			//the creep needs to face, and update the number of turns made.
			if(xPosition <= level1.getXTurns(numTurns) && (yPosition >= level1.getYTurns(numTurns)-1.5 && yPosition <= level1.getYTurns(numTurns)+1.5)){				
				direction = level1.getDirection(numTurns);
				xPosition = level1.getXTurns(numTurns);
				yPosition = level1.getYTurns(numTurns);
				numTurns++;
			}else{
				xPosition -= distance;				
			}
		//direction 4 is down
		}else{
			//if the creep reaches the waypoint, set the coords to the waypoint, change to the direction
			//the creep needs to face, and update the number of turns made.
			if((xPosition >= level1.getXTurns(numTurns)-1.5 && xPosition <= level1.getXTurns(numTurns)+ 1.5) && yPosition >= level1.getYTurns(numTurns)){				
				direction = level1.getDirection(numTurns);
				xPosition = level1.getXTurns(numTurns);
				yPosition = level1.getYTurns(numTurns);
				numTurns++;
			}else{
				yPosition += distance;				
			}
		}
	}
	
	//setters/getters
	
	public double getMoveSpeed(){
		return moveSpeed;
	}
	public int getMaxHealth(){
		return maxHealth;
	}
	public int getHealth(){
		return health;
	}
	public double getXPosition(){
		return xPosition;
	}
	public double getYPosition(){
		return yPosition;
	}
	public boolean getAtEnd(){
		return atEnd;
	}
	
	public long getCurTime(){
		return curTime;
	}
	
	public long getLastTime(){
		return lastTime;
	}
	
	public int getNumTurns(){
		return numTurns;
	}
	
	public double getDistance(){
		return distance;
	}
	
	public int getDirection(){
		return direction;
	}
	
	public void setMoveSpeed(int newSpeed){
		moveSpeed = newSpeed;
	}
	public void setMaxHealth(int health){
		maxHealth = health;
	}
	public void setHealth(int newHealth){
		health = newHealth;
	}
	public void setXPosition(int xPos){
		xPosition = xPos;
	}
	public void setYPosition(int yPos){
		yPosition = yPos;
	}
	public void setAtEnd(boolean bool){
		atEnd = bool;
	}
	
}



Thanks again for your time and advice!
Was This Post Helpful? 0
  • +
  • -

#4 jc310xc  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 14-January 11

Re: First TD in Java - creep movement

Posted 19 March 2012 - 07:58 AM

So i figured out the problem with the creep stopping at the last turn - i just didn't give it another waypoint to go to. Fixed! Still having issues with getting multiple creeps to move without the program slowing down majorly.
Was This Post Helpful? 1
  • +
  • -

#5 Mylo  Icon User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • Posts: 747
  • Joined: 11-October 11

Re: First TD in Java - creep movement

Posted 20 March 2012 - 02:57 AM

Although I'm quite unsure, I think the slow down may be the result of you creating a new thread for each monster. Then you have a loop in the run method so move is being called many times.

I suggest you have one thread (or timer) which cycles through each monster and calls the move() method every x ms. Not in a loop.
Was This Post Helpful? 0
  • +
  • -

#6 jc310xc  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 14-January 11

Re: First TD in Java - creep movement

Posted 26 March 2012 - 10:29 AM

That worked perfectly, thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1