School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,136 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,935 people online right now. Registration is fast and FREE... Join Now!




A star algorithm for 2D java game

 

A star algorithm for 2D java game

General_9

11 Oct, 2009 - 09:25 AM
Post #1

New D.I.C Head
*

Joined: 11 Oct, 2009
Posts: 1

We are developing a game as a project, can someone assist us with implementing the A star algorithm for our enemies. We have attached our source code.


CODE

import java.util.Vector;
import java.awt.geom.*;
import java.awt.event.*;
import java.awt.*;
import java.nio.ByteBuffer;


import GameEngine.Game;
import GameEngine.GameTexture;
import GameEngine.GameFont;
import GameEngine.GameObject;


// ================================================================================
==================
// ================================================================================
==================

public class SurvivalGame extends Game
{
    // Offset of the screen
    private Point2D.Float offset = new Point2D.Float(0,0);
    private int trigger = 0;
    private boolean alive = true;
    private boolean enemyAlive = true;
    
    
    // A Collection of GameObjects in the world that will be used with the collision detection system
    private Vector<GameObject> objects = new Vector<GameObject>();
    
    // Grid GameObjects
    private GameObject [] [] gridTile;
    
    // The cooldown of the gun (set this to 0 for a cool effect :> )
    private int cooldown = 10;
    private int cooldownTimer = 0;
    
    // Important GameObjects
    private PlayerObject player; // the player
    private HealthObject health; //health pack

    //private PlayerObject enemy;
    private Vector<PlayerObject> enemy = new Vector<PlayerObject>();
    //Textures that will be used
    private GameTexture bulletTexture;
  
    
    //GameFonts that will be used
    private GameFont arial, serif;
    
    // The positin of the mouse
    private Point2D.Float mousePos = new Point2D.Float (0,0);
    
    // a counter for how far the mousewheel has been moved (just an example)
    private int mouseWheelTick = 0;

  
    // Information for the random line at the bottom of the screen
    Point2D.Float [] linePositions = {new Point2D.Float(0,0), new Point2D.Float(100,100)};
    float [][] lineColours = {{1.0f,1.0f,1.0f,1.0f},{1.0f,0.0f,0.0f,1.0f}};
    
    // ================================================================================
==================
    
    public SurvivalGame (int GFPS) {
        super(GFPS);
    }

    // ================================================================================
==================
    
    public void initStep(ResourceLoader loader) {

        //loading sound objects
        Midi midiObj = new Midi();
        midiObj.run();

      
        //Loading up some fonts
        arial = loader.loadFont(  new Font("Arial", Font.ITALIC, 48) );
        serif = loader.loadFont(  new Font("Serif", Font.PLAIN, 48) );
        
        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        

        //Loading up our textures
        GameTexture healthTexture = loader.loadTexture("Textures/health.png");
        GameTexture rockTexture = loader.loadTexture("Textures/rock.png");
        GameTexture grassTexture = loader.loadTexture("Textures/gray1.png");
        bulletTexture = loader.loadTexture("Textures/bullet.png");
        
        
        int gridSize = 12;
        
        
      
        //creating the health packs to boost player's health
        for (int i = 0; i < 6; i++ ) {
          
          float x = (float) ((Math.random()*(gridSize-4)+2)*grassTexture.getWidth());
          float y = (float) ((Math.random()*(gridSize-4)+2)*grassTexture.getHeight());
          
              health = new HealthObject(x, y);
              health.addTexture(healthTexture, 0, 0);
              objects.add(health);
        }
        
        
        // creating the floor objects
        gridTile = new GameObject[gridSize][gridSize];
        for (int i = 0; i < gridSize; i++ ) {
            for (int j = 0; j < gridSize; j++ ) {
                gridTile[i][j] = new GameObject(grassTexture.getWidth()*i,grassTexture.getHeight()*j);
                gridTile[i][j].addTexture(grassTexture, 0, 0);
            }
        }
        
        
        // Creating wall objects
        for (int i = 0; i < grassTexture.getWidth()*gridSize; i += rockTexture.getWidth()) {
            WallObject go = new WallObject(i, 0);
            go.addTexture(rockTexture, 0, 0);
            objects.add(go);
            
            go = new WallObject(i, grassTexture.getHeight()*(gridSize-1));
            go.addTexture(rockTexture, 0, 0);
            objects.add(go);
        }
        for (int i = grassTexture.getHeight(); i < grassTexture.getHeight()*(gridSize-1); i += rockTexture.getHeight()) {
            WallObject go = new WallObject(0, i);
            go.addTexture(rockTexture, 0, 0);
            objects.add(go);
            
            go = new WallObject(rockTexture.getWidth()*(gridSize-1), i);
            go.addTexture(rockTexture, 0, 0);
            objects.add(go);
        }
        
        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        
        // Creating the player's ship
        player = new PlayerObject(
                             (float)(grassTexture.getWidth()*12)/2f,
                             (float)(grassTexture.getHeight()*2)/2f);
        
        for (int i = 0; i < 72; i++) {
            player.addTexture(loader.load("Textures/ship/spaceship_sm"+i+".gif"), 16, 16);
        }
        
        objects.add(player);

        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        
        //Creating enemy ships
        for(int t=0; t<3; t++)
        {

        enemy.add(new PlayerObject((float)(grassTexture.getWidth()*(12-(t*4)))/2f,
                                 (float)(grassTexture.getHeight()*(20-(t*6)))/2f));
        
        for (int i = 0; i < 72; i++) {
            enemy.elementAt(t).addTexture(loader.load("Textures/ship/spaceship_sm"+i+".gif"), 14, 14);
        }
        
        objects.add(enemy.elementAt(t));
        }
    }

    // ================================================================================
==================
    
    // this method is used to fire a bullet
    public void fireBullet() {
        
        cooldownTimer = cooldown;
        
        float dir = 90+player.getDegreesTo(mousePos);
        BulletObject bullet =
                new BulletObject(
                         player.getPosition().x + (float)Math.sin(Math.toRadians(dir))*32, player.getPosition().y - (float)Math.cos(Math.toRadians(dir))*32, 1f, 300, bulletTexture);
        
        //bullet.setVelocity(player.getVelocity());
        bullet.applyForceInDirection(dir, 6f);

      
        objects.add(bullet);
        
    }

    //this method is used to fire an enemy bullet
    public void enemyFireBullet() {

        cooldownTimer = cooldown;
        for(int t=0; t<enemy.size(); t++)
        {
        float dir = 100+enemy.elementAt(t).getDegreesTo(player);//enemy bullets will miss player by 10 degrees
        BulletObject bullet =
                new BulletObject(
                         enemy.elementAt(t).getPosition().x + (float)Math.sin(Math.toRadians(dir))*32, enemy.elementAt(t).getPosition().y - (float)Math.cos(Math.toRadians(dir))*32, 1f, 300, bulletTexture);

        //bullet.setVelocity(player.getVelocity());
        bullet.applyForceInDirection(dir, 6f);
  
        objects.add(bullet);
        }
    }

    public static boolean isPointInBox(final Point2D.Float point, final Rectangle2D.Float box) {
        return box.contains(point.x, point.y);
    }
    
    // This is a pretty bad implementation and faster ones exist, it is suggested you find a better one. At least try make use of the Rectangle2D's createIntersection method.
    public static boolean boxIntersectBox (final Rectangle2D.Float d, final Rectangle2D.Float d2) {
        return  isPointInBox(new Point2D.Float (d.x, d.y), d2) ||
                isPointInBox(new Point2D.Float (d.x, d.y+d.height), d2) ||
                isPointInBox(new Point2D.Float (d.x+d.width, d.y), d2) ||
                isPointInBox(new Point2D.Float (d.x+d.width, d.y+d.height), d2) ||
                isPointInBox(new Point2D.Float (d2.x, d2.y), d) ||
                isPointInBox(new Point2D.Float (d2.x, d2.y+d2.height), d) ||
                isPointInBox(new Point2D.Float (d2.x+d2.width, d2.y), d) ||
                isPointInBox(new Point2D.Float (d2.x+d2.width, d2.y+d2.height), d);
    }
    
    private void handleControls(GameInputInterface gii) {
        
        //----------------------------------
        
        // This isn't so great, there are better and neater ways to do this, you are encouraged to implement a better one
        boolean move = false;
        float directionToMove = 0;
        
        if(gii.keyDown(KeyEvent.VK_UP)) {
            move = true;
            if(gii.keyDown(KeyEvent.VK_LEFT) && !gii.keyDown(KeyEvent.VK_RIGHT))
                directionToMove = 225;
            else if(gii.keyDown(KeyEvent.VK_RIGHT) && !gii.keyDown(KeyEvent.VK_LEFT))
                directionToMove = 135;
            else
                directionToMove = 180;
        }
        else if(gii.keyDown(KeyEvent.VK_DOWN)) {
            move = true;
            if(gii.keyDown(KeyEvent.VK_LEFT) && !gii.keyDown(KeyEvent.VK_RIGHT))
                directionToMove = -45;
            else if(gii.keyDown(KeyEvent.VK_RIGHT) && !gii.keyDown(KeyEvent.VK_LEFT))
                directionToMove = 45;
            else
                directionToMove = 0;
        }
        else if(gii.keyDown(KeyEvent.VK_LEFT) && !gii.keyDown(KeyEvent.VK_RIGHT)) {
            move = true;
            directionToMove = 270;
        }
        else if(gii.keyDown(KeyEvent.VK_RIGHT) && !gii.keyDown(KeyEvent.VK_LEFT)) {
            move = true;
            directionToMove = 90;
        }
        if (move)
            player.moveInDirection(directionToMove);
        
        if (cooldownTimer <= 0) {
            if(gii.keyDown(KeyEvent.VK_SPACE) || gii.mouseButtonDown(MouseEvent.BUTTON1)) {
                fireBullet();
                
            }
        }
        cooldownTimer --;
        
    }

    // ================================================================================
==================
    
    public void logicStep(GameInputInterface gii) {
        /*if(gii.keyDown(KeyEvent.VK_W)) {
            offset.y -= 3.0;
        }
        if(gii.keyDown(KeyEvent.VK_S)) {
            offset.y += 3.0;
        }
        if(gii.keyDown(KeyEvent.VK_A)) {
            offset.x += 3.0;
        }
        if(gii.keyDown(KeyEvent.VK_D)) {
            offset.x -= 3.0;
        }
        if(gii.keyDown(KeyEvent.VK_ESCAPE)) {
            endGame();
        }*/
        
        // some examples of the mouse interface
        mouseWheelTick+= gii.mouseWheelRotation();
        mousePos.x = (float)gii.mouseXScreenPosition() - offset.x;
        mousePos.y = (float)gii.mouseYScreenPosition() - offset.y;
        
        //----------------------------------
        
        if (alive) {
            handleControls(gii);
            player.setDirection(90+player.getDegreesTo(mousePos));
            if(enemy.size() == 0)//all enemies dead
            {
                objects.clear();
                enemyAlive = false;
            }
            else
            {
            for(int t=0; t<enemy.size(); t++)
            {
                //enemies move in player's direction
            enemy.elementAt(t).setDirection(90+enemy.elementAt(t).getDegreesTo(player));
            
            if (trigger%20 == 0)
            {
                enemy.elementAt(t).moveInDirection(90+enemy.elementAt(t).getDegreesTo(player));
            }
            if (trigger%500 == 0)//controls how often enemies fire bullets
            {
                enemyFireBullet();
            }
            trigger++;//variable used to control how often bullets are fired
        }
        }
        }
        // NOTE: you must call doTimeStep for ALL game objects once per frame!
        // updateing step for each object
        for (int i = 0; i < objects.size(); i++) {
            objects.elementAt(i).doTimeStep();
        }
        
        // setting the camera offset
        offset.x = -player.getPosition().x + (this.getViewportDimension().width/2);
        offset.y = -player.getPosition().y + (this.getViewportDimension().height/2);
        
        
        //checking each unit against each other unit for collisions
        for (int i = 0; i < objects.size(); i++) {
            for (int j = i+1; j < objects.size(); j++) {
                GameObject o1 = objects.elementAt(i);
                GameObject o2 = objects.elementAt(j);
                if (boxIntersectBox(o1.getAABoundingBox(), o2.getAABoundingBox())) {
                  
                            if (fine_tune_collision(o1, o2))//checks if the pixels are both non-transparent
                            {
                              if (o1 instanceof WallObject && o2 instanceof WallObject) {
                        //do nothing
                              }
                              else if ((o1 instanceof BulletObject && o2 instanceof WallObject) || o1 instanceof WallObject && o2 instanceof BulletObject) {
                        // just destroy the bullet, not the wall
                        if (o1 instanceof BulletObject)
                            o1.setMarkedForDestruction(true);
                        else
                            o2.setMarkedForDestruction(true);
                        
                              }
                              else if ((o1 instanceof PlayerObject && o2 instanceof WallObject) || o1 instanceof WallObject && o2 instanceof PlayerObject) {
                                  if(o1 == player || o2 == player)
                                  {
                                        player.revertPosition();
                                  }
                                  else if(o1 instanceof PlayerObject)
                                  {
                                      for(int t=0; t<enemy.size(); t++)
                                      {
                                          if(o1 == enemy.elementAt(t))
                                          {
                                              enemy.elementAt(t).revertPosition();
                                          }
                                      }
                                  }
                                  else
                                  {
                                      for(int t=0; t<enemy.size(); t++)
                                      {
                                          if (o2 == enemy.elementAt(t))
                                          {
                                              enemy.elementAt(t).revertPosition();
                                          }
                                      }
                                  }
                              }
                          else if (o1 instanceof PlayerObject && o2 instanceof PlayerObject){
                        if (o1 == player)
                                {//kill player
                                    o1.setMarkedForDestruction(true);
                                    player.setHealth(0);
                                }
                                else
                                {//kill player
                                    o2.setMarkedForDestruction(true);
                                    player.setHealth(0);
                                }
                          }
                              else if ((o1 instanceof PlayerObject && o2 instanceof HealthObject) || o1 instanceof HealthObject && o2 instanceof PlayerObject)
                              {
                                  if(o1 == player)
                                  {
                                      if (player.getHealth() == 100)
                                      {//dont change health but health pack used up
                                          o2.setMarkedForDestruction(true);
                                      }
                                      else
                                      {//increase player's health and health pack used up
                                        player.increaseHealth();
                                        o2.setMarkedForDestruction(true);
                                      }
                                  }
                                  else if(o2 == player)
                                  {
                                      if (player.getHealth() == 100)
                                      {
                                          o1.setMarkedForDestruction(true);
                                      }
                                      else
                                      {
                                        player.increaseHealth();
                                        o1.setMarkedForDestruction(true);
                                      }
                                  }
                              }
                              else if ((o1 instanceof BulletObject && o2 instanceof HealthObject || o1 instanceof HealthObject && o2 instanceof BulletObject))
                              {
                                  //Do nothing
                              }
                          else {//bullets shoot player or enemies
                                  if(o1 == player)
                                  {
                                      if(player.getHealth() == 25)
                                      {
                                          player.setMarkedForDestruction(true);
                                          o2.setMarkedForDestruction(true);
                                          player.setHealth(0);
                                      }
                                      else
                                      {
                                          player.reduceHealth();
                                          o2.setMarkedForDestruction(true);
                                      }
                                  }
                                  else if(o2 == player)
                                  {
                                      if(player.getHealth() == 25)
                                      {
                                          player.setMarkedForDestruction(true);
                                          o1.setMarkedForDestruction(true);
                                          player.setHealth(0);
                                      }
                                      else
                                      {
                                           player.reduceHealth();
                                           o1.setMarkedForDestruction(true);
                                      }

                                  }
                                  else
                                  {
                                      o1.setMarkedForDestruction(true);
                                      o2.setMarkedForDestruction(true);
                                  }
                        }
                     }
                }
            
        }
        
        
        // destroying units that need to be destroyed
        for (int t = 0; t < objects.size(); t++) {
            if (objects.elementAt(t).isMarkedForDestruction()) {
                if (objects.elementAt(t) == player) {
                        alive = false;
               }
                else if (objects.elementAt(t) instanceof PlayerObject)
                {
                    for(int j=0; j<enemy.size(); j++)
                    {
                        if(objects.elementAt(t) == enemy.elementAt(j))
                            enemy.remove(j);
                    }
                  }
            // removing object from list of GameObjects
                objects.remove(t);
                t--;
                    }
                        }
            
    }
    }
    // ================================================================================
==================
    
    
    public void renderStep(GameDrawer drawer) {
        //For every object that you want to be rendered, you must call the draw function with it as a parameter
        
        // NOTE: Always draw transparent objects last!
        
        // Offsetting the world so that all objects are drawn
        drawer.setWorldOffset(offset.x, offset.y);
        drawer.setColour(1.0f, 1.0f, 1.0f, 1.0f);

        // drawing the ground tiles
        for (int i = 0; i < gridTile.length; i++ ) {
            for (int j = 0; j < gridTile[i].length; j++ ) {
                drawer.draw(gridTile[i][j], 1.0f, 1.0f, 1.0f, 1.0f, 0);
            }
        }
        
        // drawing all the objects in the game
        for (GameObject o: objects) {
            drawer.draw(o, 1.0f, 1.0f, 1.0f, 1.0f, 0);
        }
        

        // this is just a random line drawn in the corner of the screen
        //drawer.draw(GameDrawer.LINES, linePositions, lineColours, 0.5f);
        /*
        if (player != null) {
            Point2D.Float [] playerLines = {mousePos, player.getPosition()};
            drawer.draw(GameDrawer.LINES, playerLines, lineColours, 0.5f);
        }*/
        
        drawer.setColour(1.0f,1.0f,1.0f,1.0f);
        
        // Changing the offset to 0 so that drawn objects won't move with the camera
        drawer.setWorldOffset(0, 0);
     &nbs

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 02:56PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month