Super basic player movement working.

WorldChunkRefactoring
Martin Felis 2022-12-04 20:51:02 +01:00
parent 63a11c603a
commit e6b4cecae6
8 changed files with 122 additions and 25 deletions

View File

@ -3,19 +3,22 @@ using System;
public class Player : KinematicBody
{
// Declare member variables here. Examples:
// private int a = 2;
// private string b = "text";
private MovableComponent _movable;
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
_movable = GetNode<MovableComponent>("Movable");
if (_movable != null)
{
_movable.Connect("PositionUpdated", this, nameof(OnPositionUpdated));
}
}
// // Called every frame. 'delta' is the elapsed time since the previous frame.
// public override void _Process(float delta)
// {
//
// }
private void OnPositionUpdated(Vector3 newPosition)
{
Transform transform = Transform;
transform.origin = newPosition;
Transform = transform;
}
}

View File

@ -9,17 +9,17 @@
config_version=4
_global_script_classes=[ {
"base": "Reference",
"base": "Node",
"class": "ClickableComponent",
"language": "GDScript",
"path": "res://components/ClickableComponent.gd"
}, {
"base": "Reference",
"base": "KinematicBody2D",
"class": "CollisionLine",
"language": "GDScript",
"path": "res://utils/CollisionLine.gd"
}, {
"base": "Reference",
"base": "Node",
"class": "ColorComponent",
"language": "GDScript",
"path": "res://components/ColorComponent.gd"
@ -54,7 +54,7 @@ _global_script_classes=[ {
"language": "GDScript",
"path": "res://utils/SpringDamper.gd"
}, {
"base": "Reference",
"base": "Sprite",
"class": "TintedSpriteComponent",
"language": "GDScript",
"path": "res://components/TintedSpriteComponent.gd"

View File

@ -64,7 +64,8 @@ public class AdaptiveWorldStream : Spatial
_hexGrid = new HexGrid();
// connect signals
var result = _streamContainerArea.Connect("input_event", this, nameof(OnAreaInputEvent));
_streamContainerArea.Connect("input_event", this, nameof(OnAreaInputEvent));
_streamContainer.Connect("TileSelected", this, nameof(OnTileSelected));
// CreateTileGrid();
@ -97,7 +98,8 @@ public class AdaptiveWorldStream : Spatial
public void CreateStreamActiveTiles()
{
foreach (int coord_x in Enumerable.Range((int)_streamContainer.CurrentOffsetCoordRect.Position.x, (int)_streamContainer.CurrentOffsetCoordRect.Size.x))
foreach (int coord_x in Enumerable.Range((int)_streamContainer.CurrentOffsetCoordRect.Position.x,
(int)_streamContainer.CurrentOffsetCoordRect.Size.x))
{
foreach (int coord_y in Enumerable.Range((int)_streamContainer.CurrentOffsetCoordRect.Position.y,
(int)_streamContainer.CurrentOffsetCoordRect.Size.y))
@ -130,7 +132,7 @@ public class AdaptiveWorldStream : Spatial
_tileLabel.Text = playerTransform.ToString();
_tileOffsetLabel.Text = _currentTile.OffsetCoords.ToString();
playerTransform.origin += new Vector3(-0.1f, 0, 1) * delta;
//playerTransform.origin += new Vector3(-0.1f, 0, 1) * delta;
_player.Transform = playerTransform;
Transform tileHighlightTransform = Transform.Identity;
@ -169,4 +171,20 @@ public class AdaptiveWorldStream : Spatial
_player.Transform = highlightTransform;
}
}
public void OnTileSelected(HexTile3D tile)
{
if (_player == null)
{
return;
}
MovableComponent movableComponent = _player.GetNode<MovableComponent>("Movable");
if (movableComponent == null)
{
return;
}
movableComponent.targetPosition = tile.Transform.origin;
}
}

View File

@ -70,7 +70,7 @@ func update_streaming_tiles():
tile_3d.set_tiletype(GameTile.TileType.Grass)
tile_3d.transform.origin = Vector3(hex_center.x, height, hex_center.y)
num_tiles_label.text = str(len(stream_container.tiles_by_offset_coord.values()))
num_tiles_label.text = str(len(stream_container.tiles_by_offit_coord.values()))
num_active_tiles_label.text = str(stream_active_tiles.get_child_count())

View File

@ -1,4 +1,4 @@
[gd_scene load_steps=13 format=2]
[gd_scene load_steps=14 format=2]
[ext_resource path="res://scenes/AdaptiveWorldStream.cs" type="Script" id=1]
[ext_resource path="res://entities/Player3D.tscn" type="PackedScene" id=2]
@ -7,6 +7,7 @@
[ext_resource path="res://entities/Player.cs" type="Script" id=5]
[ext_resource path="res://scenes/DebugCamera.gd" type="Script" id=6]
[ext_resource path="res://utils/TileHighlight.tscn" type="PackedScene" id=7]
[ext_resource path="res://components/MovableComponent.cs" type="Script" id=8]
[sub_resource type="CubeMesh" id=1]
size = Vector3( 1, 1, 1 )
@ -60,14 +61,17 @@ directional_shadow_mode = 0
[node name="Control" type="Control" parent="."]
margin_right = 40.0
margin_bottom = 40.0
mouse_filter = 2
[node name="HBoxContainer" type="HBoxContainer" parent="Control"]
margin_right = 40.0
margin_bottom = 40.0
mouse_filter = 2
[node name="GridContainer" type="GridContainer" parent="Control/HBoxContainer"]
margin_right = 127.0
margin_bottom = 158.0
mouse_filter = 2
columns = 2
[node name="Label9" type="Label" parent="Control/HBoxContainer/GridContainer"]
@ -225,4 +229,7 @@ script = ExtResource( 6 )
transform = Transform( 1, 0, 0, 0, -4.37114e-08, -1, 0, 1, -4.37114e-08, 0, 0.5, 0 )
mesh = SubResource( 8 )
[node name="Movable" type="Node" parent="Player"]
script = ExtResource( 8 )
[editable path="OPlayer"]

View File

@ -104,7 +104,7 @@ public class HexTile3D : Spatial
InputEventMouseButton mouseButtonEvent = (InputEventMouseButton)inputEvent;
if (mouseButtonEvent.ButtonIndex == 1 && mouseButtonEvent.Pressed)
{
EmitSignal("tile_selected", this);
EmitSignal("TileSelected", this);
}
}
}

View File

@ -20,6 +20,9 @@ public class StreamContainer : Spatial
// exports
[Export] public Vector2 Dimensions = new Vector2(8, 4);
[Signal]
delegate void TileSelected(HexTile3D tile3d);
// other members
private Rect2 _worldRect;
private Rect2 _currentOffsetCoordRect;
@ -101,6 +104,7 @@ public class StreamContainer : Spatial
{
RemovedCoords.Add(tile3D.OffsetCoords);
_activeTiles.RemoveChild(tile3D);
tile3D.Disconnect("TileSelected", this, nameof(OnTileClicked));
tile3D.QueueFree();
}
}
@ -173,6 +177,7 @@ public class StreamContainer : Spatial
HexTile3D tile3D = (HexTile3D)_hexTileScene.Instance();
tile3D.OffsetCoords = offsetCoords;
_activeTiles.AddChild(tile3D);
tile3D.Connect("TileSelected", this, nameof(OnTileClicked));
Transform tileTransform = tile3D.Transform;
tileTransform.origin.y = GD.Randf() * 0.2f;
@ -189,4 +194,10 @@ public class StreamContainer : Spatial
{
UpdateRects(_hexGrid.GetHexCenter(cell));
}
public void OnTileClicked(HexTile3D tile)
{
GD.Print("Clicked on Tile at " + tile.OffsetCoords);
EmitSignal("TileSelected", tile);
}
}

58
utils/SpringDamper.cs Normal file
View File

@ -0,0 +1,58 @@
// Based on: allenchou.net/2015/04/game-math-precise-control-over-numeric-springing/
using Godot;
using System;
using System.Diagnostics;
public class SpringDamper : Node
{
// Declare member variables here. Examples:
// private int a = 2;
// private string b = "text";
public float omega = 1;
public float zeta = 1;
public SpringDamper(float osc_freq = 1.0f, float osc_red = 0.1f, float osc_red_h = 1.0f)
{
Debug.Assert(osc_red > 0.001 && osc_red < 0.999);
omega = osc_freq * 2 * Mathf.Pi;
zeta = Mathf.Log(1.0f - osc_red) / (-omega * osc_red_h);
}
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
}
public (Vector3, Vector3) Calc(Vector3 x, Vector3 v, Vector3 xt, float h)
{
float f = 1 + 2 * h * zeta * omega;
float oo = omega * omega;
float hoo = oo * h;
float hhoo = hoo * h;
float det_inv = 1.0f / (f + hhoo);
Vector3 det_x = f * x + h * v + hhoo * xt;
Vector3 det_v = v + hoo * (xt - x);
return (det_x * det_inv, det_v * det_inv);
}
public (Vector3, Vector3) CalcClampedSpeed(Vector3 x, Vector3 v, Vector3 xt, float h, float speedMax)
{
var defaultResult = Calc(x, v, xt, h);
Vector3 x_new = defaultResult.Item1;
Vector3 vel_new = (x_new - x) / h;
float speed_new = vel_new.Length();
if (speed_new > speedMax)
{
vel_new = (vel_new / speed_new) * speedMax;
x_new = x + vel_new * h;
}
return (x_new, vel_new);
}
}