Files
HillLike/HillLikeGame.cs

90 lines
2.1 KiB
C#

using osu.Framework;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Input.Events;
using osu.Framework.Platform;
using osuTK;
using osuTK.Graphics;
namespace HillLike;
public class HillLikeGame : Game
{
private World _world = null!;
[BackgroundDependencyLoader]
private void Load(GameHost host)
{
Add(new Container
{
RelativeSizeAxes = Axes.Both,
Children =
[
_world = new World(),
new Hud(_world)
]
});
}
protected override void Update()
{
base.Update();
_world.Step((float)(Clock.ElapsedFrameTime / 1000.0));
}
protected override bool OnKeyDown(KeyDownEvent e)
{
_world.HandleKeyDown(e);
return base.OnKeyDown(e);
}
protected override void OnKeyUp(KeyUpEvent e)
{
_world.HandleKeyUp(e);
base.OnKeyUp(e);
}
}
public class Hud : CompositeDrawable
{
private readonly Box _speedBar;
private readonly Box _speedBarBg;
private readonly World _world;
public Hud(World world)
{
this._world = world;
RelativeSizeAxes = Axes.Both;
InternalChildren =
[
_speedBarBg = new Box
{
Anchor = Anchor.TopLeft,
Origin = Anchor.TopLeft,
Position = new Vector2(20, 42),
Size = new Vector2(260, 10),
Colour = new Color4(30, 30, 30, 220)
},
_speedBar = new Box
{
Anchor = Anchor.TopLeft,
Origin = Anchor.TopLeft,
Position = new Vector2(20, 42),
Size = new Vector2(0, 10),
Colour = new Color4(120, 180, 255, 255)
}
];
}
protected override void Update()
{
base.Update();
var speed = _world.Car.Velocity.X;
_speedBar.Width = 260 * Math.Clamp(Math.Abs(speed) / 25f, 0, 1);
}
}