GodotComponentTest/scenes/HexGrid3DTest.gd

69 lines
2.2 KiB
GDScript
Raw Normal View History

2022-08-25 15:33:44 +02:00
extends Spatial
onready var hexgrid = preload("res://addons/gdhexgrid/HexGrid.gd").new()
onready var HexTile3D = preload("res://scenes/HexTile3D.tscn")
2022-08-25 17:06:26 +02:00
onready var tiles = $Tiles
onready var fps_label = $CanvasLayer/HBoxContainer/FPSLabel
onready var player_pos_label = $CanvasLayer/HBoxContainer/PlayerPos
2022-08-25 23:40:19 +02:00
onready var tile_pos_label = $CanvasLayer/HBoxContainer/TilePos
2022-08-25 17:06:26 +02:00
onready var player = $Player
var player_velocity = Vector3.ZERO
2022-08-31 16:43:15 +02:00
var player_speed = 5
2022-08-25 17:06:26 +02:00
var target_coordinate = Vector3.ZERO
2022-08-25 23:40:19 +02:00
var target_tile = null
var current_tile = null
2022-08-25 15:33:44 +02:00
func _ready():
print (hexgrid)
var num_tiles_x = 50
var num_tiles_z = 50
for i in range(-num_tiles_x / 2, num_tiles_x / 2):
for j in range (-num_tiles_z / 2, num_tiles_z / 2):
var pos = hexgrid.get_hex_center_from_offset(Vector2(i, j))
var tile = HexTile3D.instance()
2022-08-31 16:43:15 +02:00
var height = (sin(pos.y * 0.3) * sin(pos.y * 0.8) * 0.8 + cos ((pos.x) * 0.9) * 1.24) * 0.5 + 0.4 + rand_range(-0.3, 0.3)
2022-08-25 15:33:44 +02:00
tile.transform.origin = Vector3(pos.x, height, pos.y)
2022-08-25 17:06:26 +02:00
tile.connect("tile_selected", self, "on_tile_selected")
2022-08-25 23:40:19 +02:00
tile.connect("tile_mouse_entered", self, "on_tile_hover")
2022-08-25 17:06:26 +02:00
tiles.add_child(tile)
2022-08-25 15:33:44 +02:00
var pos = hexgrid.get_hex_center3(Vector3(0, 0, 0))
print (pos)
var hex = hexgrid.get_hex_at(pos)
print (hex)
pass
2022-08-25 17:06:26 +02:00
func _process(delta):
fps_label.text = "FPS: " + str(Performance.get_monitor(Performance.TIME_FPS))
player_pos_label.text = "Pos: " + str(player.transform.origin)
func _physics_process(delta):
var position_error = target_coordinate - player.transform.origin
var error_length = position_error.length()
if error_length > 0.1:
player_velocity = position_error / error_length * player_speed
else:
player_velocity = Vector3.ZERO
player.transform.origin += player_velocity * delta
func on_tile_selected(tile):
2022-08-25 23:40:19 +02:00
target_tile = tile
2022-08-31 16:43:15 +02:00
target_coordinate = Vector3(tile.transform.origin.x, tile.transform.origin.y, tile.transform.origin.z)
2022-08-25 17:06:26 +02:00
var position_error = target_coordinate - player.transform.origin
print ("clicked on tile " + str(tile.transform.origin) + " error: " + str(position_error))
2022-08-25 23:40:19 +02:00
func on_tile_hover(tile):
current_tile = tile
tile_pos_label.text = "Tile: " + str(current_tile.transform.origin)