@Override
  public RubeImage read(Json json, JsonValue jsonData, Class type) {
    if (scene.getBodies() == null) return null;

    RubeImage defaults = RubeDefaults.Image.image;

    RubeImage image = new RubeImage();

    image.angle = json.readValue("angle", float.class, defaults.angle, jsonData);
    int bodyIndex = json.readValue("body", int.class, jsonData);
    if (bodyIndex >= 0 && bodyIndex < scene.getBodies().size)
      image.body = scene.getBodies().get(bodyIndex);

    image.center.set(json.readValue("center", Vector2.class, defaults.center, jsonData));

    RubeVertexArray corners = json.readValue("corners", RubeVertexArray.class, jsonData);
    if (corners != null) {
      tmp.set(corners.x[0], corners.y[0]).sub(corners.x[1], corners.y[1]);
      image.width = tmp.len();
      tmp.set(corners.x[1], corners.y[1]).sub(corners.x[2], corners.y[2]);
      image.height = tmp.len();
    }

    image.file = json.readValue("file", String.class, jsonData);

    if (stripImageFile) {
      int fslashIndex = image.file.lastIndexOf('/');
      int dotIndex = image.file.lastIndexOf('.');

      if (fslashIndex != -1 && dotIndex != -1 && fslashIndex < dotIndex) {
        image.file = image.file.substring(fslashIndex + 1, dotIndex);
      }
    }

    image.filter = json.readValue("filter", int.class, defaults.filter, jsonData);
    image.name = json.readValue("name", String.class, jsonData);
    image.opacity = json.readValue("opacity", float.class, defaults.opacity, jsonData);
    image.renderOrder = json.readValue("renderOrder", int.class, defaults.renderOrder, jsonData);
    image.scale = json.readValue("scale", float.class, defaults.scale, jsonData);
    image.flip = json.readValue("flip", boolean.class, defaults.flip, jsonData);

    int[] colorArray =
        json.readValue("colorTint", int[].class, RubeDefaults.Image.colorArray, jsonData);

    image.color.r = (float) colorArray[0] / 255;
    image.color.g = (float) colorArray[1] / 255;
    image.color.b = (float) colorArray[2] / 255;
    image.color.a = (float) colorArray[3] / 255;

    RubeCustomProperty customProperty = null;
    if (json.getSerializer(RubeCustomProperty.class) != null)
      customProperty = json.readValue("customProperties", RubeCustomProperty.class, jsonData);

    scene.onAddImage(image, customProperty);

    return image;
  }
Exemple #2
0
 /**
  * Sets the dimensions of this raycast. If stepLength is equals to 0, {@link #stepLength} will be
  * the distance between beginPoint and endPoint.
  *
  * @param beginPoint the point to start the raycast
  * @param endPoint the point to end the raycast
  * @param stepLength the length of each step
  * @throws InvalidRayCastStepException if stepLength is negative
  */
 public void setRay(Vector2 beginPoint, Vector2 endPoint, float stepLength) {
   this.beginPoint.set(beginPoint);
   this.endPoint.set(endPoint);
   if (stepLength < 0) {
     throw new InvalidRayCastStepException("stepLength cannot be negative");
   }
   if (stepLength == 0) {
     this.stepLength = Math.abs(endPoint.len() - beginPoint.len());
   } else {
     this.stepLength = stepLength;
   }
   hit = false;
 }
Exemple #3
0
  public static Vector2 pursue(Vector2 target, Vector2 targetVelocity, Vehicle v) {
    Vector2 velocity = v.getVelocity();
    Vector2 desiredVelocity = v.getCenter().cpy().sub(target);

    float ownVelocityAlignment = desiredVelocity.dot(velocity);
    float relativeHeading = targetVelocity.dot(velocity);
    if (ownVelocityAlignment > 0 && relativeHeading < -0.95) {
      return seek(target, v);
    }

    float lookAheadTime = desiredVelocity.len() / (v.getMaxVelocity() + targetVelocity.len());

    Vector2 predicetTargetPos = target.cpy().add(targetVelocity).cpy().mul(lookAheadTime);

    return seek(predicetTargetPos, v);
  }
 public void drawLine(
     TextureRegion tex, Vector2 p1, Vector2 p2, Color col, float width, boolean precise) {
   Vector2 v = SolMath.getVec(p2);
   v.sub(p1);
   drawLine(tex, p1.x, p1.y, SolMath.angle(v, precise), v.len(), col, width);
   SolMath.free(v);
 }
  public void drawArrow(
      Vector2 a,
      Vector2 b,
      float width,
      float feather,
      float headDist,
      float headWidth,
      Color color) {
    // Firstly, draw the body of the arrow. B is the front.
    drawLine(a, b, width, feather, color);

    // Get the direction vector a->b
    dir.set(a);
    dir.sub(b);
    dir.scl(1 / dir.len());

    // Get the point down the line that the arrow head lines get to
    d.set(dir);
    d.scl(headDist);
    d.add(b);

    // Now, move d out to the sides
    amount.set(dir);
    amount.rotate(90);
    amount.scl(headWidth);

    right.set(d).add(amount);
    left.set(d).sub(amount);

    // Draw the arrow heads to not-quite-b to make it prettier
    c.set(b);
    c.sub(dir.scl(-width * 0.2f));
    drawLine(c, left, width, feather, color);
    drawLine(c, right, width, feather, color);
  }
Exemple #6
0
  /**
   * Update position & velocity vectors given acceleration and deltaTime
   *
   * @param deltaTime
   */
  private void updatePosition(float deltaTime) {
    if (accel.len() > 0) {
      accel.scl(deltaTime); // dv = a * dt
      velocity.add(accel.x, accel.y); // vf = vi + dv

      // Limit velocity to max
      if (velocity.len() > MAX_VELOCITY) {
        velocity.nor().scl(MAX_VELOCITY);
      }
    }
    Vector2 deltaPos = new Vector2(velocity.x * deltaTime, velocity.y * deltaTime);

    Vector2 newDelta = Collision.tryMove(this, bounds, deltaPos);
    if (!newDelta.equals(deltaPos)) { // We hit something
      stopMoving();
    }
    position.add(newDelta); // d = d*dt
  }
Exemple #7
0
  public static Vector2 arrive(Vector2 targetPos, Vehicle v) {
    Vector2 desiredVelocity = v.getCenter().cpy().sub(targetPos);
    Vector2 velocity = v.getVelocity();

    float len = desiredVelocity.len();
    if (len > 0.05f) {
      desiredVelocity.nor();
      float min = Math.min(len, v.getMaxVelocity());
      desiredVelocity.mul(min);
      desiredVelocity.sub(velocity).mul(-CORRECTION);
    }
    return desiredVelocity;
  }
Exemple #8
0
  /**
   * Constructs a new Entity
   *
   * @param texture the texture to display with the Entity
   * @param vel the initial velocity of the Entity
   */
  public Entity(TextureRegion texture, Vector2 vel, float rotation) {
    sprite = new Sprite(texture);
    Vector2 size = new Vector2(sprite.getWidth() / 2, sprite.getHeight() / 2);
    sprite.setOrigin(size.x, size.y);
    entities.add(this);
    sprite.rotate(rotation);
    maxSize = size.len();
    velocity = vel;
    id = objCount++;

    initVelocity = vel;
    initRotation = rotation;
    initPos = new Vector2(0, 0);
  }
Exemple #9
0
  private void avoid(Interactive3DObject collisionObject, int collisionHandle) {
    float distance = position.dst(collisionObject.getPosition());
    float minDistance = getCollisionRadius() + collisionObject.getCollisionRadius();
    float urgency = (float) (1) / (distance - minDistance);
    if (urgency > 20 || urgency < 0) {
      urgency = 20;
    }
    boolean destinationsCollide =
        destination.dst(((MovableI3DO) collisionObject).destination) < (minDistance)
            && position.dst(destination) < minDistance;
    // System.out.println("Urgency: " + urgency);
    if (urgency >= 1 && collisionHandle != COLLISION_BACK) {
      pace = (float) (maxPace * (1 / Math.sqrt(urgency)));
    } else {
      pace = maxPace;
    }

    if (urgency > (float) 0.5
        && urgency + (float) 0.2 > avoidUrgency
        && !destinationsCollide
        && collisionHandle != COLLISION_BACK) {
      // System.out.println("Urgency: " + urgency);
      center.set(
          (float) (position.x + collisionObject.position.x) / 2,
          (float) (position.z + collisionObject.position.z) / 2);
      delta.set(center.x - position.x, center.y - position.z);
      ortho.set(-delta.y, delta.x).scl((float) 1 / (ortho.len()));
      calcRotationAngle(ortho.x, ortho.y);
    } else if (destinationsCollide
        && distance - (float) 0.2 <= minDistance
        && destinationsCollide) {
      setDestination(this.position);
    } else if (urgency + (float) 0.2 < avoidUrgency && distance > minDistance) {
      System.out.println("back to orginal destination!");
      this.calcRotationAngle(destination.x - this.position.x, destination.z - this.position.z);
      avoid = false;
    }
    avoidUrgency = urgency;
  }
Exemple #10
0
  @Override
  public void update() {
    if (!isActive()) return;

    posCpy.set(GameModel.getPlayer().position);
    diffToPlayer.set(posCpy.sub(position));

    float wantedAngle = MathUtils.atan2(diffToPlayer.y, diffToPlayer.x);
    /*
                      ^
            3,14 <--- |----> 0
    */
    if (direction == AbstractMap.N && (wantedAngle > MathUtils.PI || wantedAngle < 0)) return;
    else if (direction == AbstractMap.S && wantedAngle > 0) return;
    else if (direction == AbstractMap.W
        && (!(wantedAngle < MathUtils.PI / 2f && wantedAngle > -MathUtils.PI / 2f))) return;
    else if (direction == AbstractMap.E
        && (!(wantedAngle > MathUtils.PI / 2f || wantedAngle < -MathUtils.PI / 2f))) return;

    body.setTransform(position.x, position.y, wantedAngle);

    if (diffToPlayer.len() < Config.getDimensions().WORLD_WIDTH / 2f)
      if (MathUtils.random()
          < (type == CANNON_TYPE.SINGLE ? Config.BULLET_RATE_LOW : Config.BULLET_RATE_MEDIUM)) {
        float angle = body.getAngle() * MathUtils.radiansToDegrees;

        origin.set(position).add(diffToPlayer.scl(.1f));
        posCpy.set(GameModel.getPlayer().position);

        if (type == CANNON_TYPE.SINGLE) {
          weapon.fire1(origin, posCpy.rotate(MathUtils.random(-1, 1)));
          createGunSmoke(angle);
        } else if (type == CANNON_TYPE.DOUBLE) {
          weapon.fire1(origin, posCpy.rotate(MathUtils.random(-1, 1)));
          weapon.fire2(origin, posCpy.rotate(MathUtils.random(-1, 1)));
        }
      }
  }
  public void update(float deltaTime) {
    processKeys();

    if (state == FOLLOW) {
      target.set(map.bob.pos);
      if (map.bob.dir == Bob.RIGHT) target.x--;
      if (map.bob.dir == Bob.LEFT) target.x++;
      target.y += 0.2f;

      vel.set(target).sub(pos).scl(Math.min(4, pos.dst(target)) * deltaTime);
      if (vel.len() > MAX_VELOCITY) vel.nor().scl(MAX_VELOCITY);
      tryMove();
    }

    if (state == CONTROLLED) {
      accel.scl(deltaTime);
      vel.add(accel.x, accel.y);
      if (accel.x == 0) vel.x *= DAMP;
      if (accel.y == 0) vel.y *= DAMP;
      if (vel.x > MAX_VELOCITY) vel.x = MAX_VELOCITY;
      if (vel.x < -MAX_VELOCITY) vel.x = -MAX_VELOCITY;
      if (vel.y > MAX_VELOCITY) vel.y = MAX_VELOCITY;
      if (vel.y < -MAX_VELOCITY) vel.y = -MAX_VELOCITY;
      vel.scl(deltaTime);
      tryMove();
      vel.scl(1.0f / deltaTime);
    }

    if (state == FIXED) {
      if (stateTime > 5.0f) {
        stateTime = 0;
        state = FOLLOW;
      }
    }

    stateTime += deltaTime;
  }
  /**
   * Rain clouds appear with a certain chance, influenced by the weather For every rain cloud in the
   * grid the velocity of every rain object is updated Rain clouds are removed if they have passed a
   * certain time
   */
  @ScheduledMethod(start = 1, interval = 1, priority = 0)
  public void rain() {
    // Let new raingroups appear with a certain chance
    double chance = SimulationParameters.rainProb;
    // The probability of rain appearing decreases if there is already rain in the grid
    if (noRainGroups == 1) chance = (chance / (noRainGroups)) * 0.5;
    if (noRainGroups == 2) chance = (chance / (noRainGroups)) * 0.1;
    if (noRainGroups > 2) chance = (chance / (noRainGroups)) * 0.01;
    double f = urng.nextDouble();
    if (f < chance) {
      // Let rain appear
      int x = rand.nextInt((SimulationParameters.gridSize - 0) + 1);
      int y = rand.nextInt((SimulationParameters.gridSize - 0) + 1);
      int[] newLoc = {x, y};
      // Let new raingroup appear in random location
      RainGroup rg = new RainGroup(ContextUtils.getContext(this), grid, newLoc);
      noRainGroups++;
      rainGroups.add(rg);
    }

    ArrayList<RainGroup> toRemove = new ArrayList<RainGroup>();
    for (RainGroup rg : rainGroups) {
      // Get velocity vector of the rain
      float x = Wind.getWindVelocity().x;
      float y = Wind.getWindVelocity().y;
      Vector2 velRain = new Vector2(x, y);
      velRain.setLength(
          Wind.getWindVelocity().len() * 0.9f); // Rain speed is a bit lower than that of the wind

      List<Rain> toRemove1 = new ArrayList<Rain>();
      // Let rain be carried by the wind
      if (urng.nextDouble() < velRain.len()) {
        for (Rain rain : rg.getRainObjects()) {
          Directions dir = Directions.fromVectorToDir(velRain);
          GridPoint pt = grid.getLocation(rain);
          int cX = pt.getX() + dir.xDiff;
          int cY = pt.getY() + dir.yDiff;

          // If new rain-location is out of borders, delete this rain object
          // In this way the cloud "travels" out of the grid
          if (cX < 0
              || cX >= SimulationParameters.gridSize
              || cY < 0
              || cY >= SimulationParameters.gridSize) {
            toRemove1.add(rain);
          } else grid.moveTo(rain, cX, cY);
        }
      }

      for (Rain r : toRemove1) {
        rg.removeRain(r);
        TreeBuilder.performance.decreaseRainCount();
      }
    }

    // Remove the raingroups from our list which were removed from the context
    for (RainGroup rg : toRemove) {
      rainGroups.remove(rg);
      noRainGroups--;
    }
  }
 public void pushAway(Vector2 origin, float force) {
   Vector2 v = new Vector2(origin.x - position.x, origin.y - position.y);
   v.scl(force / v.len());
   velocity.add(v);
 }
 public OvaleParticuleGenerator(float hauteur) {
   hauteur /= 5.3f;
   init(hauteur);
   originalScale = positionEmiter.len();
 }