Created
July 16, 2016 18:09
-
-
Save piedoom/8b41a0da0f0cde30d1da604a8cd2a915 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.Xna.Framework; | |
using Microsoft.Xna.Framework.Content; | |
using Microsoft.Xna.Framework.Graphics; | |
using Nez; | |
using Nez.Sprites; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using Vapor.Code.Manager; | |
namespace Vapor.Code.Player | |
{ | |
class Player : Component, IUpdatable | |
{ | |
const float accelerationTimeAirborne = 0.1f; | |
const float accelerationTimeAfterWallJump = 0.02f; | |
const float accelerationTimeGrounded = 0.3f; | |
float currentAcceleration; | |
bool inWallJump = false; | |
float moveSpeed = 6; | |
float maxJumpHeight = 2; | |
float minJumpHeight = 0.4f; | |
float timeToJumpApex = 0.4f; | |
float velocityXSmoothing; | |
float gravity; | |
float maxJumpVelocity; | |
float minJumpVelocity; | |
public BoxController2D controller; | |
Vector2 velocity; | |
public override void onAddedToEntity() | |
{ | |
var texture = entity.scene.content.Load<Texture2D>("Sprites/Player/PlayerTest"); | |
var sprite = this.addComponent(new Sprite(texture)); | |
var collider = this.addCollider<BoxCollider>(new BoxCollider(sprite.width, sprite.height)); | |
sprite.origin = collider.origin; | |
controller = this.addComponent(new BoxController2D(sprite, collider)); | |
// calculate jump height and gravity | |
gravity = (float)((2 * maxJumpHeight) / Math.Pow(timeToJumpApex, 2)); | |
maxJumpVelocity = - Math.Abs(gravity) * timeToJumpApex - 0.1f; | |
minJumpVelocity = - (float)Math.Sqrt(2 * Math.Abs(gravity) * minJumpHeight); | |
} | |
public void update() | |
{ | |
if (controller.collisions.above || controller.collisions.below) | |
{ | |
velocity.Y = 0; | |
} | |
velocity.Y += gravity * Time.deltaTime; | |
// Jumping | |
if (InputManager.Instance.Input.Jump.isPressed && controller.collisions.below) | |
{ | |
velocity.Y = maxJumpVelocity; | |
} | |
// wall jumping | |
if (InputManager.Instance.Input.Jump.isPressed && !controller.collisions.below && (controller.collisions.left || controller.collisions.right)) | |
{ | |
velocity.Y = maxJumpVelocity; | |
velocity.X = moveSpeed * -Math.Sign(velocity.X); | |
inWallJump = true; | |
} | |
// when jump key off | |
if (InputManager.Instance.Input.Jump.isReleased) | |
{ | |
if (velocity.Y < minJumpVelocity) | |
velocity.Y = minJumpVelocity; | |
} | |
// on ground | |
if (controller.collisions.below) | |
{ currentAcceleration = accelerationTimeGrounded; inWallJump = false; } | |
// in air | |
else if (!controller.collisions.below && !inWallJump) | |
currentAcceleration = accelerationTimeAirborne; | |
// after wall jump | |
else if (!controller.collisions.below && inWallJump && velocity.Y < 0) | |
currentAcceleration = accelerationTimeAfterWallJump; | |
// stop wall jump acceleration time after apex of jump | |
else if (!controller.collisions.below && inWallJump && velocity.Y > 0) | |
{ currentAcceleration = accelerationTimeAirborne; inWallJump = false; } | |
var targetVelocityX = InputManager.Instance.Input.InputX.value * moveSpeed; | |
velocity.X = MathHelper.Lerp(velocity.X, targetVelocityX, currentAcceleration); | |
controller.Move(velocity); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment