PirateTreasureHunt/editor/GridLevel.gd

109 lines
2.4 KiB
GDScript
Raw Normal View History

2021-06-13 15:03:10 +02:00
extends Node2D
enum TileType {
None,
Sand,
Grass
}
var HexGrid = preload("res://thirdparty/gdhexgrid/HexGrid.gd").new()
2021-06-13 20:40:05 +02:00
var size = [40,60]
2021-06-13 15:03:10 +02:00
var tile_size = 64
var HexPoints = null
var HexColors = null
# Tiles order:
# 0,0 0,1 0,2 ...
# 1,0 1,1 1,2 ...
# 2,0 2,1 2,2
# ...
# .. size[0],size[1]
var tile_data = []
# Called when the node enters the scene tree for the first time.
func _ready():
clear()
pass # Replace with function body.
func clear():
create_hex_points (tile_size)
HexGrid.hex_scale = Vector2(tile_size, tile_size) * 2
for r in range (size[1]):
for c in range (size[0]):
tile_data.append(TileType.None)
2021-06-13 20:40:05 +02:00
func GetTile(r, c):
2021-06-13 15:03:10 +02:00
if r < 0 or r >= size[1] or c < 0 or c >= size[0]:
print ("Invalid tile coord: ", r, ", ", c)
return
return tile_data[c * size[1] + r]
2021-06-13 20:40:05 +02:00
func SetTile(r, c, tile_type):
2021-06-13 15:03:10 +02:00
if r < 0 or r >= size[1] or c < 0 or c >= size[0]:
print ("Invalid tile coord: ", r, ", ", c)
return
tile_data[c * size[1] + r] = tile_type
update()
func WorldToTileCoords (pos):
pos = pos
return HexGrid.get_hex_at(Vector2(pos.x, -pos.y)).axial_coords
func GetHexCenter(hex):
var center = HexGrid.get_hex_center(hex)
return Vector2 (center.x, -center.y)
func create_hex_points (size):
HexPoints = PoolVector2Array()
var NoneColors = PoolColorArray()
var SandColors = PoolColorArray()
var GrassColors = PoolColorArray()
for i in range (7):
var angle = (60 * i) * PI / 180
HexPoints.append(Vector2(cos(angle), sin(angle)) * size)
NoneColors.append("#555555")
SandColors.append("#ffa106")
GrassColors.append("#4b9635")
HexColors = {
TileType.None: NoneColors,
TileType.Sand: SandColors,
TileType.Grass: GrassColors
}
func draw_grid():
var pos_start = HexGrid.get_hex_center(Vector2(0,0))
var w = 2 * tile_size
var h = sqrt(3) * tile_size
var initial_transform = get_canvas_transform()
for ci in range (size[0]):
draw_set_transform_matrix(initial_transform)
for ri in range (size[1]):
var tile_type = GetTile(ri, ci)
if tile_type == null:
tile_type = TileType.None
2021-06-13 20:40:05 +02:00
2021-06-13 15:03:10 +02:00
var color_array = HexColors[tile_type]
var tile_center = GetHexCenter(Vector2(ri, ci))
draw_set_transform(tile_center, 0, Vector2.ONE)
draw_polygon(HexPoints, color_array)
draw_polyline(HexPoints, "#888888")
func _draw():
draw_grid()
# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
# pass