GodotComponentTest/scenes/Game.cs

290 lines
12 KiB
C#
Raw Normal View History

using System.Diagnostics;
using Godot;
2022-12-02 21:09:40 +01:00
public class Game : Spatial
2022-12-02 21:09:40 +01:00
{
private ImageTexture _blackWhitePatternTexture;
private Camera _camera;
private Vector3 _cameraOffset;
private Label _centerLabel;
private HexCell _currentTile;
2022-12-02 21:09:40 +01:00
// ui elements
2022-12-03 20:34:01 +01:00
private Label _framesPerSecondLabel;
2023-06-24 16:59:40 +02:00
private Control _gameUi;
private Button _generateWorldButton;
private Label _goldCountLabel;
private TextureRect _heightTextureRect;
2022-12-02 21:09:40 +01:00
// other members
private HexGrid _hexGrid;
private InteractionSystem _interactionSystem;
private HexCell _lastTile;
private Label _mouseTileCubeLabel;
private Label _mouseTileAxialLabel;
2022-12-02 21:09:40 +01:00
private Spatial _mouseTileHighlight;
private Label _mouseTileOffsetLabel;
private Label _mouseWorldLabel;
private Label _numCoordsAddedLabel;
private Label _numCoordsRemovedLabel;
private Label _numTilesLabel;
private Player _player;
// scene nodes
private Spatial _tileHighlight;
2022-12-04 20:51:02 +01:00
2022-12-02 21:09:40 +01:00
// Resources
private PackedScene _tileHighlightScene;
private TileInstanceManager _tileInstanceManager;
private ShaderMaterial _tileMaterial;
private Label _tileOffsetLabel;
private World _world;
private TextureRect _worldTextureRect;
2022-12-02 21:09:40 +01:00
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
2023-06-11 12:45:09 +02:00
// debugStatsContainer
var debugStatsContainer = (Container)FindNode("DebugStatsContainer");
2023-06-11 12:45:09 +02:00
_framesPerSecondLabel = debugStatsContainer.GetNode<Label>("fps_label");
_centerLabel = debugStatsContainer.GetNode<Label>("center_label");
2023-06-11 12:45:09 +02:00
_tileOffsetLabel = debugStatsContainer.GetNode<Label>("tile_offset_label");
_numTilesLabel = debugStatsContainer.GetNode<Label>("num_tiles_label");
_mouseWorldLabel = debugStatsContainer.GetNode<Label>("mouse_world_label");
2023-08-13 21:08:12 +02:00
_mouseTileOffsetLabel = debugStatsContainer.GetNode<Label>("mouse_tile_offset_label");
_mouseTileCubeLabel = debugStatsContainer.GetNode<Label>("mouse_tile_cube_label");
_mouseTileAxialLabel = debugStatsContainer.GetNode<Label>("mouse_tile_axial_label");
2023-06-11 12:45:09 +02:00
_numCoordsAddedLabel = debugStatsContainer.GetNode<Label>("num_coords_added_label");
_numCoordsRemovedLabel = debugStatsContainer.GetNode<Label>("num_coords_removed_label");
2022-12-02 21:09:40 +01:00
// UI elements
var worldGeneratorContainer = (Container)FindNode("WorldGeneratorContainer");
2023-06-11 12:45:09 +02:00
_worldTextureRect = worldGeneratorContainer.GetNode<TextureRect>("WorldTextureRect");
_heightTextureRect = worldGeneratorContainer.GetNode<TextureRect>("HeightTextureRect");
_generateWorldButton = worldGeneratorContainer.GetNode<Button>("WorldGenerateButton");
2023-06-24 16:59:40 +02:00
_gameUi = (Control)FindNode("GameUI");
_goldCountLabel = _gameUi.GetNode<Label>("GoldCount");
Debug.Assert(_goldCountLabel != null);
2022-12-02 21:09:40 +01:00
// scene nodes
_tileHighlight = GetNode<Spatial>("TileHighlight");
_mouseTileHighlight = GetNode<Spatial>("MouseTileHighlight");
2022-12-02 21:09:40 +01:00
_player = GetNode<Player>("Player");
2023-06-11 12:45:09 +02:00
_camera = (Camera)FindNode("Camera");
_cameraOffset = _camera.GlobalTranslation - _player.GlobalTranslation;
2023-02-12 21:10:28 +01:00
_world = (World)FindNode("World");
_tileInstanceManager = (TileInstanceManager)FindNode("TileInstanceManager");
// populate UI values
var generatorWorldSizeSlider = worldGeneratorContainer.GetNode<Slider>("HBoxContainer/WorldSizeSlider");
2022-12-02 21:09:40 +01:00
// resources
_tileHighlightScene = GD.Load<PackedScene>("utils/TileHighlight.tscn");
_tileMaterial = GD.Load<ShaderMaterial>("materials/HexTileTextureLookup.tres");
Debug.Assert(_tileMaterial != null);
2022-12-04 20:51:02 +01:00
_blackWhitePatternTexture = new ImageTexture();
var image = new Image();
image.Load("assets/4x4checker.png");
_blackWhitePatternTexture.CreateFromImage(image, (uint)(Texture.FlagsEnum.Mipmaps | Texture.FlagsEnum.Repeat));
2022-12-02 21:09:40 +01:00
// other members
_lastTile = new HexCell();
_currentTile = new HexCell();
_hexGrid = new HexGrid();
_interactionSystem = GetNode<InteractionSystem>("InteractionSystem");
Debug.Assert(_interactionSystem != null);
2022-12-02 21:09:40 +01:00
// connect signals
_generateWorldButton.Connect("pressed", this, nameof(OnGenerateButton));
2023-08-28 14:01:09 +02:00
_player.TaskQueueComponent.Connect("StartInteraction", _interactionSystem,
nameof(_interactionSystem.OnStartInteraction));
_player.Connect("GoldCountChanged", this, nameof(OnGoldCountChanged));
_tileInstanceManager.Connect("TileClicked", this, nameof(OnTileClicked));
_tileInstanceManager.Connect("TileHovered", this, nameof(OnTileHovered));
_world.Connect("OnWorldViewTileTypeImageChanged", this, nameof(OnWorldViewTileTypeImageChanged));
_world.Connect("OnHeightmapImageChanged", this, nameof(OnHeightmapImageChanged));
2023-02-12 21:10:28 +01:00
2023-02-12 16:44:48 +01:00
// register entity events
foreach (Node node in GetNode("Entities").GetChildren())
if (node.HasSignal("EntityClicked"))
2023-02-12 16:44:48 +01:00
node.Connect("EntityClicked", this, nameof(OnEntityClicked));
2023-02-12 21:10:28 +01:00
// perform dependency injection
//_streamContainer.SetWorld(_tileWorld);Clicked
var worldInfoComponent = _player.GetNode<WorldInfoComponent>("WorldInfo");
2023-02-12 21:10:28 +01:00
2022-12-04 21:54:44 +01:00
UpdateCurrentTile();
2022-12-02 21:09:40 +01:00
}
public override void _Input(InputEvent inputEvent)
2022-12-02 21:09:40 +01:00
{
if (inputEvent.IsAction("Forward")) GD.Print("Forward");
2022-12-02 21:09:40 +01:00
if (inputEvent.IsAction("Back")) GD.Print("Back");
}
2022-12-03 00:43:19 +01:00
2022-12-04 21:54:44 +01:00
public void UpdateCurrentTile()
2022-12-02 21:09:40 +01:00
{
// cast a ray from the camera to center
var cameraNormal = _camera.ProjectRayNormal(_camera.GetViewport().Size * 0.5f);
var cameraPosition = _camera.ProjectRayOrigin(_camera.GetViewport().Size * 0.5f);
var cameraDir = cameraNormal - cameraPosition;
Vector3 centerCoord;
if (Mathf.Abs(cameraDir.y) > Globals.EpsPosition)
{
centerCoord = cameraPosition + cameraNormal * (-cameraPosition.y / cameraNormal.y);
}
else
{
centerCoord = _camera.GlobalTranslation;
centerCoord.y = 0;
}
_world.SetCenterPlaneCoord(new Vector2(_player.GlobalTranslation.x, _player.GlobalTranslation.z));
_currentTile = _hexGrid.GetHexAt(new Vector2(centerCoord.x, centerCoord.z));
2022-12-02 21:09:40 +01:00
_centerLabel.Text = centerCoord.ToString("F3");
_tileOffsetLabel.Text = _currentTile.OffsetCoords.ToString();
2022-12-04 21:54:44 +01:00
}
2022-12-04 21:54:44 +01:00
public override void _Process(float delta)
{
_framesPerSecondLabel.Text = Engine.GetFramesPerSecond().ToString();
_lastTile = _currentTile;
2022-12-02 21:09:40 +01:00
2022-12-04 21:54:44 +01:00
UpdateCurrentTile();
var tileHighlightTransform = Transform.Identity;
var currentTileCenter = _hexGrid.GetHexCenter(_currentTile);
2022-12-02 21:09:40 +01:00
tileHighlightTransform.origin.x = currentTileCenter.x;
tileHighlightTransform.origin.z = currentTileCenter.y;
_tileHighlight.Transform = tileHighlightTransform;
2022-12-03 00:43:19 +01:00
var cameraTransform = _camera.Transform;
cameraTransform.origin = _player.GlobalTranslation + _cameraOffset;
_camera.Transform = cameraTransform;
}
public void OnGenerateButton()
{
GD.Print("Generating");
var worldSizeSlider = (Slider)FindNode("WorldSizeSlider");
if (worldSizeSlider == null) GD.PrintErr("Could not find WorldSizeSlider!");
}
public void OnAreaInputEvent(Node camera, InputEvent inputEvent, Vector3 position, Vector3 normal,
int shapeIndex)
2022-12-02 21:09:40 +01:00
{
var cellAtCursor = _hexGrid.GetHexAt(new Vector2(position.x, position.z));
var highlightTransform = Transform.Identity;
2022-12-02 21:09:40 +01:00
_mouseWorldLabel.Text = position.ToString("F3");
_mouseTileOffsetLabel.Text = cellAtCursor.OffsetCoords.ToString("N");
_mouseTileCubeLabel.Text = cellAtCursor.CubeCoords.ToString("N");
_mouseTileAxialLabel.Text = cellAtCursor.AxialCoords.ToString("N");
2022-12-02 21:09:40 +01:00
_mouseTileHighlight.Transform = highlightTransform;
2022-12-04 20:51:02 +01:00
}
2023-01-04 22:49:00 +01:00
public void OnTileClicked(HexTile3D tile)
2022-12-04 20:51:02 +01:00
{
if (_player == null) return;
2022-12-04 20:51:02 +01:00
if (_player.InteractionComponent != null) _player.InteractionComponent.EmitSignal("InteractionEnd");
2023-08-28 14:01:09 +02:00
2023-02-12 21:10:28 +01:00
_player.TaskQueueComponent.Reset();
_player.TaskQueueComponent.Queue.Enqueue(new TaskQueueComponent.NavigationTask(
new NavigationPoint(tile.GlobalTranslation)));
2023-05-01 18:37:35 +02:00
}
2023-01-04 22:49:00 +01:00
public void OnTileHovered(HexTile3D tile)
{
var highlightTransform = tile.GlobalTransform;
_mouseTileHighlight.Transform = highlightTransform;
_mouseWorldLabel.Text = highlightTransform.origin.ToString("F3");
_mouseTileOffsetLabel.Text = tile.OffsetCoords.ToString("N");
_mouseTileCubeLabel.Text = tile.Cell.CubeCoords.ToString("N");
2023-08-28 14:01:09 +02:00
_player.NavigationComponent.FindPath(_player, _player.GlobalTranslation, tile.GlobalTranslation);
}
2023-01-04 22:49:00 +01:00
public void OnEntityClicked(Entity entity)
{
GD.Print("Clicked on entity at " + entity.GlobalTranslation);
var mountPoint = (Spatial)entity.FindNode("MountPoint");
if (mountPoint != null)
{
2023-02-12 21:10:28 +01:00
_player.TaskQueueComponent.Reset();
_player.TaskQueueComponent.Queue.Enqueue(new TaskQueueComponent.NavigationTask(
new NavigationPoint(mountPoint.GlobalTransform)));
2023-02-12 21:10:28 +01:00
_player.TaskQueueComponent.Queue.Enqueue(new TaskQueueComponent.InteractionTask(entity));
}
2022-12-02 21:09:40 +01:00
}
public void ResetGameState()
{
var playerStartTransform = Transform.Identity;
playerStartTransform.origin.y = 0;
_player.Transform = playerStartTransform;
_player.TaskQueueComponent.Reset();
_player.NavigationComponent.PlanDirectPath(_player, playerStartTransform.origin, playerStartTransform.origin,
playerStartTransform.basis.Quat());
2023-06-24 16:59:40 +02:00
_goldCountLabel.Text = "0";
2023-08-28 14:01:09 +02:00
foreach (Spatial entity in GetNode("Entities").GetChildren())
{
var entityTransform = entity.Transform;
var entityPlanePos = new Vector2(entityTransform.origin.x, entityTransform.origin.z);
var entityOffsetCoordinates = _hexGrid.GetHexAt(entityPlanePos).OffsetCoords;
entityTransform.origin.y = 0;
entity.Transform = entityTransform;
}
}
private void OnHeightmapImageChanged(Image heightmapImage)
{
var newHeightmapTexture = new ImageTexture();
newHeightmapTexture.CreateFromImage(heightmapImage,
(uint)(Texture.FlagsEnum.Mipmaps | Texture.FlagsEnum.Repeat));
_heightTextureRect.Texture = newHeightmapTexture;
}
private void OnWorldViewTileTypeImageChanged(Image viewTileTypeImage)
{
var newWorldTexture = new ImageTexture();
newWorldTexture.CreateFromImage(viewTileTypeImage,
(uint)(Texture.FlagsEnum.Mipmaps | Texture.FlagsEnum.Repeat));
_worldTextureRect.Texture = newWorldTexture;
_tileMaterial.SetShaderParam("MapAlbedoTexture", newWorldTexture);
_tileMaterial.SetShaderParam("TextureSize", (int)newWorldTexture.GetSize().x);
2023-11-01 17:59:43 +01:00
_tileMaterial.SetShaderParam("CoordinateOffsetU", (int)_world.WorldTextureCoordinateOffset.x);
_tileMaterial.SetShaderParam("CoordinateOffsetV", (int)_world.WorldTextureCoordinateOffset.y);
}
public void OnGoldCountChanged(int goldCount)
{
var animationPlayer = _gameUi.GetNode<AnimationPlayer>("AnimationPlayer");
_goldCountLabel.Text = goldCount.ToString();
2023-06-24 16:59:40 +02:00
animationPlayer.CurrentAnimation = "FlashLabel";
animationPlayer.Seek(0);
animationPlayer.Play();
}
2022-12-02 21:09:40 +01:00
}