Sample game. Easing methods. Colors.

This commit is contained in:
Matt Thorson
2020-05-08 21:05:29 -07:00
parent 786b692a3f
commit 12ea2e43bc
18 changed files with 545 additions and 20 deletions

View File

@ -0,0 +1,22 @@
namespace Strawberry.Sample
{
static public class Controls
{
static public VirtualAxis MoveX;
static public VirtualButton Jump;
static public void Init()
{
Jump = new VirtualButton()
.AddKey(.Space)
.AddKey(.X)
.AddButton(0, .A)
.PressBuffer(0.1f);
MoveX = new VirtualAxis()
.AddKeys(.Left, .Right, .TakeNewer)
.AddButtons(0, .DpadLeft, .DpadRight, .TakeNewer)
.AddAxis(0, .LeftX, 0.3f);
}
}
}

13
SampleGame/src/Level.bf Normal file
View File

@ -0,0 +1,13 @@
namespace Strawberry.Sample
{
public class Level : Scene
{
public this()
{
Add(new Player(.(50, 50)));
Add(new Solid(.(0, 168), .(0, 0, 320, 12)));
Add(new JumpThru(.(200, 132), 48));
Add(new MovingJumpThru(.(136, 100), 32, .(124, 140), 2f));
}
}
}

View File

@ -0,0 +1,50 @@
namespace Strawberry.Sample
{
public class MovingJumpThru : JumpThru
{
private Point moveFrom;
private Point moveTo;
private float moveTime;
private float movingLerp;
private bool movingPositive;
public this(Point pos, int width, Point moveTo, float moveTime)
: base(pos, width)
{
moveFrom = Position;
this.moveTo = moveTo;
this.moveTime = moveTime;
movingLerp = 0;
movingPositive = true;
}
public override void Update()
{
base.Update();
if (movingPositive)
{
movingLerp += Time.Delta / moveTime;
if (movingLerp >= 1)
{
movingLerp = 1;
movingPositive = false;
}
}
else
{
movingLerp -= Time.Delta / moveTime;
if (movingLerp <= 0)
{
movingLerp = 0;
movingPositive = true;
}
}
let target = Vector.Lerp(moveFrom, moveTo, Ease.CubeInOut(movingLerp));
MoveTo(target);
}
}
}

108
SampleGame/src/Player.bf Normal file
View File

@ -0,0 +1,108 @@
using System;
namespace Strawberry.Sample
{
public class Player : Actor
{
public Vector Speed;
private Timer tJumpGrace;
private Timer tVarJump;
public this(Point pos)
: base(pos)
{
Hitbox = Rect(-4, -8, 8, 8);
Add(tJumpGrace = new Timer());
Add(tVarJump = new Timer());
}
public override void Update()
{
base.Update();
const float coyoteTime = 0.1f; // Time after leaving a ledge when you can still jump
const float varJumpTime = 0.2f; // Time after jumping that you can hold the jump button to continue gaining upward speed
const float jumpSpeed = -160;
const float jumpXBoost = 30; // If left or right is held at the moment of a jump, this horizontal speed boost is applied
const float maxFall = 160;
const float gravity = 1000;
const float halfGravThreshold = 40; // Halves gravity at the peak of a jump, if the jump button is held
const float maxRun = 100;
const float runAccel = 800;
const float runAccelAirMult = 0.8f; // Gives you slightly less control of horizontal motion in the air
let onGround = GroundCheck();
if (onGround)
tJumpGrace.Value = coyoteTime;
//Vertical Control
{
//Jumping
if (tJumpGrace && Controls.Jump.Pressed)
{
Controls.Jump.ClearPressBuffer();
tJumpGrace.Clear();
tVarJump.Value = varJumpTime;
Speed.Y = jumpSpeed;
Speed.X += jumpXBoost * Controls.MoveX.Valuei;
}
else
{
//Gravity
{
float mult;
if (Controls.Jump.Check && Math.Abs(Speed.Y) <= halfGravThreshold)
mult = 0.5f;
else
mult = 1;
Speed.Y = Calc.Approach(Speed.Y, maxFall, gravity * mult * Time.Delta);
}
//Variable Jumping
if (tVarJump)
{
if (Controls.Jump.Check)
Speed.Y = jumpSpeed;
else
tVarJump.Clear();
}
}
}
//Horizontal Control
{
float accel = runAccel;
if (!onGround)
accel *= runAccelAirMult;
Speed.X = Calc.Approach(Speed.X, Controls.MoveX.Valuei * maxRun, accel * Time.Delta);
}
//Resolve Speed
MoveX(Speed.X * Time.Delta, scope => OnCollideX);
MoveY(Speed.Y * Time.Delta, scope => OnCollideY);
}
private void OnCollideX(Collision col)
{
Speed.X = 0;
ZeroRemainderX();
}
private void OnCollideY(Collision col)
{
Speed.Y = 0;
ZeroRemainderY();
}
public override void Draw()
{
base.Draw();
DrawHitbox(.Red);
}
}
}

14
SampleGame/src/Program.bf Normal file
View File

@ -0,0 +1,14 @@
using System;
namespace Strawberry.Sample
{
static public class Program
{
static public int Main(String[] args)
{
let game = scope SampleGame();
game.Run();
return 0;
}
}
}

View File

@ -0,0 +1,14 @@
using System;
namespace Strawberry.Sample
{
public class SampleGame : Game
{
public this()
: base("Strawberry Sample Game!", 320, 180, 3)
{
Controls.Init();
Scene = new Level();
}
}
}