Added the World Added Mobs, and spawning those mobs at runtime Added simple camera movement Added simple UI/HUD
77 lines
2.4 KiB
C#
77 lines
2.4 KiB
C#
using Godot;
|
|
using System;
|
|
|
|
public partial class Camera2d : Camera2D
|
|
{
|
|
private float targetZoom = 1.0f;
|
|
private float minZoom = .1f;
|
|
private float maxZoom = 1.0f;
|
|
private float zoomIncrement = .1f;
|
|
private float zoomRate = 8.0f;
|
|
|
|
public override void _Ready()
|
|
{
|
|
// Print the size of the viewport.
|
|
GD.Print("Viewport Resolution is: ", GetViewport().GetVisibleRect().Size);
|
|
}
|
|
|
|
public override void _Input(InputEvent @event)
|
|
{
|
|
// Mouse in viewport coordinates.
|
|
if (@event is InputEventMouseButton eventMouseButton)
|
|
{
|
|
GD.Print("Mouse Click/Unclick at: ", eventMouseButton.Position);
|
|
|
|
switch (eventMouseButton.ButtonIndex)
|
|
{
|
|
case MouseButton.Left:
|
|
Position = eventMouseButton.Position;
|
|
break;
|
|
case MouseButton.WheelDown:
|
|
zoomIn();
|
|
break;
|
|
case MouseButton.WheelUp:
|
|
zoomOut();
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
else if (@event is InputEventMouseMotion eventMouseMotion)
|
|
{
|
|
Vector2 zoomThreshold = new Vector2(.2f, .2f); // When should the zoom doesnt be accounted
|
|
Vector2 zoomThresholdSpeed = new Vector2(1.0f, 1.0f); // What value to use when the Threshold is reached
|
|
|
|
GD.Print("Mouse Motion at: ", eventMouseMotion.Position);
|
|
|
|
if (eventMouseMotion.ButtonMask == MouseButtonMask.Middle)
|
|
Position -= eventMouseMotion.Relative * (Zoom < zoomThreshold ? zoomThresholdSpeed : Zoom);
|
|
}
|
|
|
|
GD.Print("Camera Position: ", Position);
|
|
}
|
|
|
|
public override void _PhysicsProcess(double delta)
|
|
{
|
|
Zoom = Lerp(Zoom, targetZoom * Vector2.One, zoomRate * (float)delta);
|
|
if (Zoom.X == targetZoom)
|
|
SetPhysicsProcess(false);
|
|
|
|
GD.Print("Camera Zoom: ", Zoom);
|
|
}
|
|
|
|
private void zoomIn()
|
|
{
|
|
targetZoom = Single.Max(targetZoom - zoomIncrement, minZoom);
|
|
SetPhysicsProcess(true);
|
|
}
|
|
|
|
private void zoomOut()
|
|
{
|
|
targetZoom = Single.Min(targetZoom + zoomIncrement, maxZoom);
|
|
SetPhysicsProcess(true);
|
|
}
|
|
|
|
public static Vector2 Lerp(Vector2 from, Vector2 to, float weight) => from + (to - from) * weight;
|
|
}
|