Super basic player movement working.
parent
63a11c603a
commit
e6b4cecae6
|
@ -3,19 +3,22 @@ using System;
|
||||||
|
|
||||||
public class Player : KinematicBody
|
public class Player : KinematicBody
|
||||||
{
|
{
|
||||||
// Declare member variables here. Examples:
|
private MovableComponent _movable;
|
||||||
// private int a = 2;
|
|
||||||
// private string b = "text";
|
|
||||||
|
|
||||||
// Called when the node enters the scene tree for the first time.
|
// Called when the node enters the scene tree for the first time.
|
||||||
public override void _Ready()
|
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.
|
private void OnPositionUpdated(Vector3 newPosition)
|
||||||
// public override void _Process(float delta)
|
{
|
||||||
// {
|
Transform transform = Transform;
|
||||||
//
|
transform.origin = newPosition;
|
||||||
// }
|
Transform = transform;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,17 +9,17 @@
|
||||||
config_version=4
|
config_version=4
|
||||||
|
|
||||||
_global_script_classes=[ {
|
_global_script_classes=[ {
|
||||||
"base": "Reference",
|
"base": "Node",
|
||||||
"class": "ClickableComponent",
|
"class": "ClickableComponent",
|
||||||
"language": "GDScript",
|
"language": "GDScript",
|
||||||
"path": "res://components/ClickableComponent.gd"
|
"path": "res://components/ClickableComponent.gd"
|
||||||
}, {
|
}, {
|
||||||
"base": "Reference",
|
"base": "KinematicBody2D",
|
||||||
"class": "CollisionLine",
|
"class": "CollisionLine",
|
||||||
"language": "GDScript",
|
"language": "GDScript",
|
||||||
"path": "res://utils/CollisionLine.gd"
|
"path": "res://utils/CollisionLine.gd"
|
||||||
}, {
|
}, {
|
||||||
"base": "Reference",
|
"base": "Node",
|
||||||
"class": "ColorComponent",
|
"class": "ColorComponent",
|
||||||
"language": "GDScript",
|
"language": "GDScript",
|
||||||
"path": "res://components/ColorComponent.gd"
|
"path": "res://components/ColorComponent.gd"
|
||||||
|
@ -54,7 +54,7 @@ _global_script_classes=[ {
|
||||||
"language": "GDScript",
|
"language": "GDScript",
|
||||||
"path": "res://utils/SpringDamper.gd"
|
"path": "res://utils/SpringDamper.gd"
|
||||||
}, {
|
}, {
|
||||||
"base": "Reference",
|
"base": "Sprite",
|
||||||
"class": "TintedSpriteComponent",
|
"class": "TintedSpriteComponent",
|
||||||
"language": "GDScript",
|
"language": "GDScript",
|
||||||
"path": "res://components/TintedSpriteComponent.gd"
|
"path": "res://components/TintedSpriteComponent.gd"
|
||||||
|
|
|
@ -23,10 +23,10 @@ public class AdaptiveWorldStream : Spatial
|
||||||
private Area _streamContainerArea;
|
private Area _streamContainerArea;
|
||||||
private Spatial _streamContainerActiveTiles;
|
private Spatial _streamContainerActiveTiles;
|
||||||
private Player _player;
|
private Player _player;
|
||||||
|
|
||||||
// Resources
|
// Resources
|
||||||
private PackedScene _tileHighlightScene;
|
private PackedScene _tileHighlightScene;
|
||||||
|
|
||||||
// other members
|
// other members
|
||||||
private HexGrid _hexGrid;
|
private HexGrid _hexGrid;
|
||||||
private HexCell _lastTile;
|
private HexCell _lastTile;
|
||||||
|
@ -56,7 +56,7 @@ public class AdaptiveWorldStream : Spatial
|
||||||
|
|
||||||
// resources
|
// resources
|
||||||
_tileHighlightScene = GD.Load<PackedScene>("utils/TileHighlight.tscn");
|
_tileHighlightScene = GD.Load<PackedScene>("utils/TileHighlight.tscn");
|
||||||
|
|
||||||
// other members
|
// other members
|
||||||
_lastTile = new HexCell();
|
_lastTile = new HexCell();
|
||||||
_currentTile = new HexCell();
|
_currentTile = new HexCell();
|
||||||
|
@ -64,10 +64,11 @@ public class AdaptiveWorldStream : Spatial
|
||||||
_hexGrid = new HexGrid();
|
_hexGrid = new HexGrid();
|
||||||
|
|
||||||
// connect signals
|
// 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();
|
// CreateTileGrid();
|
||||||
|
|
||||||
//playerTransform.origin += new Vector3(0, 0, -1) * delta;
|
//playerTransform.origin += new Vector3(0, 0, -1) * delta;
|
||||||
Transform playerTransform = _player.Transform;
|
Transform playerTransform = _player.Transform;
|
||||||
playerTransform.origin.x = 3;
|
playerTransform.origin.x = 3;
|
||||||
|
@ -97,7 +98,8 @@ public class AdaptiveWorldStream : Spatial
|
||||||
|
|
||||||
public void CreateStreamActiveTiles()
|
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,
|
foreach (int coord_y in Enumerable.Range((int)_streamContainer.CurrentOffsetCoordRect.Position.y,
|
||||||
(int)_streamContainer.CurrentOffsetCoordRect.Size.y))
|
(int)_streamContainer.CurrentOffsetCoordRect.Size.y))
|
||||||
|
@ -117,7 +119,7 @@ public class AdaptiveWorldStream : Spatial
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void _Process(float delta)
|
public override void _Process(float delta)
|
||||||
{
|
{
|
||||||
_framesPerSecondLabel.Text = Engine.GetFramesPerSecond().ToString();
|
_framesPerSecondLabel.Text = Engine.GetFramesPerSecond().ToString();
|
||||||
|
@ -130,7 +132,7 @@ public class AdaptiveWorldStream : Spatial
|
||||||
_tileLabel.Text = playerTransform.ToString();
|
_tileLabel.Text = playerTransform.ToString();
|
||||||
_tileOffsetLabel.Text = _currentTile.OffsetCoords.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;
|
_player.Transform = playerTransform;
|
||||||
|
|
||||||
Transform tileHighlightTransform = Transform.Identity;
|
Transform tileHighlightTransform = Transform.Identity;
|
||||||
|
@ -166,7 +168,23 @@ public class AdaptiveWorldStream : Spatial
|
||||||
|
|
||||||
if (inputEvent is InputEventMouseButton)
|
if (inputEvent is InputEventMouseButton)
|
||||||
{
|
{
|
||||||
_player.Transform = highlightTransform;
|
_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;
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -70,7 +70,7 @@ func update_streaming_tiles():
|
||||||
tile_3d.set_tiletype(GameTile.TileType.Grass)
|
tile_3d.set_tiletype(GameTile.TileType.Grass)
|
||||||
tile_3d.transform.origin = Vector3(hex_center.x, height, hex_center.y)
|
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())
|
num_active_tiles_label.text = str(stream_active_tiles.get_child_count())
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -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://scenes/AdaptiveWorldStream.cs" type="Script" id=1]
|
||||||
[ext_resource path="res://entities/Player3D.tscn" type="PackedScene" id=2]
|
[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://entities/Player.cs" type="Script" id=5]
|
||||||
[ext_resource path="res://scenes/DebugCamera.gd" type="Script" id=6]
|
[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://utils/TileHighlight.tscn" type="PackedScene" id=7]
|
||||||
|
[ext_resource path="res://components/MovableComponent.cs" type="Script" id=8]
|
||||||
|
|
||||||
[sub_resource type="CubeMesh" id=1]
|
[sub_resource type="CubeMesh" id=1]
|
||||||
size = Vector3( 1, 1, 1 )
|
size = Vector3( 1, 1, 1 )
|
||||||
|
@ -60,14 +61,17 @@ directional_shadow_mode = 0
|
||||||
[node name="Control" type="Control" parent="."]
|
[node name="Control" type="Control" parent="."]
|
||||||
margin_right = 40.0
|
margin_right = 40.0
|
||||||
margin_bottom = 40.0
|
margin_bottom = 40.0
|
||||||
|
mouse_filter = 2
|
||||||
|
|
||||||
[node name="HBoxContainer" type="HBoxContainer" parent="Control"]
|
[node name="HBoxContainer" type="HBoxContainer" parent="Control"]
|
||||||
margin_right = 40.0
|
margin_right = 40.0
|
||||||
margin_bottom = 40.0
|
margin_bottom = 40.0
|
||||||
|
mouse_filter = 2
|
||||||
|
|
||||||
[node name="GridContainer" type="GridContainer" parent="Control/HBoxContainer"]
|
[node name="GridContainer" type="GridContainer" parent="Control/HBoxContainer"]
|
||||||
margin_right = 127.0
|
margin_right = 127.0
|
||||||
margin_bottom = 158.0
|
margin_bottom = 158.0
|
||||||
|
mouse_filter = 2
|
||||||
columns = 2
|
columns = 2
|
||||||
|
|
||||||
[node name="Label9" type="Label" parent="Control/HBoxContainer/GridContainer"]
|
[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 )
|
transform = Transform( 1, 0, 0, 0, -4.37114e-08, -1, 0, 1, -4.37114e-08, 0, 0.5, 0 )
|
||||||
mesh = SubResource( 8 )
|
mesh = SubResource( 8 )
|
||||||
|
|
||||||
|
[node name="Movable" type="Node" parent="Player"]
|
||||||
|
script = ExtResource( 8 )
|
||||||
|
|
||||||
[editable path="OPlayer"]
|
[editable path="OPlayer"]
|
||||||
|
|
|
@ -104,7 +104,7 @@ public class HexTile3D : Spatial
|
||||||
InputEventMouseButton mouseButtonEvent = (InputEventMouseButton)inputEvent;
|
InputEventMouseButton mouseButtonEvent = (InputEventMouseButton)inputEvent;
|
||||||
if (mouseButtonEvent.ButtonIndex == 1 && mouseButtonEvent.Pressed)
|
if (mouseButtonEvent.ButtonIndex == 1 && mouseButtonEvent.Pressed)
|
||||||
{
|
{
|
||||||
EmitSignal("tile_selected", this);
|
EmitSignal("TileSelected", this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,6 +20,9 @@ public class StreamContainer : Spatial
|
||||||
// exports
|
// exports
|
||||||
[Export] public Vector2 Dimensions = new Vector2(8, 4);
|
[Export] public Vector2 Dimensions = new Vector2(8, 4);
|
||||||
|
|
||||||
|
[Signal]
|
||||||
|
delegate void TileSelected(HexTile3D tile3d);
|
||||||
|
|
||||||
// other members
|
// other members
|
||||||
private Rect2 _worldRect;
|
private Rect2 _worldRect;
|
||||||
private Rect2 _currentOffsetCoordRect;
|
private Rect2 _currentOffsetCoordRect;
|
||||||
|
@ -101,6 +104,7 @@ public class StreamContainer : Spatial
|
||||||
{
|
{
|
||||||
RemovedCoords.Add(tile3D.OffsetCoords);
|
RemovedCoords.Add(tile3D.OffsetCoords);
|
||||||
_activeTiles.RemoveChild(tile3D);
|
_activeTiles.RemoveChild(tile3D);
|
||||||
|
tile3D.Disconnect("TileSelected", this, nameof(OnTileClicked));
|
||||||
tile3D.QueueFree();
|
tile3D.QueueFree();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -173,6 +177,7 @@ public class StreamContainer : Spatial
|
||||||
HexTile3D tile3D = (HexTile3D)_hexTileScene.Instance();
|
HexTile3D tile3D = (HexTile3D)_hexTileScene.Instance();
|
||||||
tile3D.OffsetCoords = offsetCoords;
|
tile3D.OffsetCoords = offsetCoords;
|
||||||
_activeTiles.AddChild(tile3D);
|
_activeTiles.AddChild(tile3D);
|
||||||
|
tile3D.Connect("TileSelected", this, nameof(OnTileClicked));
|
||||||
|
|
||||||
Transform tileTransform = tile3D.Transform;
|
Transform tileTransform = tile3D.Transform;
|
||||||
tileTransform.origin.y = GD.Randf() * 0.2f;
|
tileTransform.origin.y = GD.Randf() * 0.2f;
|
||||||
|
@ -189,4 +194,10 @@ public class StreamContainer : Spatial
|
||||||
{
|
{
|
||||||
UpdateRects(_hexGrid.GetHexCenter(cell));
|
UpdateRects(_hexGrid.GetHexCenter(cell));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void OnTileClicked(HexTile3D tile)
|
||||||
|
{
|
||||||
|
GD.Print("Clicked on Tile at " + tile.OffsetCoords);
|
||||||
|
EmitSignal("TileSelected", tile);
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue