87 lines
2.5 KiB
GDScript
87 lines
2.5 KiB
GDScript
class_name DeveloperPawn
|
|
extends CharacterBody2D
|
|
|
|
# Movement speed in pixels per second.
|
|
@export var movement_speed: float = 500.0
|
|
|
|
# Zoom speed.
|
|
@export var zoom_speed: float = 1.0
|
|
|
|
# Reference to the Camera2D node.
|
|
@export var camera: Camera2D
|
|
|
|
# Reference to the new MapCanvas node.
|
|
@export var map_canvas: CanvasLayer
|
|
|
|
var map_mode : bool = false
|
|
|
|
func _ready() -> void:
|
|
# Ensure all references are set.
|
|
if not camera:
|
|
print("ERROR: Camera2D reference not set on DeveloperPawn.")
|
|
set_physics_process(false)
|
|
return
|
|
if not map_canvas:
|
|
print("ERROR: MapCanvas reference not set on DeveloperPawn.")
|
|
set_physics_process(false)
|
|
return
|
|
|
|
# Disable the default physics processing on the pawn itself.
|
|
# We will handle all movement manually for direct control.
|
|
set_physics_process(false)
|
|
|
|
# Set the process mode to get input and move the pawn.
|
|
set_process(true)
|
|
|
|
# Hide the map canvas initially.
|
|
map_canvas.hide()
|
|
|
|
# Called every frame to handle input.
|
|
func _process(delta: float) -> void:
|
|
if not map_mode:
|
|
handle_input(delta)
|
|
|
|
# Handles player input for movement and zooming.
|
|
func handle_input(delta: float) -> void:
|
|
# Get movement input from WASD keys.
|
|
var input_vector = Vector2.ZERO
|
|
if Input.is_action_pressed("ui_up"):
|
|
input_vector.y -= 1
|
|
if Input.is_action_pressed("ui_down"):
|
|
input_vector.y += 1
|
|
if Input.is_action_pressed("ui_left"):
|
|
input_vector.x -= 1
|
|
if Input.is_action_pressed("ui_right"):
|
|
input_vector.x += 1
|
|
|
|
# Normalize the vector to prevent faster diagonal movement.
|
|
input_vector = input_vector.normalized()
|
|
|
|
# Update the pawn's position directly.
|
|
position += input_vector * movement_speed * delta
|
|
|
|
# Handles zoom input from the mouse wheel and map mode toggle.
|
|
func _input(event: InputEvent) -> void:
|
|
if event is InputEventMouseButton:
|
|
if camera and not map_mode:
|
|
if event.button_index == MOUSE_BUTTON_WHEEL_UP:
|
|
# Zoom in by dividing the zoom vector.
|
|
camera.zoom -= Vector2(zoom_speed, zoom_speed)
|
|
elif event.button_index == MOUSE_BUTTON_WHEEL_DOWN:
|
|
# Zoom out by multiplying the zoom vector.
|
|
camera.zoom += Vector2(zoom_speed, zoom_speed)
|
|
|
|
# Clamp the zoom to prevent it from getting too large or too small.
|
|
# The minimum zoom is now 0.01, and the maximum is a larger value.
|
|
camera.zoom.x = clamp(camera.zoom.x, 0.01, 250.0)
|
|
camera.zoom.y = clamp(camera.zoom.y, 0.01, 250.0)
|
|
|
|
if event.is_action_pressed("ui_map_mode"):
|
|
map_mode = not map_mode
|
|
if map_mode:
|
|
# If entering map mode, show the map canvas.
|
|
map_canvas.show()
|
|
else:
|
|
# If exiting map mode, hide the map canvas.
|
|
map_canvas.hide()
|