implement primitive parcel drawing

This commit is contained in:
Antti Hakkarainen 2023-02-17 22:52:42 +02:00
parent 0cf64a4bc9
commit e619b9a6df
8 changed files with 112 additions and 26 deletions

View file

@ -1,4 +1,4 @@
[gd_scene load_steps=14 format=3 uid="uid://c5deq5s55q0ii"]
[gd_scene load_steps=15 format=3 uid="uid://c5deq5s55q0ii"]
[ext_resource type="Script" path="res://scripts/Main.gd" id="1_ysxum"]
[ext_resource type="Script" path="res://scripts/EventBus.gd" id="2_0j1ud"]
@ -13,6 +13,7 @@
[ext_resource type="Script" path="res://scripts/CameraMarker.gd" id="7_6krn1"]
[ext_resource type="Script" path="res://scripts/MapBackground.gd" id="8_ron2j"]
[ext_resource type="Script" path="res://scripts/UILayer.gd" id="9_ebec0"]
[ext_resource type="Script" path="res://scripts/InfoLayer.gd" id="14_hbedy"]
[node name="Main" type="Node"]
script = ExtResource("1_ysxum")
@ -45,6 +46,10 @@ script = ExtResource("8_ron2j")
[node name="MainMenu" parent="EventBus" instance=ExtResource("2_wfpe2")]
[node name="InfoLayer" type="Node2D" parent="EventBus"]
visible = false
script = ExtResource("14_hbedy")
[node name="UILayer" type="CanvasLayer" parent="EventBus"]
process_mode = 1
visible = false

View file

@ -41,6 +41,9 @@ func set_camera_position(pos: Vector2) -> void:
func set_camera_limits() -> void:
if Globals.map_size < Globals.MAP_MIN_WIDTH:
push_error("Camera: implausible map size '" + str(Globals.map_size) + "' while setting camera limits:")
# set camera bounds to map size, with chunk_in_px room to go over
self.set_limit(SIDE_LEFT, -chunk_in_px.x)
self.set_limit(SIDE_RIGHT, Globals.map_size*Globals.TILE_SIZE_X + chunk_in_px.x)

View file

@ -16,7 +16,7 @@ var map_filenames:Array = [
"res://maps/tampere_256px.png",
"res://maps/tampere_10x10km_4096px.png"
]
var map_filename:String = map_filenames[3]
var map_filename:String = map_filenames[1]
var _world_generator:WorldGenerator
@ -78,7 +78,7 @@ func _unhandled_input(event) -> void:
node_camera.camera_reset_rotation()
if event.is_action_pressed("take_screenshot"):
node_camera.take_screenshot()
node_camera.camera_take_screenshot()
if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT:
if !node_camera.get_camera_panning() and event.pressed:
@ -153,6 +153,10 @@ func start_new_game():
node_game.set_visible(true)
node_uilayer.set_visible(true)
var node_infolayer:InfoLayer
node_infolayer = find_child("InfoLayer")
node_infolayer.set_visible(true)
# set camera to center of the map
node_camera.camera_reset_rotation()
node_camera.set_camera_position(

View file

@ -5,6 +5,29 @@ extends Node
var chunks_loaded:int = 0
###################################
# VARIABLE GAME DATA, saved #
###################################
# map size is based on input image x*y pixel size
@export var map_size:int
# store terrain type (water, land, forest etc. for every map cell)
@export var map_terrain_data:Array[PackedInt32Array] = [[]]
# preprocess and store exact tile for every map cell to speed up setting tiles
@export var map_tile_data:Array[Array] = [[]]
# list of parcels the map is divided to
@export var map_parcel_data:Array[Array] = [[]]
# current camera zoom level
@export var CAMERA_ZOOM_LEVEL:float
@export var CAMERA_POSITION:Vector2i
# minimap texture, used also as save game's imagetexture
@export var minimap_texture:ImageTexture = null
###################################
# FILE PATHS #
@ -22,7 +45,7 @@ const SCRIPT_PATH:String = "res://scripts"
var minimap_colors:Dictionary = {
Globals.TILE_WATER : Color8(42, 31, 255),
Globals.TILE_TERRAIN: Color8(148, 113, 71),
Globals.TILE_FOREST: Color8(0,123,19),
Globals.TILE_FOREST : Color8(0,123,19),
"default": Color8(255,0,255),
}
@ -37,34 +60,24 @@ const CHUNK_SIZE:Vector2i = Vector2i(32,32)
# tilemap tile types
enum {TILE_WATER, TILE_TERRAIN, TILE_FOREST, TILE_BOG}
# parcel owner types
enum {PARCEL_CITY, PARCEL_STATE, PARCEL_PRIVATE}
# tilemap layers
enum {LAYER_TERRAIN, LAYER_BUILDINGS}
const TILESET_TERRAIN:TileSet = preload("res://scenes/Chunk.tres")
# map size is based on input image x*y pixel size
var map_size:int = 0
# store terrain type (water, land, forest etc. for every map cell)
var map_terrain_data:Array[PackedInt32Array] = [[]]
# preprocess and store exact tile for every map cell to speed up setting tiles
var map_tile_data:Array[Array] = [[]]
###################################
# CAMERA SETTINGS #
###################################
# minimap camera marker sprite
var camera_marker:Sprite2D
# GAME WINDOW DEFAULT SIZE
const DEFAULT_X_RES:int = 1920
const DEFAULT_Y_RES:int = 1080
# current camera zoom level
var CAMERA_ZOOM_LEVEL: float = 1.0
var CAMERA_POSITION:Vector2i
# camera movement settings
const CAMERA_MIN_ZOOM_LEVEL: float = 0.1
const CAMERA_MAX_ZOOM_LEVEL: float = 2.0

23
scripts/InfoLayer.gd Normal file
View file

@ -0,0 +1,23 @@
class_name InfoLayer
extends Node2D
# displays various info layers of the game
# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
func _draw():
for y in 16:
for x in 64:
draw_rect(Rect2(x*16*16, y*64*16, 16*16, 64*16), Color8(200,25,25,220), false, 4.0)
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta):
self.position = Vector2(0,0)

View file

@ -4,7 +4,6 @@ extends Control
signal set_camera_position(pos:Vector2)
signal set_map_background_texture(texture, scaling)
@onready var minimap_texture:ImageTexture = null
@onready var sprite:Sprite2D
@onready var is_mouse_inside_minimap:bool = false
@onready var position_multiplier:float
@ -15,7 +14,7 @@ var observe_mouse_inside_minimap:bool = false
# Called when the node enters the scene tree for the first time.
func _ready():
self.minimap_texture = ImageTexture.new()
Globals.minimap_texture = ImageTexture.new()
func _draw():
@ -69,7 +68,7 @@ func generate_minimap() -> void:
image.set_pixel(x, y, color)
minimap_texture = ImageTexture.create_from_image(image)
Globals.minimap_texture = ImageTexture.create_from_image(image)
func set_camera_marker() -> void:
@ -79,7 +78,7 @@ func set_camera_marker() -> void:
func set_minimap() -> void:
self.sprite = self.find_child("MinimapSprite")
self.sprite.texture = minimap_texture
self.sprite.texture = Globals.minimap_texture
# The size of a sprite is determined from its texture
var texture_size = sprite.texture.get_size()

View file

@ -3,8 +3,12 @@ extends CanvasLayer
@onready var node_minimap:Minimap
func _ready() -> void:
node_minimap = find_child("Minimap")
func set_ready() -> void:
node_minimap.set_ready()

View file

@ -93,11 +93,46 @@ func generate_biomes() -> void:
Globals.map_terrain_data[y][x] = Globals.TILE_FOREST
# can add other tresholds here for other biomes
# TODO move to globals later
var parcel_width = 16
var parcel_height = 64
# forests are not generated yet so can just compare water and terrain
func is_filled_with_water(coords:Vector2i) -> bool:
var terrain_tile_count:int = 0
for y in range(coords.y, coords.y + parcel_height):
for x in range(coords.x, coords.x + parcel_width):
if Globals.map_terrain_data[y][x] == Globals.TILE_TERRAIN:
terrain_tile_count += 1
# parcel is ok if it has at least one land
if terrain_tile_count > 0:
return false
return true
func generate_parcels() -> void:
# divide the land area Cadastres / Parcels
#print("generating parcels")
pass
# TODO better solution, this is something my skills were able to handle at proto stage
# should replace with a real/better algo when I am skilled enough to do it
Globals.map_parcel_data.resize(Globals.map_size / parcel_height)
for y in Globals.map_size / parcel_height:
Globals.map_parcel_data[y].resize(Globals.map_size / parcel_width)
for x in Globals.map_size / parcel_width:
# ignore parcels full fo water
if !is_filled_with_water(Vector2i(y,x)):
# 0 = top left corner, 1 = bottom right corner, 2 = owner
Globals.map_parcel_data[y][x] = [
Vector2i(y * parcel_height, x * parcel_width),
Vector2i(y * parcel_height + parcel_height, x * parcel_width + parcel_width),
Globals.PARCEL_STATE
]
#for row in Globals.map_parcel_data:
# print(row)
#for col in row:
# print(Globals.map_parcel_data[row][col])
func generate_world(filename) -> bool: