Package character

Source Code of character.GhostCharacter

/**
*
*/
package character;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Random;

import javax.swing.ImageIcon;

import application.LabyrinthApp;

import labyrinth.Labyrinth;
import labyrinth.Room;

/**
* @author adrien
*
*/
public class GhostCharacter extends DefaultCharacter {

  protected Random r;
  protected Room previousRoom;
 
  public static ImageIcon gLeft;
  public static ImageIcon gRight;
  public static ImageIcon gUp;
  public static ImageIcon gDown;
 
  public GhostCharacter(Room start, Labyrinth lab) {
    super();
    labyrinth = lab;
    r = new Random();
   
    this.setLocation(start);
    this.movement = 0.7f;
  }
 
  /**
   * Tells if this character is a monster or not.
   * @return true if is a monster, false if not.
   */
  @Override
  public boolean isMonster()
  {
    return true;
  }

  /**
          * Put the character in another room
          * @param current the room where you want to put him
          */
  public void moveRandom() {
    Room choosen = currentRoom;
    int cpt = 0, maxLight = 0;
   
    // Find the brightest room (i.e. find the player if the monster sees the light).
    for (Room r : accessibleRooms)
    {
      if (r.getLight() > maxLight)
      {
        cpt = 0;
        maxLight = r.getLight();
        choosen = r;
      }
      else if (r.getLight() == maxLight)
        cpt++;
    }
   
    // IfW there are several rooms at the same bright level (i.e. no light).
    if (cpt > 0)
    {
      ArrayList<Room> forwardRooms = new ArrayList<Room>();
      forwardRooms.addAll(accessibleRooms);
      forwardRooms.remove(previousRoom);
      if(forwardRooms.isEmpty())  // We chose a room in front of the player ...
        choosen = previousRoom;
      else            // ... or the room backward if it's the only choice.
        choosen = forwardRooms.get(r.nextInt(forwardRooms.size()));
    }
     
    this.setLocation(choosen);
  }
 
  /**
     * Put the character in another room
     * @param current the room where you want to put him
     */
  @Override
  public void setLocation(Room current) {
    if (movable)
    {
      previousRoom = currentRoom;
      this.currentRoom = current;
      this.updateAccessibleRooms(labyrinth.accessiblesRooms(this));
     
      Collection<Character> tmp = LabyrinthApp.getPlayers();
      for(Character c : tmp)
      {
        if (c.getLocation() == currentRoom)
          c.doDamage(5);
      }
    }
  }
 
   /**
     * Updates the pixel position using the current room.
     * @param unite the unite of the grid
     * @param movement the scale of the movement
     * @return the corresponding sprite (which is a reference to a static sprite)
     */
  public ImageIcon updatePos()
  {
    ImageIcon res = super.updatePos();
   
    if (res == DefaultCharacter.dDown)
      res = GhostCharacter.gDown;
    if (res == DefaultCharacter.dUp)
      res = GhostCharacter.gUp;
    if (res == DefaultCharacter.dLeft)
      res = GhostCharacter.gLeft;
    if (res == DefaultCharacter.dRight)
      res = GhostCharacter.gRight;
   
    return res;
  }
}
TOP

Related Classes of character.GhostCharacter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.