Пример #1
0
  @Override
  public void render() {

    if (platform.getPosition().x > 10) {
      platform.setLinearVelocity(-PLATFORM_VELOCITY, 0);
    } else if (platform.getPosition().x < -10) {
      platform.setLinearVelocity(PLATFORM_VELOCITY, 0);
    }

    Gdx.gl.glClearColor(1, 1, 1, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    world.step(1 / 60f, 6, 2);
    dDebugRenderer.render(world, cam.combined);
  }
Пример #2
0
  public void setUp() {

    BodyDef bodyDef = new BodyDef();
    bodyDef.position.set(x + width / 2, y + height / 2);
    bodyDef.type = BodyDef.BodyType.KinematicBody;

    PolygonShape shape = new PolygonShape();
    shape.setAsBox(width / 2, height / 2);

    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = shape;

    body = world.createBody(bodyDef);
    body.createFixture(fixtureDef);
    body.setUserData(Constants.robotNinjaID);

    body.setLinearVelocity(Constants.standardVelocity);

    sprite = new Sprite(texture);
    sprite.setSize(width, height);
    sprite.setOrigin(width / 2, height / 2);
    sprite.flip(false, flipy);
    sprite.setPosition(x, y);

    dead = false;
  }
Пример #3
0
  public Flanker(
      AgentBody head, Body body, TextureRegion image, float radius, float offsetX, float offsetY) {
    super(body, radius, image);
    this.head = head;

    mind = new PrioritizingMind();

    mind.addBehavior(
        new OffsetArriveAngled(
            head, 3, Config.getDimensions().SCREEN_LONGEST, 3, offsetX, offsetY));
    mind.addAttackBehavior(
        new OffsetArriveAngled(
            head, 3, Config.getDimensions().SCREEN_LONGEST, 3, offsetX, offsetY));
    mind.addDefendBehavior(
        new OffsetArriveAngled(
            head, 3, Config.getDimensions().SCREEN_LONGEST, 3, offsetX, offsetY));

    body.setLinearVelocity(
        MathUtils.random(minSpeed * 2) - minSpeed, MathUtils.random(minSpeed * 2) - minSpeed);

    initHealth(1);

    playerFocused = true;
    flying = true;

    bodyType = BODY_TYPE.METAL;
  }
Пример #4
0
 public void update() {
   if (body.getUserData() == Constants.robotNinjaID) {
     body.setLinearVelocity(Constants.standardVelocity.x, 0);
   }
   sprite.setPosition(
       body.getPosition().x - sprite.getWidth() / 2,
       body.getPosition().y - sprite.getHeight() / 2);
 }
Пример #5
0
 public void hit() {
   Gyaya.manager.get("audio/music/background.ogg", Music.class).stop();
   Gyaya.manager.get("audio/sounds/uaBArt - Bell.mp3", Sound.class).play();
   playerIsDead = true;
   Filter filter = new Filter();
   filter.maskBits = Gyaya.NOTHING_BIT;
   for (Fixture fixture : b2body.getFixtureList()) fixture.setFilterData(filter);
   b2body.setLinearVelocity(new Vector2(0, 4f));
 }
Пример #6
0
  private void restart() {

    float y = (HEIGHT_WORLD / 2) - 10;

    planetCoreModel.setTransform(0, y, 0);
    planetCoreModel.setLinearVelocity(0f, 0f);
    planetCoreModel.setAngularVelocity(1f);

    planetModel.setTransform(0, y, 0f);
    planetModel.setLinearVelocity(0f, 0f);
    planetModel.setAngularVelocity(1f);

    Vector2 vec = new Vector2();

    for (int i = 0; i < MAX_BALLS; i++) {
      float tx = rand.nextFloat() * 1.0f - 1f;
      float ty = HEIGHT_WORLD + BALL_RADIUS;
      float angle = rand.nextFloat() * MathUtils.PI * 2;

      ballModels[i].setActive(false);
      ballModels[i].setLinearVelocity(vec.set(0, -10));
      ballModels[i].setAngularVelocity(0f);
      ballModels[i].setTransform(vec.set(tx, ty), angle);
    }

    tweenManager.killAll();

    Tween.call(
            new TweenCallback() {
              private int idx = 0;

              @Override
              public void onEvent(int arg0, BaseTween<?> arg1) {
                if (idx < ballModels.length) {
                  ballModels[idx].setAwake(true);
                  ballModels[idx].setActive(true);
                  idx += 1;
                }
              }
            })
        .repeat(MAX_BALLS, 0.5f)
        .start(tweenManager);
  }
 public void applySteering(Body body) {
   // For Kinematic & Static bodies, we set the velocity
   BodyType type = body.getType();
   if (type == BodyType.KinematicBody || type == BodyType.StaticBody) {
     body.setLinearVelocity(velocity);
     body.setAngularVelocity(rotation);
   } else {
     // body is dynamic, so we apply an impulse
     body.applyLinearImpulse(velocity, body.getWorldCenter());
     body.applyAngularImpulse(rotation);
   }
 }
Пример #8
0
 private void inputUpdate(float deltaTime) {
   float horizontalForce = 0;
   if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
     horizontalForce -= 1f;
   }
   if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
     horizontalForce += 1f;
   }
   if (Gdx.input.isKeyPressed(Input.Keys.SPACE)) {
     player.applyForceToCenter(0, 50, false);
   }
   player.setLinearVelocity(horizontalForce * 5, player.getLinearVelocity().y);
 }
    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;
      }
    }
Пример #10
0
  private void jumpMario(final AnimatedSprite face) {
    final Body faceBody = (Body) face.getUserData();

    // float bufX = mario.getX();
    // float bufY = mario.getY();
    // mario.setVisible(false);
    // mario = marioJump;
    // mario.setPosition(bufX, bufY);
    // mario.setVisible(true);

    final Vector2 velocity =
        Vector2Pool.obtain(this.mGravityX * -1, (float) (this.mGravityY * -0.7));
    faceBody.setLinearVelocity(velocity);
    Vector2Pool.recycle(velocity);
  }
  private void processPhysicsMovement(
      Body body, InputComponent ic, Position pc, Velocity vc, Rotation rc, int id) {
    if (body == null) {
      logger.error("ERR: cannot process movement for ent#" + id + "; body == null");
      lifeCycleMapper.get(id).kill();
      return;
    }
    body.setLinearVelocity(0, 0);

    // accelerate
    Vector2 force = ic.direction.cpy().nor().scl(ic.speed);
    body.applyLinearImpulse(force, body.getWorldCenter(), true);

    // update entity
    pc.position.set(body.getPosition());
    vc.velocity.set(body.getLinearVelocity());
    rc.angle = MathUtils.radiansToDegrees * body.getAngle();
  }
Пример #12
0
  // can be performed in update
  public void maybeTeleport(SolGame game, SolShip owner) {
    if (!myShouldTeleport) return;

    TextureAtlas.AtlasRegion tex = game.getTexMan().getTex(TEX_PATH, null);
    float blipSz = owner.getHull().config.getApproxRadius() * 3;
    game.getPartMan().blip(game, owner.getPos(), SolMath.rnd(180), blipSz, 1, Vector2.Zero, tex);
    game.getPartMan().blip(game, myNewPos, SolMath.rnd(180), blipSz, 1, Vector2.Zero, tex);

    float newAngle = owner.getAngle() + myAngle;
    Vector2 newSpd = SolMath.getVec(owner.getSpd());
    SolMath.rotate(newSpd, myAngle);

    Body body = owner.getHull().getBody();
    body.setTransform(myNewPos, newAngle * SolMath.degRad);
    body.setLinearVelocity(newSpd);

    SolMath.free(newSpd);
  }
Пример #13
0
 public static Body createBullet(World world, GunShot gunshot, Vector2 origin) {
   Vector2 position = origin.cpy().add(gunshot.getDir().cpy().scl(.4f)),
       velocity = gunshot.getDir().cpy().scl(gunshot.getGun().getMuzzleVelocity());
   float angle = (float) Math.atan2(gunshot.getDir().y, gunshot.getDir().x);
   Body body =
       com.blastedstudios.gdxworld.physics.PhysicsHelper.createCircle(
           world,
           BULLET_RADIUS,
           position,
           angle,
           BodyType.DynamicBody,
           .2f,
           .5f,
           BULLET_DENSITY,
           gunshot.getBeing().getMask(),
           gunshot.getBeing().getCat(),
           (short) 0);
   body.setLinearVelocity(velocity);
   body.setBullet(true);
   body.setUserData(gunshot);
   return body;
 }
Пример #14
0
  public PatrolShip() {
    // Creating a box2D entity
    BodyDef circleDef = new BodyDef();
    circleDef.type = BodyDef.BodyType.DynamicBody;

    body = com.astrodestroyer.Game.b2World.createBody(circleDef);

    shape = new CircleShape();
    shape.setRadius(radius);

    fixtureDef = new FixtureDef();
    fixtureDef.shape = shape;
    fixtureDef.density = 1.0f;
    fixtureDef.friction = 0.0f;
    fixtureDef.restitution = 0.0f;
    fixtureDef.filter.groupIndex = 1;
    fixture = body.createFixture(fixtureDef);

    Random rnd = new Random();
    body.setTransform(
        (rnd.nextFloat() - 0.5f) * 2f, (rnd.nextFloat() - 0.5f) * 2f, rnd.nextFloat() * 2f - 1f);
    body.setLinearVelocity(rnd.nextFloat() * 0.5f - 0.25f, rnd.nextFloat() * 0.5f - 0.25f);
  }
  /**
   * Method is called on every collision between two Box2D bodies. Behaviour of certain body
   * collisions are defined in this method. Collisions between bodies are defined and accessed by
   * means of bitwise or-ing their category bits.
   */
  @Override
  public void beginContact(Contact c) {
    Fixture fa = c.getFixtureA();
    Fixture fb = c.getFixtureB();

    int collisionDefinition = fa.getFilterData().categoryBits | fb.getFilterData().categoryBits;

    switch (collisionDefinition) {
        // foot collides with floor
      case B2DVars.BIT_PLAYER_FOOT | B2DVars.BIT_BLOCKS:
        numJumps++;
        break;

        // Player collides with student, deal damage to player or kill enemy
      case B2DVars.BIT_PLAYER | B2DVars.BIT_STUDENT:
        System.out.println("hit!");
        playerStudentCollisionCount++;

        Vector2 playerPosition, playerVelocity, studentPosition;
        Body player, student;

        if (fa.getUserData().equals("player")) {
          player = fa.getBody();
          playerPosition = fa.getBody().getPosition();
          playerVelocity = fa.getBody().getLinearVelocity();
          student = fb.getBody();
          studentPosition = fb.getBody().getPosition();
        } else {
          player = fb.getBody();
          playerPosition = fb.getBody().getPosition();
          playerVelocity = fb.getBody().getLinearVelocity();
          student = fa.getBody();
          studentPosition = fa.getBody().getPosition();
        }

        // Player hits enemy from above, 0.1 offset is to ensure player is significantly above enemy
        if (playerPosition.y - 0.1 > studentPosition.y && playerVelocity.y <= 0) {
          Game.ass.getAudio("kill").play(1.0f);
          bodiesToKill.add(student);
          player.setLinearVelocity(player.getLinearVelocity().x, 2f);
        } else {
          ((Player) player.getUserData()).damagePlayer();
          Game.ass.getAudio("hit").play(1.0f);
          Vector2 point = new Vector2(playerPosition.x, playerPosition.y);

          if (playerPosition.x < studentPosition.x) {
            player.applyLinearImpulse(new Vector2(-30f, 0), point, true);
          } else {
            player.applyLinearImpulse(new Vector2(30f, 0), point, true);
          }
        }
        break;

        // player collides with coin
      case B2DVars.BIT_PLAYER | B2DVars.BIT_COINS:
        if (fa.getUserData().equals("coin")) {
          // delete coin
          bodiesToKill.add(fa.getBody());
          Game.ass.getAudio("coin").play(1.0f);
        } else {
          bodiesToKill.add(fb.getBody());
          Game.ass.getAudio("coin").play(1.0f);
        }
        break;

        // player collides with end of level region
      case B2DVars.BIT_PLAYER | B2DVars.BIT_END:
        if (fa.getUserData().equals("end")) {
          Player playerB = (Player) fb.getBody().getUserData();
          if (playerB.getNumCoins() == playerB.getNumCoinsToCollect()) {
            playerB.setLevelCompleteBool(true);
            break;
          }
          playerB.setLevelCompleteBool(false);
          break;
        } else {
          Player playerA = (Player) fa.getBody().getUserData();
          if (playerA.getNumCoins() == playerA.getNumCoinsToCollect()) {
            playerA.setLevelCompleteBool(true);
            break;
          }
          playerA.setLevelCompleteBool(false);
          break;
        }
    }
  }
Пример #16
0
 public void jump() {
   if (footContacts < 1) {
     return;
   }
   body.setLinearVelocity(new Vector2(body.getLinearVelocity().x, 12));
 }
Пример #17
0
  @Override
  public void draw() {
    Gdx.gl.glClearColor(0, 0.2f, 1, 1);
    camera.position.interpolate(
        new Vector3(camera.position.x, camY, 0), 0.1f, Interpolation.pow2Out);
    camera.update();
    batch.setProjectionMatrix(camera.combined);

    if (!pause) world.step(1 / 60f, 8, 3);

    world.getBodies(bodies);
    batch.begin();
    for (i = 0; i < 5; i++) {
      if (backgrounds[i]
          .getBoundingRectangle()
          .overlaps(
              new Rectangle(
                  camera.position.x - camera.viewportWidth / 2,
                  camera.position.y - camera.viewportHeight / 2,
                  camera.viewportWidth,
                  camera.viewportHeight))) {
        backgrounds[i].draw(batch);
      }
    }
    traySprite.draw(batch);
    mpHandler.update(
        Gdx.graphics.getDeltaTime(), true, batch, tap, new Vector2(unprojected.x, unprojected.y));
    for (i = 0; i < bodies.size; i++) {
      scbd = bodies.get(i);
      if (scbd != tmpbd) {
        bodyY.add(scbd.getPosition().y);
      }
      if (scbd.getWorldCenter().y < -2
          || scbd.getWorldCenter().x < -1.3f
          || scbd.getWorldCenter().x > 6.5f) {
        mpHandler.removeFromBody(scbd);
        world.destroyBody(scbd);
        if (!gameOver) {
          if (score > MainClass.highScore) {
            newHighScoreSound.play(MainClass.sound);
          } else {
            gameOverSound.play(MainClass.sound);
          }
          gameOver = true;
          scoreLabel.addAction(new AlphaAction());
          muffinSource.remove();
          pauseButton.remove();
          gameOverDialog.show(this, score);
        }
      }
      if (scbd.getType() == BodyDef.BodyType.DynamicBody) {
        AdjustToBody(muffinSprite, muffinOrigin, scbd, 1.228f, 1);
        muffinSprite.draw(batch);
        if (scbd.isAwake()) {
          if (Math.abs(scbd.getLinearVelocity().len2()) > 10) {
            AdjustToBody(scaredmuffinface, muffinOrigin, scbd, 1.228f, 1);
            scaredmuffinface.draw(batch);
          } else {
            AdjustToBody(awakemuffinface, muffinOrigin, scbd, 1.228f, 1);
            awakemuffinface.draw(batch);
          }
        } else {
          AdjustToBody(normalmuffinface, muffinOrigin, scbd, 1.228f, 1);
          normalmuffinface.draw(batch);
        }
      }
    }
    batch.end();
    camY = 0;
    if (bodyY.size > 5) {
      bodyY.sort();
      for (i = bodyY.size - 3; i < bodyY.size; i++) {
        camY += bodyY.get(i);
      }
      camY /= 3;
      camY += 2;
    }
    if (camY < 4) camY = 4;
    if (drag) {
      tmpbd.setLinearVelocity(
          (unprojected.x - 0.5f - tmpbd.getPosition().x) * 20,
          (unprojected.y - 0.5f - tmpbd.getPosition().y) * 20);
    }
    super.draw();
    bodies.clear();
    bodyY.clear();
  }
Пример #18
0
 public void resetPosition() {
   body.setLinearVelocity(0, 0);
   body.setTransform(startPosition, 0);
 }
Пример #19
0
 // Set velocity of all bodies
 public void setLinearVelocity(Vector2 vel) {
   for (Body b : scene.getBodies()) {
     b.setLinearVelocity(vel);
   }
 }
Пример #20
0
  @Override
  public void create() {
    cam = new OrthographicCamera(48, 32);
    cam.position.set(0, 15, 0);
    cam.update();

    Box2D.init();
    world = new World(new Vector2(0, -9.8f), true);
    dDebugRenderer = new Box2DDebugRenderer();

    spawnBoxes = false;
    // GROUND
    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyDef.BodyType.StaticBody;
    bodyDef.position.set(0, 0);

    ground = world.createBody(bodyDef);
    PolygonShape shape = new PolygonShape();
    shape.setAsBox(15, 1);

    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = shape;
    fixtureDef.density = 0f;

    ground.createFixture(fixtureDef);

    // CIRCLE
    bodyDef.type = BodyDef.BodyType.DynamicBody;
    bodyDef.position.set(0, 10);

    circle = world.createBody(bodyDef);

    CircleShape circleShape = new CircleShape();
    circleShape.setRadius(1);

    fixtureDef.shape = circleShape;
    fixtureDef.density = 10;
    fixtureDef.restitution = 1;

    circle.createFixture(fixtureDef);

    // BOX
    bodyDef.type = BodyDef.BodyType.DynamicBody;
    bodyDef.position.set(0, 15);

    box = world.createBody(bodyDef);

    bodyDef.bullet = false;
    shape.setAsBox(1, 1);

    fixtureDef.friction = 100f;
    fixtureDef.restitution = 0.5f;
    fixtureDef.shape = shape;
    fixtureDef.density = 2;

    box.createFixture(fixtureDef);

    // PLATFORM
    bodyDef.type = BodyDef.BodyType.KinematicBody;
    bodyDef.position.set(-10, 6);

    platform = world.createBody(bodyDef);

    shape.setAsBox(3, .5f);

    fixtureDef.shape = shape;
    fixtureDef.density = 0;
    fixtureDef.friction = 7;
    fixtureDef.restitution = 0;

    platform.createFixture(fixtureDef);

    Gdx.input.setInputProcessor(new MyInputProcessor());

    platform.setLinearVelocity(PLATFORM_VELOCITY, 0);
  }