clamp camera to map
This commit is contained in:
parent
63ea78592a
commit
0f6343b44f
5 changed files with 61 additions and 22 deletions
|
@ -1,4 +1,4 @@
|
||||||
[gd_scene load_steps=7 format=3 uid="uid://nfayf78xiuap"]
|
[gd_scene load_steps=8 format=3 uid="uid://nfayf78xiuap"]
|
||||||
|
|
||||||
[ext_resource type="Script" path="res://scripts/Main.gd" id="1_ysxum"]
|
[ext_resource type="Script" path="res://scripts/Main.gd" id="1_ysxum"]
|
||||||
[ext_resource type="Script" path="res://scripts/ChunkHandler.gd" id="2_6cequ"]
|
[ext_resource type="Script" path="res://scripts/ChunkHandler.gd" id="2_6cequ"]
|
||||||
|
@ -6,6 +6,7 @@
|
||||||
[ext_resource type="PackedScene" uid="uid://2we3txfr812u" path="res://scenes/Camera_zoom_2d.tscn" id="4_rx82t"]
|
[ext_resource type="PackedScene" uid="uid://2we3txfr812u" path="res://scenes/Camera_zoom_2d.tscn" id="4_rx82t"]
|
||||||
[ext_resource type="Script" path="res://scripts/EntityPlacer.gd" id="5_8jju5"]
|
[ext_resource type="Script" path="res://scripts/EntityPlacer.gd" id="5_8jju5"]
|
||||||
[ext_resource type="Script" path="res://scripts/Minimap.gd" id="5_rg28x"]
|
[ext_resource type="Script" path="res://scripts/Minimap.gd" id="5_rg28x"]
|
||||||
|
[ext_resource type="Texture2D" uid="uid://cxn2fno1j7vf5" path="res://icon.svg" id="7_w68w7"]
|
||||||
|
|
||||||
[node name="Main" type="Node2D"]
|
[node name="Main" type="Node2D"]
|
||||||
script = ExtResource("1_ysxum")
|
script = ExtResource("1_ysxum")
|
||||||
|
@ -116,12 +117,14 @@ script = ExtResource("5_rg28x")
|
||||||
|
|
||||||
[node name="CameraMarker" type="Sprite2D" parent="UILayer/Control/Minimap"]
|
[node name="CameraMarker" type="Sprite2D" parent="UILayer/Control/Minimap"]
|
||||||
position = Vector2(-32, 0)
|
position = Vector2(-32, 0)
|
||||||
|
texture = ExtResource("7_w68w7")
|
||||||
|
|
||||||
[node name="MinimapSprite" type="Sprite2D" parent="UILayer/Control/Minimap"]
|
[node name="MinimapSprite" type="Sprite2D" parent="UILayer/Control/Minimap"]
|
||||||
texture_repeat = 1
|
texture_repeat = 1
|
||||||
centered = false
|
centered = false
|
||||||
|
|
||||||
[connection signal="set_camera_position" from="." to="CameraZoom2D" method="_on_set_camera_position"]
|
[connection signal="set_camera_position" from="." to="CameraZoom2D" method="_on_set_camera_position"]
|
||||||
|
[connection signal="worldgen_ready" from="." to="CameraZoom2D" method="_on_main_worldgen_ready"]
|
||||||
[connection signal="worldgen_ready" from="." to="ChunkHandler" method="_on_main_worldgen_ready"]
|
[connection signal="worldgen_ready" from="." to="ChunkHandler" method="_on_main_worldgen_ready"]
|
||||||
[connection signal="worldgen_ready" from="." to="UILayer/Control/Minimap" method="_on_main_worldgen_ready"]
|
[connection signal="worldgen_ready" from="." to="UILayer/Control/Minimap" method="_on_main_worldgen_ready"]
|
||||||
[connection signal="pressed" from="UILayer/Control/ConstructionPanel/button_residental" to="UILayer/Control" method="_on_button_residental_pressed"]
|
[connection signal="pressed" from="UILayer/Control/ConstructionPanel/button_residental" to="UILayer/Control" method="_on_button_residental_pressed"]
|
||||||
|
|
|
@ -5,23 +5,32 @@ extends Camera2D
|
||||||
var is_panning_camera = false
|
var is_panning_camera = false
|
||||||
var tween
|
var tween
|
||||||
|
|
||||||
|
var camera_bounds:Array = [Vector2(0,0), Vector2(256*16, 256*16)]
|
||||||
|
var chunk_in_px:Vector2i = Vector2i(Globals.CHUNK_SIZE.x*Globals.TILE_SIZE_X, Globals.CHUNK_SIZE.y*Globals.TILE_SIZE_Y)
|
||||||
|
var game_res = DisplayServer.window_get_size(0)
|
||||||
|
var x_min_limit = self.game_res.x/2 - chunk_in_px.x
|
||||||
|
|
||||||
func camera_zoom_in() -> void:
|
|
||||||
_set_camera_zoom_level(Globals.CAMERA_ZOOM_LEVEL - Globals.CAMERA_ZOOM_FACTOR)
|
func _on_main_worldgen_ready() -> void:
|
||||||
|
# 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)
|
||||||
|
self.set_limit(SIDE_TOP, -chunk_in_px.y)
|
||||||
|
self.set_limit(SIDE_BOTTOM, Globals.map_size*Globals.TILE_SIZE_Y + chunk_in_px.y)
|
||||||
|
|
||||||
|
|
||||||
|
func _on_set_camera_position(pos: Vector2) -> void:
|
||||||
|
self.position = pos
|
||||||
|
|
||||||
|
|
||||||
func camera_zoom_out() -> void:
|
func _process(_delta) -> void:
|
||||||
_set_camera_zoom_level(Globals.CAMERA_ZOOM_LEVEL + Globals.CAMERA_ZOOM_DURATION)
|
|
||||||
|
|
||||||
|
|
||||||
func get_camera_position():
|
|
||||||
return self.position
|
|
||||||
|
|
||||||
|
|
||||||
func _process(_delta) -> void:
|
|
||||||
Globals.CAMERA_POSITION = self.position
|
Globals.CAMERA_POSITION = self.position
|
||||||
|
|
||||||
|
|
||||||
|
func _ready() -> void:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
func _set_camera_zoom_level(value: float) -> void:
|
func _set_camera_zoom_level(value: float) -> void:
|
||||||
Globals.CAMERA_ZOOM_LEVEL = clamp(value, Globals.CAMERA_MIN_ZOOM_LEVEL, Globals.CAMERA_MAX_ZOOM_LEVEL)
|
Globals.CAMERA_ZOOM_LEVEL = clamp(value, Globals.CAMERA_MIN_ZOOM_LEVEL, Globals.CAMERA_MAX_ZOOM_LEVEL)
|
||||||
|
|
||||||
|
@ -35,10 +44,6 @@ func _set_camera_zoom_level(value: float) -> void:
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
func _on_set_camera_position(pos: Vector2) -> void:
|
|
||||||
self.position = pos
|
|
||||||
|
|
||||||
|
|
||||||
func _unhandled_input(event):
|
func _unhandled_input(event):
|
||||||
if event.is_action_pressed("camera_zoom_in"):
|
if event.is_action_pressed("camera_zoom_in"):
|
||||||
camera_zoom_in()
|
camera_zoom_in()
|
||||||
|
@ -53,5 +58,30 @@ func _unhandled_input(event):
|
||||||
|
|
||||||
if event is InputEventMouseMotion and is_panning_camera:
|
if event is InputEventMouseMotion and is_panning_camera:
|
||||||
self.position -= event.relative * Globals.CAMERA_PAN_MULTI
|
self.position -= event.relative * Globals.CAMERA_PAN_MULTI
|
||||||
|
# prevent camera from going overboard
|
||||||
|
self.position.x = clamp(
|
||||||
|
self.position.x,
|
||||||
|
x_min_limit,
|
||||||
|
Globals.map_size*Globals.TILE_SIZE_X - chunk_in_px.x
|
||||||
|
);
|
||||||
|
self.position.y = clamp(
|
||||||
|
self.position.y,
|
||||||
|
0,
|
||||||
|
Globals.map_size*Globals.TILE_SIZE_Y
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
func camera_zoom_in() -> void:
|
||||||
|
_set_camera_zoom_level(Globals.CAMERA_ZOOM_LEVEL - Globals.CAMERA_ZOOM_FACTOR)
|
||||||
|
|
||||||
|
|
||||||
|
func camera_zoom_out() -> void:
|
||||||
|
_set_camera_zoom_level(Globals.CAMERA_ZOOM_LEVEL + Globals.CAMERA_ZOOM_DURATION)
|
||||||
|
|
||||||
|
|
||||||
|
func get_camera_position():
|
||||||
|
return self.position
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -104,9 +104,9 @@ const GROUND_TILE_COLOR_IN_MAP_FILE: Color = Color(0,0,0)
|
||||||
const WATER_TILE_COLOR_IN_MAP_FILE: Color = Color(1,1,1)
|
const WATER_TILE_COLOR_IN_MAP_FILE: Color = Color(1,1,1)
|
||||||
|
|
||||||
# min and max sizes for a map so the map won't be unreasonably small or large
|
# min and max sizes for a map so the map won't be unreasonably small or large
|
||||||
const MAP_MIN_HEIGHT:int = 128
|
const MAP_MIN_HEIGHT:int = 256
|
||||||
const MAP_MAX_HEIGHT:int = 4096
|
const MAP_MAX_HEIGHT:int = 4096
|
||||||
const MAP_MIN_WIDTH:int = 128
|
const MAP_MIN_WIDTH:int = 256
|
||||||
const MAP_MAX_WIDTH:int = 4096
|
const MAP_MAX_WIDTH:int = 4096
|
||||||
|
|
||||||
# tile size
|
# tile size
|
||||||
|
|
|
@ -20,7 +20,7 @@ var map_filenames:Array = [
|
||||||
"res://maps/tampere_256px.png",
|
"res://maps/tampere_256px.png",
|
||||||
"res://maps/tampere_10x10km_4096px.png"
|
"res://maps/tampere_10x10km_4096px.png"
|
||||||
]
|
]
|
||||||
var map_filename:String = map_filenames[4]
|
var map_filename:String = map_filenames[3]
|
||||||
|
|
||||||
var _world_generator:WorldGenerator
|
var _world_generator:WorldGenerator
|
||||||
|
|
||||||
|
|
|
@ -6,7 +6,7 @@ extends Control
|
||||||
|
|
||||||
# Called when the node enters the scene tree for the first time.
|
# Called when the node enters the scene tree for the first time.
|
||||||
func _ready():
|
func _ready():
|
||||||
minimap_texture = ImageTexture.new()
|
self.minimap_texture = ImageTexture.new()
|
||||||
|
|
||||||
|
|
||||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||||
|
@ -17,6 +17,7 @@ func _process(_delta):
|
||||||
func _on_main_worldgen_ready():
|
func _on_main_worldgen_ready():
|
||||||
self.generate_minimap()
|
self.generate_minimap()
|
||||||
self.set_minimap()
|
self.set_minimap()
|
||||||
|
self.setup_camera_marker()
|
||||||
|
|
||||||
|
|
||||||
func generate_minimap() -> void:
|
func generate_minimap() -> void:
|
||||||
|
@ -44,8 +45,8 @@ func generate_minimap() -> void:
|
||||||
|
|
||||||
|
|
||||||
func set_minimap() -> void:
|
func set_minimap() -> void:
|
||||||
sprite = self.get_child(1)
|
self.sprite = self.find_child("MinimapSprite")
|
||||||
sprite.texture = minimap_texture
|
self.sprite.texture = minimap_texture
|
||||||
|
|
||||||
# Assuming the area has a child CollisionShape2D with a RectangleShape resource
|
# Assuming the area has a child CollisionShape2D with a RectangleShape resource
|
||||||
var area_size = self.get_rect()
|
var area_size = self.get_rect()
|
||||||
|
@ -59,6 +60,11 @@ func set_minimap() -> void:
|
||||||
var sy = area_size.y / texture_size.y
|
var sy = area_size.y / texture_size.y
|
||||||
|
|
||||||
sprite.scale = Vector2(sx, sy)
|
sprite.scale = Vector2(sx, sy)
|
||||||
|
|
||||||
|
|
||||||
|
func setup_camera_marker() -> void:
|
||||||
|
var marker = self.find_child("CameraMarker")
|
||||||
|
marker.position = Vector2i(1000,500)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue