The local player's mouse controller for the isometric view. Tests the cursor against hardcoded screen rectangles to pick UI buttons, cycles the interact mode on attack2, raycasts for hovered entities and interactables, and sends the owner's NpcMovement to a clicked interactable.
using static Sandbox.PhysicsContact;
public class PlayerMouse : Component {
[Property, ReadOnly] public BasePlayer Owner {get;set;}
public enum Layer {
World,
Interface,
}
public enum Button {
None,
Swap,
Weapon,
}
public enum Mode {
Move,
Interact,
Shoot
}
[Property, ReadOnly] public Layer TargetLayer {get; set;} = Layer.World;
[Property, ReadOnly] public Button TargetButton {get; set;} = Button.None;
[Property] public Mode InteractMode {get; set;} = Mode.Move;
[Property, ReadOnly] public IsoEntity HoveredEntity {get; set;}
[Property, ReadOnly] public IsoInteractable HoveredInteract {get; set;}
[Property, ReadOnly] public Vector2 Position => Mouse.Position;
private bool InNodeOverride = false;
private Mode LastMode;
protected override void OnFixedUpdate() {
base.OnFixedUpdate();
TargetButton = Button.None;
TargetLayer = Layer.World;
var uipos = Position;
uipos.y -= Screen.Height;
if (uipos.x > 331 && uipos.y > -173 && uipos.x < 773 && uipos.y < -36)
TargetButton = Button.Weapon;
else if (uipos.x > 329 && uipos.y > -276 && uipos.x < 456 && uipos.y < -235)
TargetButton = Button.Swap;
if (uipos.x < 495 && uipos.y > -301)
TargetLayer = Layer.Interface;
if (uipos.x < 821 && uipos.y > -222)
TargetLayer = Layer.Interface;
if (Input.Pressed("attack2") && TargetButton == Button.None) {
if (InteractMode != Mode.Shoot)
InteractMode = (Mode)((int)InteractMode + 1);
else
InteractMode = Mode.Move;
}
if (InNodeOverride)
InteractMode = LastMode;
InNodeOverride = false;
HoveredEntity = null;
HoveredInteract = null;
if (Owner.Isometric) {
var target = Owner.IsoCamera.MouseTarget();
var ray = new Ray(target + Owner.IsoCamera.WorldRotation.Backward * 512f, Owner.IsoCamera.WorldRotation.Forward);
if (InteractMode != Mode.Move && Owner.Isometric) {
var tr = Scene.Trace.Ray(ray, 2048).UsePhysicsWorld(false).UseHitboxes(true).WithTag("entity").Run();
if (tr.GameObject.IsValid() && tr.GameObject.Components.TryGet<IsoEntity>(out var ent))
HoveredEntity = ent;
}
if (!HoveredEntity.IsValid()) {
var max = Scene.Trace.Ray(ray, 2048f).WithTag("block_interactable").Run().Fraction * 2048;
foreach (var interactable in Scene.GetAllComponents<IsoInteractable>()) {
if (BasePlayer.Local.IsoEntity.Components.Get<NpcMovement>().EntityRoot.Distance(interactable.WorldPosition) > 700)
continue;
if (interactable.Box.Trace(ray.ToLocal(interactable.WorldTransform), max, out var _)) {
HoveredInteract = interactable;
break;
}
}
}
}
if (HoveredInteract.IsValid()) {
LastMode = InteractMode;
InNodeOverride = true;
InteractMode = Mode.Interact;
}
if (Input.Pressed("attack1") && HoveredInteract.IsValid()) {
var m = BasePlayer.Local.IsoEntity.Components.Get<NpcMovement>();
m.Goal = HoveredInteract.IsoPathGoal.WorldPosition;
m.OnReachTargetPosition = m.Goal;
m.OnReachTarget = HoveredInteract.Interact;
m.Go();
}
}
}