Skip to content

Commit

Permalink
[wip] handle new tilemap
Browse files Browse the repository at this point in the history
  • Loading branch information
theludovyc committed Nov 14, 2024
1 parent 2c3b221 commit 67c1e85
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 57 deletions.
32 changes: 32 additions & 0 deletions Script/World/GroundLayer.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
extends TileMapLayer

@onready var tileMap := get_parent()

func create_terrain(deep_tiles_json, shallow_tiles_json, sand_tiles_json, ground_tiles_json):
var set_cells = func(array_in, atlas_pos, array_out, type:MyTileMap.Minimap_Cell_Type):
for i in range(0, array_in.size(), 2):
var tile_vec = Vector2i(array_in[i], array_in[i + 1])

tileMap.minimap_set_cell_vec(tile_vec, type)

array_out.push_back(tile_vec)

set_cell(tile_vec, 1, atlas_pos)

var deep_tiles:PackedVector2Array
var shallow_tiles:PackedVector2Array
var sand_tiles:PackedVector2Array
var ground_tiles:PackedVector2Array

set_cells.call(deep_tiles_json, Vector2i(1, 2),
deep_tiles, MyTileMap.Minimap_Cell_Type.Deep)
set_cells.call(shallow_tiles_json, Vector2i(4, 2),
shallow_tiles, MyTileMap.Minimap_Cell_Type.Shallow)
set_cells.call(sand_tiles_json, Vector2i(7, 2),
sand_tiles, MyTileMap.Minimap_Cell_Type.Sand)
set_cells.call(ground_tiles_json, Vector2i(7, 6),
ground_tiles, MyTileMap.Minimap_Cell_Type.Ground)

set_cells_terrain_connect(ground_tiles, 0, 3)
set_cells_terrain_connect(sand_tiles, 0, 2)
set_cells_terrain_connect(shallow_tiles, 0, 1)
8 changes: 5 additions & 3 deletions theLudovyc/Game2D.gd
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func _ready():

var warehouse_center_tile = Vector2i(1, 20)

warehouse.position = tm.map_to_local(warehouse_center_tile)
warehouse.position = tm.ground_layer.map_to_local(warehouse_center_tile)

tm.build_entityStatic(warehouse, warehouse_center_tile)

Expand Down Expand Up @@ -96,9 +96,11 @@ func _process(delta):

rtl.text += str(mouse_pos) + "\n"

var tile_pos = tm.local_to_map(mouse_pos)
var tile_pos = tm.ground_layer.local_to_map(mouse_pos)

rtl.text += str(tile_pos) + "\n"

rtl.text += str(tm.minimap_get_cell(tile_pos))

rtl.text += str(tm.is_constructible(tile_pos)) + "\n"

Expand All @@ -111,7 +113,7 @@ func _process(delta):

#spawn entity
if cursor_entity:
cursor_entity.position = tm.map_to_local(tile_pos)
cursor_entity.position = tm.ground_layer.map_to_local(tile_pos)

var building_id = cursor_entity.building_id

Expand Down
24 changes: 15 additions & 9 deletions theLudovyc/Game2D.tscn
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[gd_scene load_steps=14 format=3 uid="uid://bbbuj3ne5rupv"]
[gd_scene load_steps=16 format=3 uid="uid://bbbuj3ne5rupv"]

[ext_resource type="Script" path="res://theLudovyc/Game2D.gd" id="1_i3wk0"]
[ext_resource type="Script" path="res://theLudovyc/EventBus.gd" id="2_jbfdl"]
Expand All @@ -8,10 +8,12 @@
[ext_resource type="Script" path="res://theLudovyc/TheMarket.gd" id="4_gvgis"]
[ext_resource type="Script" path="res://theLudovyc/TileMap.gd" id="4_lfw1e"]
[ext_resource type="Script" path="res://theLudovyc/TheBank.gd" id="4_s5v76"]
[ext_resource type="TileSet" uid="uid://bhin7dsw4tvvl" path="res://theLudovyc/new_tile_set.tres" id="5_6up2t"]
[ext_resource type="Script" path="res://theLudovyc/Camera2D.gd" id="5_7hgfn"]
[ext_resource type="PackedScene" uid="uid://b3s7eqy0blp6g" path="res://theLudovyc/GUI/GUI.tscn" id="6_lovin"]
[ext_resource type="Script" path="res://theLudovyc/TheTicker.gd" id="7_41bva"]
[ext_resource type="TileSet" uid="uid://bhin7dsw4tvvl" path="res://theLudovyc/new_tile_set.tres" id="11_gqnmu"]
[ext_resource type="TileSet" uid="uid://dprvpcto8mhgv" path="res://theLudovyc/tile_set_trees.tres" id="12_25naw"]
[ext_resource type="Script" path="res://Script/World/GroundLayer.gd" id="12_dmqkb"]
[ext_resource type="PackedScene" uid="uid://cdwvtbtwmrqbn" path="res://Scene/RGT/PauseMenu.tscn" id="13_xikc4"]

[node name="Game2D" type="Node"]
Expand Down Expand Up @@ -52,22 +54,26 @@ y_sort_enabled = true
[node name="TileMap" type="TileMap" parent="ZSorter"]
unique_name_in_owner = true
y_sort_enabled = true
tile_set = ExtResource("5_6up2t")
format = 2
layer_0/name = "Ground"
layer_0/y_sort_enabled = true
layer_1/name = "Trees"
layer_1/y_sort_enabled = true
layer_1/z_index = 1
script = ExtResource("4_lfw1e")

[node name="GroundLayer" type="TileMapLayer" parent="ZSorter/TileMap"]
y_sort_enabled = true
use_parent_material = true
tile_set = ExtResource("11_gqnmu")
script = ExtResource("12_dmqkb")

[node name="TreesLayer" type="TileMapLayer" parent="ZSorter/TileMap"]
z_index = 2
y_sort_enabled = true
tile_set = ExtResource("12_25naw")

[node name="Entities" type="Node2D" parent="ZSorter"]
unique_name_in_owner = true
z_index = 1
y_sort_enabled = true

[node name="CanvasLayer" type="CanvasLayer" parent="."]
visible = false

[node name="RichTextLabel" type="RichTextLabel" parent="CanvasLayer"]
anchors_preset = 2
Expand Down
50 changes: 16 additions & 34 deletions theLudovyc/TileMap.gd
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
extends TileMap
class_name MyTileMap

@onready var game:Game2D = get_tree().current_scene
@onready var ground_layer = $GroundLayer
@onready var trees_layer = $TreesLayer

var map_size:Vector2i

Expand Down Expand Up @@ -60,7 +63,7 @@ func build_entityStatic(entity:EntityStatic, tile_center:Vector2i):
var tile_coord = top_left_tile + Vector2i(x, y)

if minimap_get_cell(tile_coord) == Minimap_Cell_Type.Tree:
erase_cell(1, tile_coord)
trees_layer.erase_cell(tile_coord)

minimap_set_cell_vec(tile_coord, Minimap_Cell_Type.Building)

Expand All @@ -87,39 +90,18 @@ func create_island(map_file:String) -> int:
push_error("Error: can't parse json")
return FAILED

var set_cells = func(array_in, atlas_pos, array_out, type:Minimap_Cell_Type):
for i in range(0, array_in.size(), 2):
var tile_vec = Vector2i(array_in[i], array_in[i + 1])

minimap_set_cell_vec(tile_vec, type)

array_out.push_back(tile_vec)

set_cell(0, tile_vec, 1, atlas_pos)

var deep_tiles:PackedVector2Array
var shallow_tiles:PackedVector2Array
var sand_tiles:PackedVector2Array
var ground_tiles:PackedVector2Array

var json_map_size = json.data["size"]

map_size = Vector2i(json_map_size[0], json_map_size[1])

minimap.resize(map_size.x * map_size.y)

set_cells.call(json.data["deep_tiles"], Vector2i(1, 2),
deep_tiles, Minimap_Cell_Type.Deep)
set_cells.call(json.data["shallow_tiles"], Vector2i(4, 2),
shallow_tiles, Minimap_Cell_Type.Shallow)
set_cells.call(json.data["sand_tiles"], Vector2i(7, 2),
sand_tiles, Minimap_Cell_Type.Sand)
set_cells.call(json.data["ground_tiles"], Vector2i(7, 6),
ground_tiles, Minimap_Cell_Type.Ground)

set_cells_terrain_connect(0, ground_tiles, 0, 3)
set_cells_terrain_connect(0, sand_tiles, 0, 2)
set_cells_terrain_connect(0, shallow_tiles, 0, 1)
ground_layer.create_terrain(
json.data["deep_tiles"],
json.data["shallow_tiles"],
json.data["sand_tiles"],
json.data["ground_tiles"]
)

# spawn trees
var noise := FastNoiseLite.new()
Expand All @@ -130,20 +112,20 @@ func create_island(map_file:String) -> int:
var pos = minimap_get_pos(i)

if noise.get_noise_2dv(pos) > 0.1:
set_cell(1, pos, 2, Vector2.ZERO)
trees_layer.set_cell(pos, 1, Vector2(0, 1))

minimap[i] = Minimap_Cell_Type.Tree

return OK

func get_pos_limits() -> PackedVector2Array:
var used_rect = get_used_rect()
var used_rect = ground_layer.get_used_rect()

var pre_array:PackedVector2Array = [
map_to_local(used_rect.position),
map_to_local(used_rect.position + Vector2i(used_rect.size.x, 0)),
map_to_local(used_rect.position + Vector2i(0, used_rect.size.y)),
map_to_local(used_rect.position + Vector2i(used_rect.size.x, used_rect.size.y))
ground_layer.map_to_local(used_rect.position),
ground_layer.map_to_local(used_rect.position + Vector2i(used_rect.size.x, 0)),
ground_layer.map_to_local(used_rect.position + Vector2i(0, used_rect.size.y)),
ground_layer.map_to_local(used_rect.position + Vector2i(used_rect.size.x, used_rect.size.y))
]

var return_array:PackedVector2Array = [
Expand Down
12 changes: 1 addition & 11 deletions theLudovyc/new_tile_set.tres
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[gd_resource type="TileSet" load_steps=5 format=3 uid="uid://bhin7dsw4tvvl"]
[gd_resource type="TileSet" load_steps=3 format=3 uid="uid://bhin7dsw4tvvl"]

[ext_resource type="Texture2D" uid="uid://7c4ox3ucr5vn" path="res://theLudovyc/terrain0.png" id="1_plq3n"]
[ext_resource type="Texture2D" uid="uid://dj08oyiw86456" path="res://theLudovyc/Trees/Spruce.png" id="2_ixn8v"]

[sub_resource type="TileSetAtlasSource" id="TileSetAtlasSource_h1cbt"]
texture = ExtResource("1_plq3n")
Expand Down Expand Up @@ -528,14 +527,6 @@ texture_region_size = Vector2i(64, 32)
6:8/0/terrains_peering_bit/top_corner = 3
6:8/0/terrains_peering_bit/top_right_side = 3

[sub_resource type="TileSetAtlasSource" id="TileSetAtlasSource_wfq6b"]
texture = ExtResource("2_ixn8v")
texture_region_size = Vector2i(64, 32)
0:0/size_in_atlas = Vector2i(1, 4)
0:0/0 = 0
0:0/0/texture_origin = Vector2i(0, 45)
0:0/0/y_sort_origin = 6

[resource]
tile_shape = 1
tile_layout = 4
Expand All @@ -552,4 +543,3 @@ terrain_set_0/terrain_3/color = Color(0, 0.858824, 0.113725, 1)
custom_data_layer_0/name = "Constructible"
custom_data_layer_0/type = 1
sources/1 = SubResource("TileSetAtlasSource_h1cbt")
sources/2 = SubResource("TileSetAtlasSource_wfq6b")
15 changes: 15 additions & 0 deletions theLudovyc/tile_set_trees.tres
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[gd_resource type="TileSet" load_steps=3 format=3 uid="uid://dprvpcto8mhgv"]

[ext_resource type="Texture2D" uid="uid://c2jfvq6ggiwyi" path="res://Art/Image/Trees/Spruce1_idle_full.png" id="1_j6s44"]

[sub_resource type="TileSetAtlasSource" id="TileSetAtlasSource_obl4x"]
texture = ExtResource("1_j6s44")
0:1/size_in_atlas = Vector2i(4, 7)
0:1/0 = 0
0:1/0/texture_origin = Vector2i(0, 42)

[resource]
tile_shape = 1
tile_layout = 4
tile_size = Vector2i(64, 32)
sources/1 = SubResource("TileSetAtlasSource_obl4x")

0 comments on commit 67c1e85

Please sign in to comment.