protoype world gen based on input image

This commit is contained in:
Antti Hakkarainen 2023-02-08 13:45:34 +02:00
parent acf1ed5423
commit 30ec55066b
11 changed files with 188 additions and 32 deletions

View file

@ -2,39 +2,40 @@
class_name CameraZoom2D
extends Camera2D
var _zoom_level : float = 1.0 : set = _set_zoom_level
var is_dragging_camera = false
var is_panning_camera = false
var tween
func _set_zoom_level(value: float) -> void:
_zoom_level = clamp(value, Globals.CAMERA_MIN_ZOOM_LEVEL, Globals.CAMERA_MAX_ZOOM_LEVEL)
func _set_camera_zoom_level(value: float) -> void:
Globals.CAMERA_ZOOM_LEVEL = clamp(value, Globals.CAMERA_MIN_ZOOM_LEVEL, Globals.CAMERA_MAX_ZOOM_LEVEL)
#interpolate frames between zoom levels to make zooming look smoother
tween = get_tree().create_tween()
tween.tween_property(
self,
"zoom",
Vector2(_zoom_level, _zoom_level),
Vector2(Globals.CAMERA_ZOOM_LEVEL, Globals.CAMERA_ZOOM_LEVEL),
Globals.CAMERA_ZOOM_DURATION
)
func camera_zoom_in() -> void:
_set_camera_zoom_level(Globals.CAMERA_ZOOM_LEVEL - Globals.CAMERA_ZOOM_FACTOR)
func _unhandled_input(event):
# camera zooming
if event.is_action_pressed("camera_zoom_in"):
_set_zoom_level(_zoom_level - Globals.CAMERA_ZOOM_FACTOR)
elif event.is_action_pressed("camera_zoom_out"):
_set_zoom_level(_zoom_level + Globals.CAMERA_ZOOM_DURATION)
func camera_zoom_out() -> void:
_set_camera_zoom_level(Globals.CAMERA_ZOOM_LEVEL + Globals.CAMERA_ZOOM_DURATION)
# camera dragging
if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT:
if !is_dragging_camera and event.pressed:
is_dragging_camera = true
if is_dragging_camera and !event.pressed:
is_dragging_camera = false
if event is InputEventMouseMotion and is_dragging_camera:
if self.position != event.position:
tween = get_tree().create_tween()
tween.tween_property(self, "property", Vector2(self.position, event.position), Globals.CAMERA_ZOOM_DURATION)
else:
self.position = event.position * Globals.CAMERA_DRAG_MULTI
func _unhandled_input(event):
if event.is_action_pressed("camera_zoom_in"):
camera_zoom_in()
if event.is_action_pressed("camera_zoom_out"):
camera_zoom_out()
if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT:
if !is_panning_camera and event.pressed:
is_panning_camera = true
if is_panning_camera and !event.pressed:
is_panning_camera = false
if event is InputEventMouseMotion and is_panning_camera:
self.position -= event.relative * Globals.CAMERA_PAN_MULTI

View file

@ -20,8 +20,11 @@ const TYPE_POWERPLANT = "powerplant"
const TYPE_ROADS = "roads"
const TYPE_DEMOLISH = "demolish"
const CAMERA_MIN_ZOOM_LEVEL: float = 0.5
# camera movement settings
var CAMERA_ZOOM_LEVEL : float = 1.0
const CAMERA_MIN_ZOOM_LEVEL: float = 0.1
const CAMERA_MAX_ZOOM_LEVEL: float = 2.0
const CAMERA_ZOOM_FACTOR: float = 0.1
const CAMERA_ZOOM_DURATION: float = 0.1
const CAMERA_DRAG_MULTI:float = 2.0
const CAMERA_PAN_MULTI:float = 2.0

View file

@ -8,12 +8,30 @@
extends Node
# Called when the node enters the scene tree for the first time.
func _ready():
var world_map: TileMap
func _init():
DisplayServer.window_set_size(
Vector2i(Globals.DEFAULT_X_RES, Globals.DEFAULT_Y_RES)
)
)
# Called when the node enters the scene tree for the first time.
func _ready():
generate_terrain()
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta):
pass
func generate_terrain():
world_map = get_node("World")
var image = Image.new()
image.load("res://maps/tampere_10x10km_1000px.png")
for x in 1000:
for y in 1000:
# layer | position coords | tilemap id | coords of the tile at tilemap | alternative tile
if image.get_pixel(x, y) == Color(1,1,1,1):
world_map.set_cell(0, Vector2i(x, y), 2, Vector2i(0,0), 0)

View file

@ -40,23 +40,32 @@ func _input(event):
func place_building_to_map():
# layer | position coords | tilemap id | coords of the tile at tilemap | alternative tile
match building_type:
Globals.TYPE_RESIDENTIAL:
tilemap = Vector2i(4,4)
tilemap = Vector2i(0,0)
set_cell(0, local_to_map(get_viewport().get_mouse_position()) , 0, tilemap, 0)
Globals.TYPE_COMMERCIAL:
tilemap = Vector2i(4,12)
set_cell(0, local_to_map(get_viewport().get_mouse_position()) , 1, tilemap, 0)
Globals.TYPE_INDUSTRIAL:
tilemap = Vector2i(4,20)
set_cell(0, local_to_map(get_viewport().get_mouse_position()) , 1, tilemap, 0)
Globals.TYPE_ROADS:
tilemap = Vector2i(14,2)
set_cell(0, local_to_map(get_viewport().get_mouse_position()) , 1, tilemap, 0)
Globals.TYPE_DEMOLISH:
tilemap = Vector2i(4,4)
set_cell(0, local_to_map(get_viewport().get_mouse_position()) , 1, tilemap, 0)
Globals.TYPE_SERVICES:
tilemap = Vector2i(4,8)
set_cell(0, local_to_map(get_viewport().get_mouse_position()) , 1, tilemap, 0)
Globals.TYPE_SOCIAL:
tilemap = Vector2i(4,0)
set_cell(0, local_to_map(get_viewport().get_mouse_position()) , 1, tilemap, 0)
_: #default
tilemap = Vector2i(16,16)
set_cell(0, local_to_map(get_viewport().get_mouse_position()) , 1, tilemap, 0)
set_cell(0, local_to_map(get_viewport().get_mouse_position()), 1, tilemap, 0)
# set_cell(0, local_to_map(get_viewport().get_mouse_position()) , 1, tilemap, 0)