camera zoom in/out & camera panning

This commit is contained in:
Antti Hakkarainen 2023-02-07 19:21:51 +02:00
parent 24a05c557d
commit acf1ed5423
17 changed files with 774 additions and 80 deletions

40
scripts/CameraZoom2D.gd Normal file
View file

@ -0,0 +1,40 @@
# Class handles the camera zoom and movement in the game
class_name CameraZoom2D
extends Camera2D
var _zoom_level : float = 1.0 : set = _set_zoom_level
var is_dragging_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)
#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),
Globals.CAMERA_ZOOM_DURATION
)
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)
# 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

View file

@ -3,8 +3,7 @@ extends Control
# var view = get_node("../View")
signal button_pressed(button_name)
var BUTTON_SIZE = Vector2i(50,50)
@onready var debug_info = get_node("DebugInfo")
# name, position
var buttons = {
@ -19,18 +18,12 @@ var buttons = {
# Called when the node enters the scene tree for the first time.
func _ready():
adjust_construction_panel()
$ConstructionPanel.set_size(Vector2(500, 120))
create_buttons()
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta):
pass
debug_info.set_text(str(get_viewport().get_mouse_position()))
func adjust_construction_panel():
$ConstructionPanel.set_size(Vector2i(500, 120))
$ConstructionPanel.set_position(Vector2i(0, -200))
# defines construction toolbar buttons
func create_buttons():
for button in buttons:
@ -40,7 +33,7 @@ func create_buttons():
if(!node_path):
push_error("Error: Button '" + button + "' not found when trying to set it's properties in Control.gd!")
node_path.set_size(BUTTON_SIZE)
node_path.set_size(Globals.GUI_BUILD_BUTTON_SIZE)
node_path.set_position(values[0])
node_path.set_anchor(SIDE_TOP, anchor_top)
node_path.set_text(values[1])

View file

@ -4,6 +4,10 @@ const SCENE_PATH:String = "res://scenes/"
const ART_PATH:String = "res://art/"
const SCRIPT_PATH:String = "res://scripts"
const GUI_BUILD_BUTTON_SIZE_X: int = 50
const GUI_BUILD_BUTTON_SIZE_Y: int = 50
const GUI_BUILD_BUTTON_SIZE: Vector2i = Vector2i(GUI_BUILD_BUTTON_SIZE_X,GUI_BUILD_BUTTON_SIZE_Y)
const DEFAULT_X_RES:int = 1920
const DEFAULT_Y_RES:int = 1080
@ -15,3 +19,9 @@ const TYPE_SOCIAL = "social"
const TYPE_POWERPLANT = "powerplant"
const TYPE_ROADS = "roads"
const TYPE_DEMOLISH = "demolish"
const CAMERA_MIN_ZOOM_LEVEL: float = 0.5
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

View file

@ -1,6 +1,6 @@
# - "Cube Clicker 2000" where you click a spot and a cube appears there.
# - Then add different shapes or colors of cubes.
# - Then clamp their positions to be on a grid.
# OK - "Cube Clicker 2000" where you click a spot and a cube appears there.
# OK - Then add different shapes or colors of cubes.
# OK - Then clamp their positions to be on a grid.
# - Then make it so that when you add yellow cubes, yellow desire meter goes down and green meter goes up.
# - Then click a bunch of grey cubes in a row and have them automatically flatten out into flat grey cubes (roads).
# - Then click and drag to draw the lines of grey cubes.

8
scripts/Snippets.gd Normal file
View file

@ -0,0 +1,8 @@
extends Node
# Called when the node enters the scene tree for the first time.
func placeholder():
# print all children of the node
for _i in self.get_children():
print(_i

View file

@ -1,34 +0,0 @@
extends Node2D
var gui
var building
# Called when the node enters the scene tree for the first time.
func _ready():
pass
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta):
pass
func _on_control_button_pressed(button_name):
# create new building, in Building node it is attached to mouse cursor
var scene = load(Globals.SCENE_PATH + "Building.tscn")
building = scene.instantiate()
add_child(building)
#for _i in self.get_children():
# print(_i)
# stop processing the sprite (fix it in place). no idea if it is a good idea yet..
# print(button_name + " button pressed in Control! :-0")
func _input(event):
# places the building on cursor. a bad way!
if Input.is_anything_pressed() and building != null:
building.set_process(false)
building = null
# print(event.as_text())

62
scripts/World.gd Normal file
View file

@ -0,0 +1,62 @@
extends TileMap
var building : bool = false
var building_type: String
var tilemap: Vector2i
#var scene
# Called when the node enters the scene tree for the first time.
func _ready():
pass
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta):
pass
func _on_control_button_pressed(type):
# create new building, in Building node it is attached to mouse cursor
#scene = load(Globals.SCENE_PATH + "Building.tscn")
#building = scene.instantiate()
#add_child(building)
self.building = true
self.building_type = type
#print(button_name + " button pressed in Control! :-0")
pass
func _input(event):
# place the building
if event.is_action_pressed("place_building") and building != false:
# stop processing the sprite (fix it in place). no idea if it is a good idea yet..
#building.set_process(false)
building = false
place_building_to_map()
# cancel placement
if event.is_action_pressed("cancel"):
if building != false:
pass
func place_building_to_map():
match building_type:
Globals.TYPE_RESIDENTIAL:
tilemap = Vector2i(4,4)
Globals.TYPE_COMMERCIAL:
tilemap = Vector2i(4,12)
Globals.TYPE_INDUSTRIAL:
tilemap = Vector2i(4,20)
Globals.TYPE_ROADS:
tilemap = Vector2i(14,2)
Globals.TYPE_DEMOLISH:
tilemap = Vector2i(4,4)
Globals.TYPE_SERVICES:
tilemap = Vector2i(4,8)
Globals.TYPE_SOCIAL:
tilemap = Vector2i(4,0)
_: #default
tilemap = Vector2i(16,16)
set_cell(0, local_to_map(get_viewport().get_mouse_position()), 1, tilemap, 0)