@Override public void onContact(final Body eA, final Body eB) { AuraEntity entityA = null; AuraEntity entityB = null; if (eA != null && eA.getUserData() instanceof AuraEntity) entityA = (AuraEntity) eA.getUserData(); if (eB != null && eB.getUserData() instanceof AuraEntity) entityB = (AuraEntity) eB.getUserData(); // PROJECTILES AuraPersonnageInfo projOwner = null; AuraEntity entityShot = null; if (entityB != null && entityB instanceof AuraEntityProjectile && entityB.isAlive()) { projOwner = ((AuraEntityProjectile) entityB).getOwner(); entityB.hibernate(); if (entityA != null && entityA.isAlive()) entityShot = entityA; } if (entityA != null && entityA instanceof AuraEntityProjectile && entityA.isAlive()) { projOwner = ((AuraEntityProjectile) entityA).getOwner(); entityA.hibernate(); if (entityB != null && entityB.isAlive()) entityShot = entityB; } if (projOwner != null && entityShot != null) { getEngine().log("PROJECTILE HIT ENTITY!"); // $NON-NLS-1$ getMapPrinter().projectileHitEntity(projOwner, entityShot); } }
public GameScreen(final IkeGame game) { this.game = game; // Creates the world manager with the specified map worldManager = new WorldManager("test_map.tmx"); // Creates the player at the specified coordinates worldManager.createPlayer(1.5f, 5.5f); // Sets this class's ike body instance to the world manager's for convenience ikeBody = worldManager.player; // Gets the ike instance from ike's body ike = (Ike) ikeBody.getUserData(); // Creates the camera camera = new OrthographicCamera(); // Makes the camera render 32 tiles wide and 18 tiles high camera.setToOrtho(false, 32, 18); // Updates the camera camera.update(); // Creates the shape renderer shapeRenderer = new ShapeRenderer(); // Sets the input listener Gdx.input.setInputProcessor(new ControlListener()); }
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); }
public void remove(Array<RobotNinjas> robotNinjases) { if (body.getUserData() == Constants.deadID) { robotNinjases.removeValue(this, true); robotNinjasPool.free(this); } if (body.getPosition().x < 0 - width) { body.setUserData(Constants.deadID); if (!dead) { Constants.currentScoreValue++; Constants.standardVelocity.x += Constants.globalAcceleration; Constants.ninjaVelocity.x += Constants.globalAcceleration; Constants.globalAcceleration -= Constants.globalAccelerationDecline; } dead = true; } }
public static boolean bodyinBounds(Body body) { UserData userData = (UserData) body.getUserData(); switch (userData.getUserDataType()) { case RIGHTARM: case ENEMYRIGHT: case GROUND: case ENEMYRIGHT2: case ENEMYRIGHT3: case ENEMYRIGHT4: case FIGHTER: case ENEMYLEFT: case ENEMYLEFT2: case ENEMYLEFT3: case ENEMYLEFT4: return body.getPosition().x + userData.getWidth() > 0; } return true; }
@Override public void render() { Gdx.gl.glClearColor(0, 0, 0, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); camera.position.set(circleBody.getPosition().x, circleBody.getPosition().y, 0); camera.update(); renderer.render(world, camera.combined); System.out.println( "player.getPosition().x = " + player.getPosition().x + "\nplayer.getPosition().y = " + player.getPosition().y + "\ncamera.position = " + camera.position); Sprite sprite; sprite = (Sprite) circleBody.getUserData(); // set position and width and height and makes sure it is in the center sprite.setBounds( convertToWorld(circleBody.getPosition().x) - sprite.getWidth() / 2, convertToWorld(circleBody.getPosition().y) - sprite.getHeight() / 2, convertToWorld(circleShape.getRadius() * 2), convertToWorld(circleShape.getRadius() * 2)); tiledMapRenderer.setView(camera); tiledMapRenderer.render(); System.out.println( "Bouncing circle: " + circleBody.getMass() + "\n" + "Player: " + player.getMass()); // world.step(1/45f, 6, 2); world.step(Gdx.graphics.getDeltaTime(), 4, 4); player.setAwake(true); // camera.project(point.set(player.getPosition().x, player.getPosition().y, 0)); // logger.log(); batch.begin(); // Circle.draw(batch); sprite.draw(batch); batch.end(); }
public void update(float dt) { handleInput(); world.step(dt, 6, 2); Array<Body> bodies = cl.getBodiesToKill(); for (int i = 0; i < bodies.size; i++) { Body b = bodies.get(i); coins.removeValue((Coin) b.getUserData(), true); world.destroyBody(b); player.collectCoin(); } bodies.clear(); player.update(dt); for (int i = 0; i < coins.size; i++) { coins.get(i).update(dt); } if (player.getBody().getLinearVelocity().x > 0) { Game.ass.loadTexture("res/images/bunny.png", "bunny"); } else if (player.getBody().getLinearVelocity().x < 0) { Game.ass.loadTexture("res/images/bunny2.png", "bunny"); } }
@SuppressWarnings("Duplicates") public void handleContact(Body a, Body b) { boolean playerHitAsteroid = (WorldHandler.isAsteroid(a) && WorldHandler.isPlayer(b)) || WorldHandler.isPlayer(a) && WorldHandler.isAsteroid(b); boolean bulletHitAsteroid = (a.isBullet() && WorldHandler.isAsteroid(b)) || b.isBullet() && WorldHandler.isAsteroid(a); boolean playerHitPickup = (WorldHandler.isPlayer(a) && WorldHandler.isPickup(b)) || (WorldHandler.isPickup(a) && WorldHandler.isPlayer(b)); Body asteroid; Body player; final Body bullet; if (playerHitAsteroid) { if (WorldHandler.isAsteroid(a)) { asteroid = a; player = b; } else { asteroid = b; player = a; } PlayerActorData p_data = (PlayerActorData) player.getUserData(); AsteroidActorData a_data = (AsteroidActorData) asteroid.getUserData(); if (a_data.subHealth(5) <= 0) { a_data.isRemoved = true; stage.addDead(asteroid); } asteroid.applyForceToCenter(new Vector2(0, 100f), true); if (p_data.subHealth(a_data.getDamage()) <= 0) { // game over Gdx.app.log("plr", "plr dead"); // todo player death } if (infoLabel != null) infoLabel.setText("Player HP: " + p_data.getHealth()); playerHPBar.setBarWidth((int) p_data.getHealth()); } if (bulletHitAsteroid) { if (a.isBullet()) { bullet = a; asteroid = b; } else { bullet = b; asteroid = a; } ActorData b_data = (ActorData) bullet.getUserData(); AsteroidActorData a_data = (AsteroidActorData) asteroid.getUserData(); new Timer() .scheduleTask( new Timer.Task() { @Override public void run() { createExplosion(bullet.getPosition().x + 1f, bullet.getPosition().y + 0.7f); } }, 0.05f); b_data.isRemoved = true; stage.addDead(bullet); playerHandler.addCurrency(2); if (a_data.subHealth(WorldHandler.getProjectileDamage(bullet)) <= 0) { Gdx.app.log("OGH", "Asteroid dead - Spawn pickup?"); if (random.nextInt(100) > 70) { final Body finalAsteroid = asteroid; new Timer() .scheduleTask( new Timer.Task() { @Override public void run() { createPickup(finalAsteroid.getPosition().x, finalAsteroid.getPosition().y); } }, 2); } stage.addDead(asteroid); a_data.isRemoved = true; } } Body pickup; if (playerHitPickup) { if (WorldHandler.isPlayer(a)) { player = a; pickup = b; } else { player = b; pickup = a; } PickupData pData = (PickupData) pickup.getUserData(); PlayerActorData plData = (PlayerActorData) player.getUserData(); pData.isRemoved = true; stage.addDead(pickup); if (pData.getType().equals(Constants.CURRENCY_PICKUP_ASSET_ID)) playerHandler.addCurrency(100); else plData.addHealth(50); // todo magic numbers } }
public static boolean bodyIsRightArm(Body body) { UserData userData = (UserData) body.getUserData(); return userData != null && userData.getUserDataType() == UserDataType.RIGHTARM; }
public static boolean bodyIsLeftEnemy4(Body body) { UserData userData = (UserData) body.getUserData(); return userData != null && userData.getUserDataType() == UserDataType.ENEMYLEFT4; }
public static boolean bodyIsFighter(Body body) { UserData userData = (UserData) body.getUserData(); return userData != null && userData.getUserDataType() == UserDataType.FIGHTER; }
public static boolean bodyIsGround(Body body) { UserData userData = (UserData) body.getUserData(); return userData != null && userData.getUserDataType() == UserDataType.GROUND; }
public static boolean bodyIsEnemyRight4(Body body) { UserData userData = (UserData) body.getUserData(); return userData != null && userData.getUserDataType() == UserDataType.ENEMYRIGHT4; }
/** * 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; } } }