editor: can switch tile type

master
Martin Felis 2021-06-13 20:40:05 +02:00
parent b535b3fe60
commit 742924fafc
5 changed files with 147 additions and 80 deletions

View File

@ -87,7 +87,7 @@ script = ExtResource( 1 )
[node name="blur" type="Sprite" parent="."]
material = SubResource( 2 )
scale = Vector2( 0.983894, 1 )
scale = Vector2( 0.976278, 1 )
centered = false
[node name="pixelate" type="Sprite" parent="."]

22
editor/Camera2D.gd Normal file
View File

@ -0,0 +1,22 @@
extends Camera2D
# Declare member variables here. Examples:
# var a = 2
# var b = "text"
# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
func ScreenToWorld (pos_screen: Vector2):
var camera_offset = OS.get_window_safe_area().size * 0.5 * zoom - transform.origin
return (pos_screen * zoom - camera_offset)
func WorldToScreen (pos_world: Vector2):
pass
# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
# pass

View File

@ -8,7 +8,7 @@ enum TileType {
var HexGrid = preload("res://thirdparty/gdhexgrid/HexGrid.gd").new()
var size = [2,6]
var size = [40,60]
var tile_size = 64
var HexPoints = null
@ -34,14 +34,14 @@ func clear():
for c in range (size[0]):
tile_data.append(TileType.None)
func GetTile(c, r):
func GetTile(r, c):
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]
func SetTile(c, r, tile_type):
func SetTile(r, c, tile_type):
if r < 0 or r >= size[1] or c < 0 or c >= size[0]:
print ("Invalid tile coord: ", r, ", ", c)
return
@ -91,8 +91,7 @@ func draw_grid():
var tile_type = GetTile(ri, ci)
if tile_type == null:
tile_type = TileType.None
else:
print ("moep")
var color_array = HexColors[tile_type]
var tile_center = GetHexCenter(Vector2(ri, ci))

View File

@ -9,25 +9,47 @@ var GridLevel
var highlight_hex_coord = Vector2(0,0)
onready var screen_coords = get_node("HBoxContainer/Coords")
onready var hex_coords = get_node("HBoxContainer/GridCoords")
onready var camera = get_node("Camera2D")
onready var ui = get_node("Camera2D/UI")
onready var ScreenCoords = get_node("Camera2D/UI/ScreenCoords")
onready var HexCoords = get_node("Camera2D/UI/HexCoords")
onready var WorldCoords = get_node("Camera2D/UI/WorldCoords")
onready var TileType = get_node("Camera2D/UI/TileType")
# Called when the node enters the scene tree for the first time.
func _ready():
GridLevel = get_node("GridLevel")
TileType.add_item("None", 0)
TileType.add_item("Sand", 1)
TileType.add_item("Grass", 2)
update()
func _process(_delta):
var view_size = OS.get_window_safe_area().size
ui.rect_position = -view_size * 0.5
update()
func _input(event):
func _unhandled_input(event):
if 'position' in event:
screen_coords.text = str(event.position)
var coords = GridLevel.WorldToTileCoords (event.position)
hex_coords.text = str(coords)
highlight_hex_coord = GridLevel.GetHexCenter(coords)
ScreenCoords.text = str(event.position)
var world_coords = camera.ScreenToWorld(event.position)
WorldCoords.text = str(world_coords)
var hex_coords = GridLevel.WorldToTileCoords(world_coords)
HexCoords.text = str(hex_coords)
highlight_hex_coord = GridLevel.GetHexCenter(hex_coords)
if event is InputEventMouseButton:
if event.button_index == BUTTON_LEFT and event.pressed:
GridLevel.SetTile (coords.x, coords.y, GridLevel.TileType.Sand)
GridLevel.SetTile (hex_coords.x, hex_coords.y, TileType.selected)
if event.button_index == BUTTON_WHEEL_DOWN and event.pressed:
camera.zoom = camera.zoom * 1.0 / 0.8
if event.button_index == BUTTON_WHEEL_UP and event.pressed:
camera.zoom = camera.zoom * 0.8
if event is InputEventMouseButton and Input.is_mouse_button_pressed(BUTTON_MIDDLE):
camera.transform.origin = world_coords
update()

View File

@ -1,89 +1,34 @@
[gd_scene load_steps=4 format=2]
[gd_scene load_steps=5 format=2]
[ext_resource path="res://editor/GridLevel.gd" type="Script" id=1]
[ext_resource path="res://editor/IslandEditor.gd" type="Script" id=2]
[ext_resource path="res://editor/Area2D.gd" type="Script" id=3]
[ext_resource path="res://editor/Camera2D.gd" type="Script" id=4]
[node name="Editor" type="Node2D"]
script = ExtResource( 2 )
[node name="HBoxContainer" type="HBoxContainer" parent="."]
margin_right = 40.0
margin_bottom = 40.0
__meta__ = {
"_edit_use_anchors_": false
}
[node name="New" type="Button" parent="HBoxContainer"]
margin_right = 44.0
margin_bottom = 40.0
text = "Clear"
[node name="SpinBox" type="SpinBox" parent="HBoxContainer"]
margin_left = 48.0
margin_right = 122.0
margin_bottom = 40.0
min_value = 1.0
max_value = 32.0
value = 1.0
[node name="OptionButton" type="OptionButton" parent="HBoxContainer"]
margin_left = 126.0
margin_right = 163.0
margin_bottom = 40.0
text = "0"
__meta__ = {
"_edit_use_anchors_": false
}
[node name="CoordsLabel" type="Label" parent="HBoxContainer"]
margin_left = 167.0
margin_top = 13.0
margin_right = 210.0
margin_bottom = 27.0
text = "Screen"
[node name="Coords" type="Label" parent="HBoxContainer"]
margin_left = 214.0
margin_top = 13.0
margin_right = 234.0
margin_bottom = 27.0
text = "0,0"
[node name="GridCoordsLabel" type="Label" parent="HBoxContainer"]
margin_left = 238.0
margin_top = 13.0
margin_right = 263.0
margin_bottom = 27.0
text = "Hex"
__meta__ = {
"_edit_use_anchors_": false
}
[node name="GridCoords" type="Label" parent="HBoxContainer"]
margin_left = 267.0
margin_top = 13.0
margin_right = 287.0
margin_bottom = 27.0
text = "0,0"
[node name="GridLevel" type="Node2D" parent="."]
z_index = -2
script = ExtResource( 1 )
[node name="Area2D" type="Node2D" parent="."]
[node name="Camera2D" type="Camera2D" parent="."]
current = true
script = ExtResource( 4 )
[node name="Area2D" type="Node2D" parent="Camera2D"]
script = ExtResource( 3 )
[node name="Highlight" type="Node2D" parent="Area2D"]
[node name="Highlight" type="Node2D" parent="Camera2D/Area2D"]
[node name="Highlight" type="Label" parent="Area2D/Highlight"]
[node name="Highlight" type="Label" parent="Camera2D/Area2D/Highlight"]
margin_top = 13.0
margin_bottom = 27.0
__meta__ = {
"_edit_use_anchors_": false
}
[node name="AreaCoords" type="Label" parent="Area2D/Highlight"]
[node name="AreaCoords" type="Label" parent="Camera2D/Area2D/Highlight"]
margin_left = 10.8379
margin_top = 34.1084
margin_right = 29.8379
@ -92,7 +37,7 @@ __meta__ = {
"_edit_use_anchors_": false
}
[node name="HexCoords" type="Label" parent="Area2D/Highlight"]
[node name="HexCoords" type="Label" parent="Camera2D/Area2D/Highlight"]
margin_left = 8.0
margin_top = 13.0
margin_right = 27.0
@ -101,3 +46,82 @@ rect_pivot_offset = Vector2( -534.527, -13 )
__meta__ = {
"_edit_use_anchors_": false
}
[node name="UI" type="HBoxContainer" parent="Camera2D"]
margin_right = 287.0
margin_bottom = 40.0
__meta__ = {
"_edit_use_anchors_": false
}
[node name="New" type="Button" parent="Camera2D/UI"]
margin_right = 44.0
margin_bottom = 40.0
text = "Clear"
[node name="SpinBox" type="SpinBox" parent="Camera2D/UI"]
margin_left = 48.0
margin_right = 122.0
margin_bottom = 40.0
min_value = 1.0
max_value = 32.0
value = 1.0
[node name="TileType" type="OptionButton" parent="Camera2D/UI"]
margin_left = 126.0
margin_right = 155.0
margin_bottom = 40.0
__meta__ = {
"_edit_use_anchors_": false
}
[node name="ScreenCoordsLabel" type="Label" parent="Camera2D/UI"]
margin_left = 159.0
margin_top = 13.0
margin_right = 202.0
margin_bottom = 27.0
text = "Screen"
[node name="ScreenCoords" type="Label" parent="Camera2D/UI"]
margin_left = 206.0
margin_top = 13.0
margin_right = 226.0
margin_bottom = 27.0
text = "0,0"
[node name="HexCoordsLabel" type="Label" parent="Camera2D/UI"]
margin_left = 230.0
margin_top = 13.0
margin_right = 255.0
margin_bottom = 27.0
text = "Hex"
__meta__ = {
"_edit_use_anchors_": false
}
[node name="HexCoords" type="Label" parent="Camera2D/UI"]
margin_left = 259.0
margin_top = 13.0
margin_right = 279.0
margin_bottom = 27.0
text = "0,0"
[node name="WorldCoordsLabel" type="Label" parent="Camera2D/UI"]
margin_left = 283.0
margin_top = 13.0
margin_right = 321.0
margin_bottom = 27.0
text = "World"
__meta__ = {
"_edit_use_anchors_": false
}
[node name="WorldCoords" type="Label" parent="Camera2D/UI"]
margin_left = 325.0
margin_top = 13.0
margin_right = 345.0
margin_bottom = 27.0
text = "0,0"
__meta__ = {
"_edit_use_anchors_": false
}