Ejemplo n.º 1
0
  public void mousePressed() {
    ArrayList<Agent> agents = env.getAllAgents();
    Agent agent_clicked = null;

    if (win.mousePressed()) return;
    if (!Utilities.isPointInBox(mouseX, mouseY, 0, 0, draw_width, draw_height)) return;

    /*
     * Mouse Modes are as follows:
     * 0 = Click tool - Select agents to see information on them in the top left hand corner
     * 1 = Food tool - Place food where you click
     * 2 = Agent tool - Place agent where you click
     * 3 = Thrust tool - Thrusts the agent clicked on by 2
     * 4 = Wall tool - Place a new wall in the environment
     */

    // coordinates of the mouse within the simulation environment
    int simMouseX = (int) ((float) (mouseX - offsetX) / zoomLevel);
    int simMouseY = (int) ((float) (mouseY - offsetY) / zoomLevel);
    if (!Utilities.isPointInBox(simMouseX, simMouseY, 0, 0, Environment.width, Environment.height))
      return;

    // A wall has been placed -> add to the world
    if (PLACE_MODE) {
      PLACE_MODE = false;
      ArrayList<Wall> walls = env.getAllWalls();
      Wall wl = walls.get(walls.size() - 1);
      wl.addToWorld();
      return;
    }
    switch (mouseMode) {
      case 0:
        agent_clicked = getClickedAgent(agents, simMouseX, simMouseY);
        if (agent_clicked != null) { // agent was clicked so update selected
          selectedAgent = agent_clicked;
        }
        break;

      case 1:
        env.addSeaweed(new Point2D.Double(simMouseX, simMouseY));
        break;

      case 2:
        int heading = (int) Math.floor(Math.random() * 360);
        env.addFish(new Point2D.Double(simMouseX, simMouseY), heading);
        break;
      case 3:
        agent_clicked = getClickedAgent(agents, simMouseX, simMouseY);
        if (agent_clicked != null) { // agent was clicked so update selected
          agent_clicked.thrust(2);
        }
        break;
      case 4:
        env.addWall(
            new Point2D.Double(simMouseX, simMouseY), new Point2D.Double(simMouseX, simMouseY));
        PLACE_MODE = true;
    }
  }
Ejemplo n.º 2
0
  public void mouseMoved() {
    if (PLACE_MODE) {
      int simMouseX = (int) ((float) (mouseX - offsetX) / zoomLevel);
      int simMouseY = (int) ((float) (mouseY - offsetY) / zoomLevel);
      ArrayList<Wall> walls = env.getAllWalls();
      Wall w = walls.get(walls.size() - 1);

      w.getEnd().x = simMouseX;
      w.getEnd().y = simMouseY;
    }
  }
Ejemplo n.º 3
0
  // Deserialises the environment class selected by user
  private void restoreEnvironment() {
    JFileChooser diag = getFileChooser(false);
    if (diag.showOpenDialog(this) != JFileChooser.APPROVE_OPTION) return;

    File file = diag.getSelectedFile();
    if (!file.exists()) {
      System.out.println("Save file does not exist" + file.getAbsolutePath());
      return;
    }

    try {
      FileInputStream input = new FileInputStream(file);
      ObjectInputStream in = new ObjectInputStream(input);
      Environment en = (Environment) in.readObject();
      in.close();
      input.close();

      if (en != null) { // set all parent variables in environment to this class
        env = en;
        env.parent = this;
        env.buildBox2DWorld();

        for (Agent ag : env.getAllAgents()) {
          ag.parent = this;
          ag.world = env.world;
          ag.setupBox2d();
        }

        for (Wall wall : env.getAllWalls()) {
          wall.world = env.world;
          wall.setupBox2d();
        }
      }

      System.out.println("Environment loaded");
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Ejemplo n.º 4
0
  private void drawSimulation(PApplet canvas) {
    pushMatrix();
    translate(offsetX, offsetY);
    scale(zoomLevel);

    ArrayList<Agent> agents = env.getAllAgents(); // Returns an arraylist of agents
    ArrayList<Food> food = env.getAllFood(); // Returns an arraylist of all the food on the map
    ArrayList<Wall> walls = env.getAllWalls(); // Returns an arraylist of all walls
    ArrayList<Seaweed> seaweed = env.getAllSeaweed();

    for (int i = 0;
        i < agents.size();
        i++) { // Runs through arraylist of agents, will draw them on the canvas
      Agent ag = agents.get(i);

      // draw the field of view for the agent
      if (selectedAgent == null || !agentFocused || (agentFocused && ag == selectedAgent))
        stroke(128);
      else stroke(128, 100); // , (float) ag.getHealth()*200+55
      noFill();
      double range = ag.getVisionRange() * 2;

      pushMatrix();
      translate(ag.getX(), ag.getY());
      rotate((float) Utilities.toRadians(ag.getViewHeading() - ag.getFOV()));
      line(0, 0, (int) (range / 2), 0);
      popMatrix();

      pushMatrix();
      translate(ag.getX(), ag.getY());
      rotate((float) Utilities.toRadians(ag.getViewHeading() + ag.getFOV()));
      line(0, 0, (int) (range / 2), 0);
      popMatrix();

      arc(
          (float) ag.getX(),
          (float) ag.getY(),
          (float) range,
          (float) range,
          (float) Utilities.toRadians(ag.getViewHeading() - ag.getFOV()),
          (float) Utilities.toRadians(ag.getViewHeading() + ag.getFOV()));

      // draw our circle representation for the agent
      noStroke();
      // if(selectedAgent == null || !agentFocused || (agentFocused && ag == selectedAgent))
      // fill(theme.getColor((ag instanceof Enemy ? Types.SHARK : Types.FISH)));
      // else fill(theme.getColor((ag instanceof Enemy ? Types.SHARK : Types.FISH)), 100); //,
      // (float) ag.getHealth()*200 +55); // Alpha was severly impacting performance of simulation
      if (ag instanceof Enemy) {
        fill(
            ((ag.getSpeciesId() + 1) * 25) % 256,
            ((ag.getSpeciesId() + 1) * 47) % 256,
            ((ag.getSpeciesId() + 1) * 69) % 256,
            (agentFocused && ag == selectedAgent ? 100 : 256));
        pushMatrix();
        translate(ag.getX(), ag.getY());
        rotate((float) Utilities.toRadians(ag.getViewHeading()));
        rect(-10, -10, 20, 20);
        popMatrix();
      } else {
        fill(
            ((ag.getSpeciesId() + 1) * 25) % 256,
            ((ag.getSpeciesId() + 1) * 47) % 256,
            ((ag.getSpeciesId() + 1) * 69) % 256,
            (agentFocused && ag == selectedAgent ? 100 : 256));
        ellipse(ag.getX(), ag.getY(), 20, 20);
      }

      if (agentFocused
          && ag == selectedAgent) { // keep agent on screen if in focused / tracking mode
        int simAgX =
            (int) ((ag.getX() * zoomLevel) + offsetX); // screen coordinates of the selected agent
        int simAgY = (int) ((ag.getY() * zoomLevel) + offsetY);

        if (simAgX < trackingBounds) offsetX += trackingBounds - simAgX;
        else if (simAgX > draw_width - trackingBounds)
          offsetX -= simAgX - draw_width + trackingBounds;

        if (simAgY < trackingBounds) offsetY += trackingBounds - simAgY;
        else if (simAgY > draw_height - trackingBounds)
          offsetY -= simAgY - draw_height + trackingBounds;
      }
    }

    noStroke();
    fill(theme.getColor(Types.FOOD));
    for (int i = 0;
        i < food.size();
        i++) { // Runs through arraylist of food, will draw them on the canvas
      Food fd = food.get(i);
      ellipse(fd.getX(), fd.getY(), 5, 5);
    }

    noStroke();
    fill(201, 23, 134);
    for (int i = 0;
        i < seaweed.size();
        i++) { // Runs through arraylist of food, will draw them on the canvas
      Seaweed fd = seaweed.get(i);
      ellipse(fd.getX(), fd.getY(), 5, 5);
    }

    stroke(theme.getColor(Types.WALL));
    noFill();
    for (Wall wl : walls) { // Runs through arraylist of walls, will draw them on the canvas
      switch (wl.getWallType()) {
        case Collision.TYPE_WALL_AGENT:
          stroke(theme.getColor(Types.FISH));
          break;
        case Collision.TYPE_WALL_ENEMY:
          stroke(theme.getColor(Types.SHARK));
          break;
        default:
          stroke(theme.getColor(Types.WALL));
      }

      line(
          (float) wl.getStart().x,
          (float) wl.getStart().y,
          (float) wl.getEnd().x,
          (float) wl.getEnd().y);
    }

    popMatrix();

    fill(0);
    // rect(draw_width - 50, 0, 250, draw_height);
  }