public static List<Vector2> getPoints(List<Vector2> path, float length, boolean meters) { float factor = 1; if (meters) factor = PhysicsSystem.METERS_PER_PIXEL; length *= factor; List<Vector2> result = new ArrayList<>(); result.add(path.get(0).scl(factor)); for (int i = 0; i < path.size() - 1; ++i) { Vector2 p0 = result.get(result.size() - 1); Vector2 p1 = path.get(i + 1).scl(factor); float dx = p1.x - p0.x; float dy = p1.y - p0.y; float d = (float) Math.sqrt(dx * dx + dy * dy); int np = (int) Math.floor(d / length); float stepX = dx / np; float stepY = dy / np; float x = p0.x; float y = p0.y; for (int j = 0; j < np; ++j) { x += stepX; y += stepY; result.add(new Vector2(x, y)); } } return result; }
@Override public void render() { camera.update(); // Step the physics simulation forward at a rate of 60hz world.step(1f / 60f, 6, 2); sprite.setPosition( (body.getPosition().x * PIXELS_TO_METERS) - sprite.getWidth() / 2, (body.getPosition().y * PIXELS_TO_METERS) - sprite.getHeight() / 2); sprite.setRotation((float) Math.toDegrees(body2.getAngle())); sprite2.setPosition( (body2.getPosition().x * PIXELS_TO_METERS) - sprite2.getWidth() / 2, (body2.getPosition().y * PIXELS_TO_METERS) - sprite2.getHeight() / 2); sprite2.setRotation((float) Math.toDegrees(body.getAngle())); Gdx.gl.glClearColor(1, 1, 1, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); batch.setProjectionMatrix(camera.combined); batch.begin(); batch.draw( sprite, sprite.getX(), sprite.getY(), sprite.getOriginX(), sprite.getOriginY(), sprite.getWidth(), sprite.getHeight(), sprite.getScaleX(), sprite.getScaleY(), sprite.getRotation()); batch.draw( sprite2, sprite2.getX(), sprite2.getY(), sprite2.getOriginX(), sprite2.getOriginY(), sprite2.getWidth(), sprite2.getHeight(), sprite2.getScaleX(), sprite2.getScaleY(), sprite2.getRotation()); batch.end(); }
@Override public void onGraphicRender(GdxGraphics g) { /* Update the world physics */ PhysicsWorld.updatePhysics(); /* Sync signal emulation using system time */ boolean sync_signal = new TimeFloat().second % 60.0 == 0.0; /* * Second hand logic * * Stop when vertical, start when sync_sygnal */ if (sync_signal) { motor_seconds.setMotorSpeed(MOTOR_SPEED_SECOND); } else { double angle = -hand_second.getBody().getAngle() % (2 * Math.PI); if (angle > 2 * Math.PI * 0.995 && motor_seconds.getSpeed() != 0.0) { motor_seconds.setMotorSpeed(0.0f); } } /* * Minute hand logic * * Move from 1 minute when sync_signal */ if (sync_signal) { motor_minutes.setMotorSpeed(MOTOR_SPEED_MINUTE); } else { motor_minutes.setMotorSpeed(0.0f); } /* Get the size of the window */ final int w = getWindowWidth(); final int h = getWindowHeight(); /* Clear the graphic to draw the new image */ g.clear(); if (debug_rendering) { debugRenderer.render(world, g.getCamera().combined); g.setColor(Color.WHITE); } else { /* Create a nice grey gradient for the background */ for (int i = 0; i <= h; i++) { float c = 255 - i * 0.3f; g.setColor(new Color(c / 255, c / 255, c / 255, 1.0f)); g.drawLine(0, h - i, w, h - i); } /* Draw the clock frame */ g.drawPicture(CLOCK_CENTER.x, CLOCK_CENTER.y, bitmapClock); /* Draw the hands */ g.drawTransformedPicture( CLOCK_CENTER.x, CLOCK_CENTER.y, (float) (Math.toDegrees(hand_hour.getBody().getAngle())), 1.0f, bitmapHour); g.drawTransformedPicture( CLOCK_CENTER.x, CLOCK_CENTER.y, (float) (Math.toDegrees(hand_minute.getBody().getAngle())), 1.0f, bitmapMinute); g.drawTransformedPicture( CLOCK_CENTER.x, CLOCK_CENTER.y, (float) (Math.toDegrees(hand_second.getBody().getAngle())), 1.0f, bitmapSecond); g.setColor(Color.BLACK); } /* Display time in text */ g.drawString(w - 200, h - 10, "Famous clock from\r\n" + "the Swiss Railways."); g.drawString(w - 200, h - 80, displayTime()); g.drawSchoolLogo(); }
public void move() { m_slipTicks++; boolean alignX = false; if (m_ignoreHits > 0) { m_ignoreHits--; } if (m_bounceTicks > 0) { m_bounceTicks--; } else { m_bounce = false; } if (m_attacking) { if (m_attackTicks > 0) { m_attackTicks--; } else { m_attacking = false; m_attackPauseTicks = m_activeWeapon.getPauseTicks(); } } else if (m_attackPauseTicks > 0) { m_attackPauseTicks--; } if (m_focus) { m_gameLayer.playerSprite.setPosition(this.getX() + this.getWidth() / 2 - 20, this.getY()); if (((m_inputManager.isJumpPressed()) || (m_inputManager.isDownPressed())) && m_canExit) { m_gameLayer.playerSprite.exitBuggy( this.getX() + this.getWidth() / 2 - 20, this.getY(), m_currDir); m_focus = false; } } // Move Y Now if (m_inFloat) { this.accelerate(0, -(m_gravity / 4f)); } else { this.accelerate(0, -m_gravity); } // check for collisions above, below, right, and left. float xx = this.getX() + m_dx + this.getWidth() / 2; float bottomY = this.getY() + m_dy; float topY = this.getY() + this.getHeight(); int tileX = (int) Math.floor(xx / tw); int tileY = (int) Math.floor(bottomY / th); // this.getCollisions(tileX,tileY, m_platformTiles); // this.getPlatformCollisions(xx,this.getY() + m_dy); int lc = getCellAt(tileX, tileY); if (m_collisionPlatform == null) { m_onPlatform = false; m_activePlatform = null; } // If moving down if (m_dy < 0) { if (lc >= 0) { this.setPosition(this.getX(), (tileY + 1) * th); m_onGround = true; m_doubleJumping = false; this.playLandSound(); m_dy = 0; } else if (m_collisionPlatform != null) { // hit a platform this.setPosition( this.getX(), (m_collisionPlatform.getY() + m_collisionPlatform.getHeight() - 1f)); m_onGround = true; m_onPlatform = true; m_activePlatform = m_collisionPlatform; m_doubleJumping = false; m_onLadder = false; m_climbing = false; this.playLandSound(); m_dy = 0; } else { m_onGround = false; // THIS IS NEW } } else { // moving up if (m_dy > 0) { tileY = (int) Math.floor(topY / th); lc = getCellAt(tileX, tileY); if (lc >= 0) { m_dy = 0; this.setPosition(this.getX(), (tileY * th) - this.getHeight()); m_onGround = false; m_climbing = false; m_doubleJumping = false; } else { m_onGround = false; } } } if (m_onGround) { bottomY = this.getY() + m_dy; int nny = (int) Math.floor(bottomY / th) - 1; int cellUnder = getCellAt(tileX, nny); if ((cellUnder > 8) && (cellUnder < 11)) { m_slip = true; m_slipTicks = 0; } else { m_slip = false; } if (cellUnder == 4) { this.dieNow(); return; } } // end vertical move if ((!m_bounce) && m_focus) { float cx = 0.7f; if (m_inFloat) cx = cx / 2f; if (m_inputManager.isLeftPressed()) { this.accelerate(-cx, 0); } else if (m_inputManager.isRightPressed()) { this.accelerate(cx, 0); } else { // no left, right...so add horizontal drag if (m_onGround == false) { if (m_slipTicks > 60) { m_dx = this.applyDrag(m_dx, m_horizontalDragFactor); } else { m_dx = this.applyDrag(m_dx, m_horizontalDragFactor / 4f); } } else { m_dx = this.applyDrag(m_dx, m_horizontalDragFactor); } } } if (m_focus == false) { m_dx = this.applyDrag(m_dx, m_horizontalDragFactor); } if ((m_attacking) && (m_onGround) && (m_activeWeapon.stopMovingWhenAttacking()) && (!m_slip)) { m_dx = 0; } // check collisions X // check for collisions above, below, right, and left. float leftX = this.getX() + m_dx; float middleX = this.getX() + m_dx + this.getWidth() / 2; float rightX = this.getX() + m_dx + this.getWidth(); float yy = this.getY(); tileX = (int) Math.floor(leftX / tw); tileY = (int) Math.floor(yy / th); // collisions on left side? if (m_dx < 0) // moving left { lc = getCellAt(tileX, tileY); int tlc = getCellAt(tileX, tileY + 1); int ttlc = getCellAt(tileX, tileY + 2); if ((lc >= 0) || (tlc > 0) || (ttlc > 0)) { float correctX = (tileX + 1) * tw; float nx = correctX - this.getX(); if (nx > 0) { m_dx = 0; this.setPosition(this.getX() + 1, this.getY()); } else { m_dx = nx; } } if ((lc == 5) || (tlc == 5) || (ttlc == 5)) { this.dieNow(); return; } } else if (m_dx > 0) // moving right { tileX = (int) Math.floor(rightX / tw); lc = getCellAt(tileX, tileY); int tlc = getCellAt(tileX, tileY + 1); int ttlc = getCellAt(tileX, tileY + 2); if ((lc >= 0) || (tlc > 0) || (ttlc > 0)) { float correctX = (tileX * tw) - this.getWidth(); float nx = correctX - this.getX(); if (nx < 0) { m_dx = 0; this.setPosition(this.getX() - 1, this.getY()); } else { m_dx = nx; } } if ((lc == 6) || (tlc == 6) || (ttlc == 6)) { this.dieNow(); return; } } else if (m_dx == 0) { lc = getCellAt(tileX, tileY); tileX = (int) Math.floor(rightX / tw); int rc = getCellAt(tileX, tileY); if (lc > 0) { this.setPosition(this.getX() + 1, this.getY()); } else if (rc > 0) { this.setPosition(this.getX() - 1, this.getY()); } } if (m_onGround) { if (!m_attacking) { if (m_dx != 0) { if (!m_walkAnimation.isRunning()) { this.stopAllAnimations(); this.stopSound("buggyDrive"); this.runAnimation(m_walkAnimation); this.loopSound("buggyDrive", 0.3f); } } else { if (m_idleAnimation.isRunning()) { } else if (!m_standAnimation.isRunning()) { this.stopAllAnimations(); this.runAnimation(m_standAnimation); this.stopSound("buggyDrive"); } else if (m_standAnimation.isRunning()) { m_idleTicks++; if (m_idleTicks > 180) { this.stopAllAnimations(); this.runAnimation(m_idleAnimation); m_idleTicks = 0; } } } } } else if ((m_inFloat) && (!m_attacking)) { if (!m_floatAnimation.isRunning()) { this.stopAllAnimations(); this.runAnimation(m_floatAnimation); this.runAnimation(m_floatAnimation2); this.stopSound("buggyDrive"); } } if (m_dx > 0) { this.setScale(1, 1); m_currDir = 1; } else if (m_dx < 0) { this.setScale(-1, 1); m_currDir = -1; } if (m_lightDir != m_currDir) { m_lightDir = m_currDir; if (m_currDir > 0) { pLight.setDirection(0); m_lightX = this.getWidth() - 20; } else { m_lightX = 20; pLight.setDirection(180); } } pLight.setPosition(this.getX() + m_lightX, this.getY() + 45); m_inSwamp = false; m_inFloat = false; }
@Override public void accelerate(float ax, float ay) { float maxX = maxSpeedX; float maxY = maxSpeedY; float maxFall = maxFallVelocity; if (m_slip) { ax = ax / 2f; maxX = (maxX * 2f); } m_dx += ax; m_dy += ay; if (m_bounce) { maxX = 40; maxY = 40; maxFall = 40; } if ((m_inSwamp) && (m_dying == false)) { maxX = maxX / 6; maxFall = 4; } if (Math.abs(m_dx) > maxX) { if (!m_inSwamp) { if (m_slip) { m_dx = maxX * (m_dx / Math.abs(m_dx)); } else if (!m_onGround) { if (m_slipTicks > 30) { if (m_dx > 0) m_dx = m_dx - 0.5f; if (m_dx < 0) m_dx = m_dx + 0.5f; } else { if (m_dx > 0) { m_dx = (maxX * 2f); } else { m_dx = -(maxX * 2f); } } } else { if (m_dx > 0) m_dx = m_dx - 0.5f; if (m_dx < 0) m_dx = m_dx + 0.5f; } } else { m_dx = maxX * (m_dx / Math.abs(m_dx)); } } if (m_climbing) { if (Math.abs(m_dy) > maxY) { m_dy = maxY * (m_dy / Math.abs(m_dy)); } } else { if (m_dy < -maxFall) { m_dy = -maxFall; } if ((m_inSwamp) && (m_dying == false)) { if (m_dy > 3f) m_dy = 3f; } if ((m_inFloat) && (m_dying == false)) { if (m_dy < m_floatMaxYDown) { // m_dy += 0.25f; m_dy = m_floatMaxYDown; } } } }
@Override public void update(float deltaTime) { if (m_pause) return; if ((m_energy != 1) && (m_heartbeatSoundId > 0)) { this.stopSound("heartbeat"); m_heartbeatSoundId = -1; } if (m_levelEnding) { if (m_heartbeatSoundId > 0) { this.stopSound("heartbeat"); m_heartbeatSoundId = -1; } if (m_levelEndingFading == false) { this.translate(m_dx, m_dy); float deltaX = Math.abs(m_machineX - this.getX()); float deltaY = Math.abs(m_machineY - this.getY()); if ((deltaX < 15) && (deltaY < 15)) { m_levelEndingFading = true; this.runAnimation(m_levelEndingAnimation); } } else { if (m_levelEndingAnimation.isRunning() == false) { this.setVisible(false); m_alive = false; } } } else if (m_spawning) { if (!m_spawnAnimation.isRunning()) { m_spawning = false; } } else if (m_dying == false) { if (m_energy == 1) { if (m_heartbeatSoundId < 0) { m_heartbeatSoundId = this.loopSound("heartbeat", 0.25f); } } float ex = 0; if (m_activePlatform != null) { ex = m_activePlatform.getDX(); } if (m_pauseMoveTicks > 0) m_pauseMoveTicks--; if (m_inTeleport) { if (m_teleportState == 0) { if (m_teleportOut.isRunning() == false) { m_teleportState = 1; this.runAnimation(m_teleportAnimation); this.playSound("teleport", 0.75f); } } else if (m_teleportState == 1) { if (m_teleportAnimation.isRunning() == false) { m_teleportState = 2; this.runAnimation(m_teleportIn); this.playSound("teleportOut", 0.75f); } } else if (m_teleportState == 2) { if (m_teleportIn.isRunning() == false) { m_teleportState = 0; m_inTeleport = false; m_pauseMoveTicks = 0; } } } else if (m_pauseMoveTicks < 1) { this.move(); this.translate(m_dx + ex, m_dy); if ((m_dx != 0) || (m_dy != 0)) { lastMoveX = m_dx; lastMoveY = m_dy; } } } else if (m_dying == true) { if (m_deadState == 1) { if (m_deathAnimation.isRunning() == false) { pLight.setActive(false); m_deadState = 2; if (m_explodeDeath) { m_climbing = false; m_explodeDeath = false; m_deadState = 3; m_deadTicks = 0; m_dx = 0; m_dy = 0; } else { m_climbing = false; m_dy = 0; this.accelerate(0, 26f); maxFallVelocity = 128.0f; m_deadTicks = 0; m_dx = 0; this.playSound("falling", 0.9f); } } } else if (m_deadState == 2) { m_deadTicks++; this.accelerate(0, -(m_gravity * 1.5f)); this.translate(m_dx, m_dy); if (m_deadTicks > 90) { m_alive = false; m_deadState = 1; m_dying = false; m_explodeDeath = false; } } else if (m_deadState == 3) { m_deadTicks++; if (m_deadTicks > 130) { m_alive = false; m_deadState = 1; m_dying = false; m_explodeDeath = false; } } } }