|
| 1 | +using Godot; |
| 2 | +using SpacetimeDB.Types; |
| 3 | + |
| 4 | +public partial class CircleController : EntityController |
| 5 | +{ |
| 6 | + private static readonly Color[] ColorPalette = |
| 7 | + [ |
| 8 | + //Yellow |
| 9 | + new(175 / 255.0f, 159 / 255.0f, 49 / 255.0f), |
| 10 | + new(175 / 255.0f, 116 / 255.0f, 49 / 255.0f), |
| 11 | + //Purple |
| 12 | + new(112 / 255.0f, 47 / 255.0f, 252 / 255.0f), |
| 13 | + new(51 / 255.0f, 91 / 255.0f, 252 / 255.0f), |
| 14 | + //Red |
| 15 | + new(176 / 255.0f, 54 / 255.0f, 54 / 255.0f), |
| 16 | + new(176 / 255.0f, 109 / 255.0f, 54 / 255.0f), |
| 17 | + new(141 / 255.0f, 43 / 255.0f, 99 / 255.0f), |
| 18 | + //Blue |
| 19 | + new(2 / 255.0f, 188 / 255.0f, 250 / 255.0f), |
| 20 | + new(7 / 255.0f, 50 / 255.0f, 251 / 255.0f), |
| 21 | + new(2 / 255.0f, 28 / 255.0f, 146 / 255.0f) |
| 22 | + ]; |
| 23 | + |
| 24 | + private static CanvasLayer _labelLayer; |
| 25 | + private static CanvasLayer LabelLayer |
| 26 | + { |
| 27 | + get |
| 28 | + { |
| 29 | + if (_labelLayer == null) |
| 30 | + { |
| 31 | + |
| 32 | + if (Engine.GetMainLoop() is not SceneTree sceneTree) return null; |
| 33 | + var root = sceneTree.Root; |
| 34 | + if (root == null) return null; |
| 35 | + _labelLayer = new CanvasLayer { Name = "CircleLabelLayer" }; |
| 36 | + root.AddChild(_labelLayer); |
| 37 | + } |
| 38 | + return _labelLayer; |
| 39 | + } |
| 40 | + } |
| 41 | + private static Control _labelRoot; |
| 42 | + private static Control LabelRoot |
| 43 | + { |
| 44 | + get |
| 45 | + { |
| 46 | + if (_labelRoot == null) |
| 47 | + { |
| 48 | + _labelRoot = new Control |
| 49 | + { |
| 50 | + Name = "CircleLabelRoot", |
| 51 | + MouseFilter = Control.MouseFilterEnum.Ignore |
| 52 | + }; |
| 53 | + _labelRoot.SetAnchorsPreset(Control.LayoutPreset.FullRect); |
| 54 | + |
| 55 | + LabelLayer.AddChild(_labelRoot); |
| 56 | + } |
| 57 | + return _labelRoot; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + private Label _label; |
| 62 | + private Label Label |
| 63 | + { |
| 64 | + get |
| 65 | + { |
| 66 | + if (_label == null) |
| 67 | + { |
| 68 | + _label = new Label |
| 69 | + { |
| 70 | + Name = $"{Name}_Label", |
| 71 | + TopLevel = false, |
| 72 | + MouseFilter = Control.MouseFilterEnum.Ignore |
| 73 | + }; |
| 74 | + LabelRoot.AddChild(_label); |
| 75 | + } |
| 76 | + return _label; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + private PlayerController OwnerPlayer { get; set; } |
| 81 | + |
| 82 | + public CircleController(Circle circle, PlayerController ownerPlayer) : base(circle.EntityId, ColorPalette[circle.PlayerId % ColorPalette.Length]) |
| 83 | + { |
| 84 | + OwnerPlayer = ownerPlayer; |
| 85 | + Label.Text = ownerPlayer.Username; |
| 86 | + |
| 87 | + ownerPlayer.OnCircleSpawned(this); |
| 88 | + } |
| 89 | + |
| 90 | + public override void _Process(double delta) |
| 91 | + { |
| 92 | + base._Process(delta); |
| 93 | + UpdateScreenLabelPosition(); |
| 94 | + } |
| 95 | + |
| 96 | + public override void OnDelete() |
| 97 | + { |
| 98 | + base.OnDelete(); |
| 99 | + |
| 100 | + if (IsInstanceValid(Label)) |
| 101 | + { |
| 102 | + Label.QueueFree(); |
| 103 | + } |
| 104 | + |
| 105 | + OwnerPlayer?.OnCircleDeleted(this); |
| 106 | + } |
| 107 | + |
| 108 | + private void UpdateScreenLabelPosition() |
| 109 | + { |
| 110 | + Label.Size = Label.GetCombinedMinimumSize(); |
| 111 | + var screenPosition = GetGlobalTransformWithCanvas().Origin; |
| 112 | + var offset = new Vector2(0.0f, Radius + 8.0f); |
| 113 | + Label.Position = screenPosition + offset - (Label.Size / 2.0f); |
| 114 | + } |
| 115 | +} |
0 commit comments