GodotComponentTest/scenes/HexGrid3DTest.gd

69 lines
2.1 KiB
GDScript

extends Spatial
onready var hexgrid = preload("res://addons/gdhexgrid/HexGrid.gd").new()
onready var HexTile3D = preload("res://scenes/HexTile3D.tscn")
onready var tiles = $Tiles
onready var fps_label = $CanvasLayer/HBoxContainer/FPSLabel
onready var player_pos_label = $CanvasLayer/HBoxContainer/PlayerPos
onready var tile_pos_label = $CanvasLayer/HBoxContainer/TilePos
onready var player = $Player
var player_velocity = Vector3.ZERO
var player_speed = 10
var target_coordinate = Vector3.ZERO
var target_tile = null
var current_tile = null
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()
var height = (sin(pos.y * 0.3) * sin(pos.y * 0.8) * 0.8 + cos ((pos.x) * 0.9) * 1.24) * 0.5 - 0.5
tile.transform.origin = Vector3(pos.x, height, pos.y)
tile.connect("tile_selected", self, "on_tile_selected")
tile.connect("tile_mouse_entered", self, "on_tile_hover")
tiles.add_child(tile)
var pos = hexgrid.get_hex_center3(Vector3(0, 0, 0))
print (pos)
var hex = hexgrid.get_hex_at(pos)
print (hex)
pass
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):
target_tile = tile
target_coordinate = Vector3(tile.transform.origin.x, tile.transform.origin.z, tile.transform.origin.z)
var position_error = target_coordinate - player.transform.origin
print ("clicked on tile " + str(tile.transform.origin) + " error: " + str(position_error))
func on_tile_hover(tile):
current_tile = tile
tile_pos_label.text = "Tile: " + str(current_tile.transform.origin)