Exemplo n.º 1
0
  private void tryMove() {
    bounds.x += vel.x;
    fetchCollidableRects();
    for (int i = 0; i < r.length; i++) {
      Rectangle rect = r[i];
      if (bounds.overlaps(rect)) {
        if (vel.x < 0) bounds.x = rect.x + rect.width + 0.01f;
        else bounds.x = rect.x - bounds.width - 0.01f;
        vel.x = 0;
      }
    }

    bounds.y += vel.y;
    fetchCollidableRects();
    for (int i = 0; i < r.length; i++) {
      Rectangle rect = r[i];
      if (bounds.overlaps(rect)) {
        if (vel.y < 0) {
          bounds.y = rect.y + rect.height + 0.01f;
        } else bounds.y = rect.y - bounds.height - 0.01f;
        vel.y = 0;
      }
    }

    pos.x = bounds.x - 0.2f;
    pos.y = bounds.y - 0.2f;
  }
Exemplo n.º 2
0
  @Override
  public void update(float delta) {
    velocity.add(acceleration.cpy().scl(delta));
    boundingCircle.set(position.x + 9.0f, position.y + 6.0f, 6.5f);

    if (velocity.y < -200) {
      velocity.y = -200;
    }

    if (position.y > GameRenderer.WORLD_HEIGHT - height - 1) {
      position.y = GameRenderer.WORLD_HEIGHT - height - 1;
      velocity.y = 0;
    }

    position.add(velocity.cpy().scl(delta));

    // Rotate counterclockwise
    if (velocity.y > 0) {
      rotation += 600 * delta;

      if (rotation > 20) {
        rotation = 20;
      }
    }

    // Rotate clockwise
    if (isFalling()) {
      rotation -= 480 * delta;
      if (rotation < -90) {
        rotation = -90;
      }
    }
  }
Exemplo n.º 3
0
  @Override
  public boolean mouseMoved(InputEvent event, float x, float y) {
    Vector2 scrolling = new Vector2();
    float zone = 10;
    float halfWidth = getStage().getViewport().getWorldWidth() * 0.5f;
    float halfHeight = getStage().getViewport().getWorldHeight() * 0.5f;
    Vector3 cameraPosition = getStage().getCamera().position;
    float west = cameraPosition.x - halfWidth + zone;
    float east = cameraPosition.x + halfWidth - zone;
    float north = cameraPosition.y + halfHeight - zone;
    float south = cameraPosition.y - halfHeight + zone;

    if (x <= west) {
      scrolling.x = -1;
    }
    if (x >= east) {
      scrolling.x = 1;
    }

    if (y >= north) {
      scrolling.y = 1;
    }
    if (y <= south) {
      scrolling.y = -1;
    }

    Service.eventQueue().enqueue(new Event(EventName.MOVING_CAMERA, scrolling));
    return super.mouseMoved(event, x, y);
  }
Exemplo n.º 4
0
  public void movePlayer(Touchpad touchpad) {
    float touchX = touchpad.getKnobPercentX();
    float touchY = touchpad.getKnobPercentY();

    if (touchX < -0.1 && touchX < -Math.abs(touchY)) {
      spriteVector.y = 0;
      spriteVector.x = -SPRITE_SPEED;
      spriteDirection = SpriteDirection.e;
    } else if (touchX > 0.1 && touchX > Math.abs(touchY)) {
      spriteVector.y = 0;
      spriteVector.x = SPRITE_SPEED;
      spriteDirection = SpriteDirection.w;
    } else if (touchY < -0.1 && touchY <= -Math.abs(touchX)) {
      spriteVector.x = 0;
      spriteVector.y = -SPRITE_SPEED;
      spriteDirection = SpriteDirection.s;
    } else if (touchY > 0.1 && touchY >= Math.abs(touchX)) {
      spriteVector.x = 0;
      spriteVector.y = SPRITE_SPEED;
      spriteDirection = SpriteDirection.n;
    } else if (touchX <= 0.1 && touchX >= -0.1 && touchY <= 0.1 && touchY >= -0.1) {
      spriteVector.x = 0;
      spriteVector.y = 0;
    }

    if (spriteVector.x == 0 && spriteVector.y == 0) spriteState = State.Idle;
    else spriteState = State.Walking;
  }
Exemplo n.º 5
0
 public void resize() {
   float w = Gdx.graphics.getWidth();
   float h = Gdx.graphics.getHeight();
   invSize.x = 1.0f / w;
   invSize.y = 1.0f / h;
   magicSize.x = w / 16;
   magicSize.y = h / 16;
 }
Exemplo n.º 6
0
 public void onRestart(int y) {
   rotation = 0;
   position.y = y;
   velocity.x = 0;
   velocity.y = 0;
   acceleration.x = 0;
   acceleration.y = 460;
   isAlive = true;
 }
Exemplo n.º 7
0
  public void processCollisions(Array<Block> collidedBlocks) {
    for (Block block : collidedBlocks) {

      if (velocity.y <= 0
          && distance(0, bounds.y, 0, block.bounds.y + block.bounds.height) < bounds.height / 2) {
        if ((distance(bounds.x + bounds.width, 0, block.bounds.x, 0) < .1
                || distance(bounds.x, 0, block.bounds.x + block.bounds.width, 0) < .05)
            && !onGround
            && collidedBlocks.size > 1) {

          if (velocity.x > 0 && block.bounds.x > bounds.x)
            position.x = block.bounds.x - bounds.width;

          if (velocity.x < 0 && block.bounds.x < bounds.x)
            position.x = block.bounds.x + block.bounds.width;
          break;
        }

        velocity.y = 0;
        position.y = block.bounds.height + block.bounds.y;
        onGround = true;
        if (state.equals(State.JUMPING)) {
          setState(State.IDLE);
        }
      } else if (velocity.y > 0
          && distance(0, bounds.y + bounds.height, 0, block.bounds.y) < bounds.height / 2) {

        if ((distance(bounds.x + bounds.width, 0, block.bounds.x, 0) < .1
                || distance(bounds.x, 0, block.bounds.x + block.bounds.width, 0) < .05)
            && collidedBlocks.size > 1) {

          if (velocity.x > 0 && block.bounds.x > bounds.x) {
            position.x = block.bounds.x - bounds.width;
            System.out.println("going right");
          }

          if (velocity.x < 0 && block.bounds.x < bounds.x) {
            System.out.println("going left");
            position.x = block.bounds.x + block.bounds.width;
          }
          break;
        }

        position.y = block.bounds.y - bounds.height;
        velocity.y = 0;
      } else if (velocity.x > 0
          && distance(bounds.x + bounds.width, 0, block.bounds.x, 0) < bounds.width / 2) {
        velocity.x = 0;
        position.x = block.bounds.x - bounds.width;
      } else if (velocity.x < 0
          && distance(bounds.x, 0, block.bounds.x + block.bounds.width, 0) < bounds.width / 2) {
        velocity.x = 0;
        position.x = block.bounds.x + block.bounds.width;
      }
    }
  }
Exemplo n.º 8
0
  @Override
  public void interpolate(float f) {
    interpolatedPositionA.x = lastPhysicsPosA.x * f;
    interpolatedPositionA.x += joint.getAnchorA().x * (1 - f);

    interpolatedPositionA.y = lastPhysicsPosA.y * f;
    interpolatedPositionA.y += joint.getAnchorA().y * (1 - f);

    interpolatedPositionB.x = lastPhysicsPosB.x * f;
    interpolatedPositionB.x += joint.getAnchorB().x * (1 - f);

    interpolatedPositionB.y = lastPhysicsPosB.y * f;
    interpolatedPositionB.y += joint.getAnchorB().y * (1 - f);
  }
Exemplo n.º 9
0
  protected static Collision AABBvsCircle(AABB A, Circle B) {

    Vector2 AtoB = B.getPosition().cpy().sub(A.getCenter());

    float xe = A.getDimensions().x / 2;
    float ye = A.getDimensions().y / 2;

    Vector2 closest =
        new Vector2(MathUtils.clamp(AtoB.x, -xe, xe), MathUtils.clamp(AtoB.y, -ye, ye));

    boolean inside = false;
    if (AtoB.epsilonEquals(closest, (float) 1e-4)) {
      inside = true;

      if (Math.abs(AtoB.x) > Math.abs(AtoB.y)) {
        if (closest.x > 0.0f) {
          closest.x = xe;
        } else {
          closest.x = -xe;
        }
      } else {
        if (closest.y > 0.0f) {
          closest.y = ye;
        } else {
          closest.y = -ye;
        }
      }
    }

    Vector2 normal = new Vector2(AtoB).sub(closest);
    float d = normal.len2();
    float r = B.radius;

    // No collision
    if (d > r * r && !inside) {
      return null;
    }

    d = (float) Math.sqrt(d);

    float penetration = r + d;
    if (inside) {
      normal.set(AtoB).mul(1.0f).nor();
    } else {
      normal.set(AtoB).mul(-1.0f).nor();
    }

    return new Collision(normal, penetration);
  }
Exemplo n.º 10
0
    private void action(int action) {
      mCubicStartAniSprite.setVisible(false);
      mCubicStartAniSprite.stopAnimation(0);
      switch (action) {
        case ACTION_TURN_LEVEL_1:
        case ACTION_TURN_LEVEL_2:
        case ACTION_TURN_LEVEL_3:
          ACTION = action;
          final Body body = (Body) this.getUserData();
          final Vector2 vector2 = Vector2Pool.obtain(body.getPosition());
          int frame[] = new int[] {0, 1};
          body.setAngularDamping(1.2f);
          vector2.x = vector2.x / 3;
          vector2.y = vector2.y / 3;
          if (action == ACTION_TURN_LEVEL_2) {
            frame = new int[] {2, 3};
            body.setAngularDamping(2.2f);
            vector2.x = vector2.x / 2;
            vector2.y = vector2.y / 2;
          } else if (action == ACTION_TURN_LEVEL_3) {
            frame = new int[] {4, 5};
            body.setAngularDamping(3.2f);
          }
          Vol3Osyougatsu.OGG_A3_A_5_KOMATEI.play();
          mCubicTurnAniSprite.animate(new long[] {250, 250}, frame, -1);
          mCubicTurnAniSprite.setVisible(true);

          body.setActive(true);
          Log.i(TAG, "body.getPosition " + vector2);
          vector2.x = random(vector2.x) + new Random().nextFloat();
          vector2.y = random(vector2.y) + new Random().nextFloat();
          body.setLinearVelocity(vector2);
          Log.i(TAG, "body.getPosition change " + vector2);
          this.unregisterUpdateHandler(timerHandler);
          this.registerUpdateHandler(
              timerHandler =
                  new TimerHandler(
                      6f,
                      new ITimerCallback() {

                        @Override
                        public void onTimePassed(TimerHandler timerHandler) {
                          // Stop Turn
                          stopTurn();
                        }
                      }));
          break;
      }
    }
Exemplo n.º 11
0
 /**
  * Transforms the specified point in the actor's coordinates to be in the parent's coordinates.
  * Note this method will ONLY work for screen aligned, unrotated, unscaled actors!
  */
 public Vector2 localToParentCoordinates(Vector2 localCoords) {
   if (getRotation() != 0 || getScaleX() != 1 || getScaleY() != 1)
     throw new GdxRuntimeException("Only unrotated and unscaled actors may use this method.");
   localCoords.x += getX();
   localCoords.y += getY();
   return localCoords;
 }
Exemplo n.º 12
0
  public void actBehavior(float deltaTime) {
    Random rand = new Random();

    switch (state) {
      case RESTING:
        if (stateTime > restTime) {
          state = MOVING;
          accel.x = rand.nextFloat() * 2 - 1; // -1 to 1
          accel.y = rand.nextFloat() * 2 - 1;
          accel.nor().scl(ACCEL);
          stateTime = 0.0f;
        } else {
          // still resting...
        }
        break;
      case MOVING:
        if (stateTime > moveTime) {
          state = RESTING;
          accel.set(0, 0);
          velocity.set(0, 0);
          stateTime = 0.0f;
        } else {
          // still moving...
          // Continue accel'ing in current direction
          accel.set(velocity.x, velocity.y).nor().scl(ACCEL);
        }
        break;
      default:
        break;
    }
    stateTime += deltaTime;
  }
Exemplo n.º 13
0
  public void update(float delta) {

    velocity.add(acceleration.cpy().scl(delta));

    if (velocity.y > 200) {
      velocity.y = 200;
    }

    position.add(velocity.cpy().scl(delta));

    boundingCircle.set(position.x + 9, position.y + 6, 6.5f);

    if (velocity.y < 0) {
      rotation -= 600 * delta;

      if (rotation < -20) {
        rotation = -20;
      }
    }

    if (isFalling()) {
      rotation += 480 * delta;
      if (rotation > 90) {
        rotation = 90;
      }
    }
  }
 @Override
 protected void processEntity(Entity entity, float deltaTime) {
   PlatformMonsterComp pm = entity.getComponent(PlatformMonsterComp.class);
   MonsterMovementComp mm = entity.getComponent(MonsterMovementComp.class);
   BodyComp b = entity.getComponent(BodyComp.class);
   Vector2 pos = b.body.getPosition();
   pos.x *= b.invWorldScale;
   pos.y *= b.invWorldScale;
   switch (mm.moveType) {
     case LEFT:
       if (pos.x <= pm.minX) {
         standMonster(pm, mm);
       }
       break;
     case STAND:
       if (pm.standCountdown > 0) {
         pm.standCountdown -= deltaTime;
       }
       if (pm.standCountdown <= 0) {
         pm.standCountdown = 0;
         if (pos.x <= pm.minX) {
           mm.moveType = MonsterMovementComp.MoveType.RIGHT;
         } else {
           mm.moveType = MonsterMovementComp.MoveType.LEFT;
         }
       }
       break;
     case RIGHT:
       if (pos.x >= pm.maxX) {
         standMonster(pm, mm);
       }
       break;
   }
 }
Exemplo n.º 15
0
 public void move(float delta) {
   setPosition(getX() + (impulse.x * delta), getY() + (impulse.y * delta));
   if (getX() < leftBorder) {
     impulse.x = -impulse.x;
     setX(leftBorder);
   } else if (getX() > rightBorder) {
     impulse.x = -impulse.x;
     setX(rightBorder);
   } else if (getY() < bottomBorder) {
     impulse.y = -impulse.y;
     setY(bottomBorder);
   } else if (getY() > topBorder) {
     impulse.y = -impulse.y;
     setY(topBorder);
   }
 }
Exemplo n.º 16
0
 private void createBars(ShapeRenderer renderer) {
   for (int i = 0; i < 3; i++) {
     switch (i) {
       case 0:
         bars.add(new Bar(renderer, position, BIRD_FOOD_MAX, CATERPILLAR_COLOR_BODY));
         position.y -= BAR_HEIGHT + BAR_MARGIN;
         break;
       case 1:
         bars.add(new Bar(renderer, position, BIRD_ENERGY_MAX, BIRD_COLOR_BODY));
         position.y -= BAR_HEIGHT + BAR_MARGIN;
         break;
       case 2:
         bars.add(new Bar(renderer, position, BIRD_POOP_MAX, POOP_COLOR_CIRCLE));
     }
   }
 }
Exemplo n.º 17
0
 public static boolean hasArrived(Entity entity) {
   EnemyMovement enemyMovement = EntityUtil.getComponent(entity, EnemyMovement.class);
   Vector2 intent = enemyMovement.getIntent().cpy();
   Vector2 position = PhysicsUtil.getPosition(entity);
   intent.y = position.y;
   return position.epsilonEquals(intent, Constants.GAME.EPSILON);
 }
Exemplo n.º 18
0
 @Override
 public void read(Json json, JsonValue jsonData) {
   Iterator<JsonValue> iter = jsonData.iterator();
   JsonValue value;
   while (iter.hasNext()) {
     value = iter.next();
     switch (value.name()) {
       case "name":
         name = value.asString();
         break;
       case "dir":
         dir.x = value.get("x").asFloat();
         dir.y = value.get("x").asFloat();
         break;
       case "speed":
         speed = value.asFloat();
         break;
       case "bounds":
         bounds.width = value.getFloat("width");
         bounds.height = value.getFloat("height");
         setPosition(
             new Vector2(
                 value.getFloat("x") + bounds.width / 2, value.getFloat("y") + bounds.height / 2));
         break;
       case "score":
         score.read(json, value);
         break;
     }
   }
 }
 @Override
 public Vector2 accelerate(Vector2 acceleration, Animal animal) {
   // calculate the horizontal and vertical acceleration via Accelerometer
   acceleration.x = Gdx.input.getAccelerometerY();
   acceleration.y = -Gdx.input.getAccelerometerX();
   return acceleration;
 }
  public static boolean inLineOfSight(Vector2 p1, Vector2 p2, Polygon polygon, boolean obstacle) {
    tmp.set(p1);
    tmp2.set(p2);

    float verts[] = polygon.getTransformedVertices();

    for (int i = 0; i < verts.length; i += 2) {
      if (lineSegmentsCross(
          tmp.x,
          tmp.y,
          tmp2.x,
          tmp2.y,
          verts[i],
          verts[i + 1],
          verts[(i + 2) % verts.length],
          verts[(i + 3) % verts.length])) return false;
    }

    tmp.add(tmp2);
    tmp.x /= 2;
    tmp.y /= 2;

    boolean result = PolygonUtils.isPointInside(polygon, tmp.x, tmp.y, !obstacle);

    return obstacle ? !result : result;
  }
Exemplo n.º 21
0
  public void update(float delta, Array<Block> collisions) {
    // NOTE: figure out why legs go into block, could be order of checking for collisions/updating
    // position
    // position += velocity above collisions before changing the velocity
    acceleration.y = gravity;

    if (velocity.y < 0) onGround = false;

    if (delta
        > .1f) { // make sure time passed isn't great enough to cause clipping issues when window
                 // moved
      delta = .02f;
    }

    acceleration.scl(delta);
    velocity.add(acceleration);

    processCollisions(collisions);

    if (Math.abs(velocity.x) < .05) velocity.x = 0;
    else {
      velocity.x *= friction;
    }

    if (velocity.x > maxVel) velocity.x = maxVel;
    if (velocity.x < -maxVel) velocity.x = -maxVel;

    position.add(velocity.cpy().scl(delta));
    bounds.x = position.x;
    bounds.y = position.y;
    stateTime += delta;

    setPosition(getPosition());
  }
Exemplo n.º 22
0
  public void update(float delta) {
    lastShoot = null;

    boolean up = Gdx.input.isKeyPressed(Keys.W) || Gdx.input.isKeyPressed(Keys.UP);
    boolean down = Gdx.input.isKeyPressed(Keys.S) || Gdx.input.isKeyPressed(Keys.DOWN);
    boolean left = Gdx.input.isKeyPressed(Keys.A) || Gdx.input.isKeyPressed(Keys.LEFT);
    boolean right = Gdx.input.isKeyPressed(Keys.D) || Gdx.input.isKeyPressed(Keys.RIGHT);
    boolean shoot = Gdx.input.isTouched();

    if (up && !down) {
      deltaPos.y += SPEED * delta;
    } else if (down && !up) {
      deltaPos.y -= SPEED * delta;
    }
    if (left && !right) {
      deltaPos.x -= SPEED * delta;
    } else if (right && !left) {
      deltaPos.x += SPEED * delta;
    }
    if (shoot && GameScreen.oldCursor == GameScreen.aim) {
      deltaPos.x = deltaPos.y = 0;
      shoot();
    }

    dirX = Gdx.input.getX() - 400;
    dirY = 600 - Gdx.input.getY() - 300;

    angle = (float) (Math.toDegrees(Math.atan(dirY / dirX))) - 90;
    if (dirX >= 0) angle += 180;

    if (deltaPos.x != 0 || deltaPos.y != 0) currentAnimation = walking;
    else if (GameScreen.cursor == GameScreen.oldCursor && shoot) currentAnimation = shooting;
    else currentAnimation = idle;

    position.x += deltaPos.x;
    position.y += deltaPos.y;

    //		if (Gdx.input.isKeyJustPressed(Keys.SPACE)) {
    //			position.x += 70;
    //		}
    //		if (Gdx.input.isKeyJustPressed(Keys.P)) {
    //			GameScreen.cam.zoom = (GameScreen.cam.zoom == 8 ? 1 : 8);
    //		}

    animTime += delta;
    updateAABB();
  }
Exemplo n.º 23
0
  /**
   * Transforms the given vector by this transform
   *
   * @param v the vector
   */
  public Vector2 mul(Vector2 v) {
    float x = vals[POS_X] + vals[COL1_X] * v.x + vals[COL2_X] * v.y;
    float y = vals[POS_Y] + vals[COL1_Y] * v.x + vals[COL2_Y] * v.y;

    v.x = x;
    v.y = y;
    return v;
  }
Exemplo n.º 24
0
 /**
  * Updates the camera position.
  *
  * @param deltaTime
  */
 public void update(float deltaTime) {
   if (!hasTarget()) {
     return;
   }
   position.lerp(target.position, FOLLOW_SPEED * deltaTime);
   // Prevent camera from moving down too far
   position.y = Math.max(-1f, position.y);
 }
Exemplo n.º 25
0
  private void drawTriangleStrips(ShapeRenderer shapeRenderer, Color color, Color color1) {
    for (int i = 0; i < vertexDataArray.size; i++) {
      StripVertex bb = vertexDataArray.items[i];

      Array<Float> data = bb.insideVertexData;
      for (int j = 0; j < data.size - 3; ) {

        shapeRenderer.setColor(j == 0 ? color : color1);

        tmp.x = data.items[j];
        tmp.y = data.items[j + 1];
        tmp.rotateRad(angleRad);
        tmp.scl(scale);
        tmp.add(position);

        tmp1.x = data.items[j + 3];
        tmp1.y = data.items[j + 4];
        tmp1.rotateRad(angleRad);
        tmp1.scl(scale);
        tmp1.add(position);
        j += 3;

        shapeRenderer.line(tmp, tmp1);
      }
      data = bb.outsideVertexData;
      for (int j = 0; j < data.size - 3; ) {

        shapeRenderer.setColor(j == 0 ? Color.ORANGE : Color.RED);

        tmp.x = data.items[j];
        tmp.y = data.items[j + 1];
        tmp.rotateRad(angleRad);
        tmp.scl(scale);
        tmp.add(position);

        tmp1.x = data.items[j + 3];
        tmp1.y = data.items[j + 4];
        tmp1.rotateRad(angleRad);
        tmp1.scl(scale);
        tmp1.add(position);
        j += 3;

        shapeRenderer.line(tmp, tmp1);
      }
    }
  }
Exemplo n.º 26
0
 private void continueJump() {
   if (jumpState == Enums.JumpState.JUMPING) {
     if (Utils.secondsSince(jumpStartTime) < Constants.MAX_JUMP_DURATION) {
       velocity.y = Constants.JUMP_SPEED;
     } else {
       endJump();
     }
   }
 }
Exemplo n.º 27
0
 public void move(float x, float y) {
   position.x += x;
   position.y += y;
   Vector2 lPos =
       Utils.mapToScreen(
           new Vector2(position.x - 0.5F, position.y + 1),
           new Vector2(game.getMainCam().position.x, game.getMainCam().position.y));
   label.setPosition(lPos.x, lPos.y);
 }
Exemplo n.º 28
0
 public void damage(float i, float x, float y) {
   long now = TimeUtils.nanosToMillis(TimeUtils.nanoTime());
   if (now - lastDamaged > damageInterval) {
     lastDamaged = now;
     hp -= i;
     deltaPos.x += x;
     deltaPos.y += y;
   }
 }
Exemplo n.º 29
0
  public Car(float xKord, float yKord) {
    position = new Vector2();
    size = new Vector2(Gdx.graphics.getWidth() / 5, Gdx.graphics.getWidth() / 5);
    speed = new Vector2(0, -700);

    collisionRect = new Rectangle(position.x, position.y, size.x, size.y);
    position.x = xKord;
    position.y = yKord;
  }
Exemplo n.º 30
0
  private void setVelocity() {

    velocity = new Vector2(random.nextFloat() / 2, random.nextFloat() / 2);
    if (random.nextBoolean()) {
      velocity.x = -velocity.x;
    }
    if (random.nextBoolean()) {
      velocity.y = -velocity.y;
    }
  }