godotengine / godot-proposals

Godot Improvement Proposals (GIPs)
MIT License
1.07k stars 69 forks source link

Add "RTS Camera" script template for `Camera2D` #5326

Open Linko-3D opened 1 year ago

Linko-3D commented 1 year ago

Describe the project you are working on

Godot 4 has added built-in templates like basic movements for CharacterBody2D and CharacterBody3D.

I suggest a template for the Camera2D to make RTS games. You can pan the camera, zoom, unzoom, it zooms toward the mouse's position and you can set a max zoom and unzoom. It has the least amount of line of codes. It will be useful for all RTS 2D games. Here is the script:

extends Camera2D

var zoom_percentage = 15
var max_zoom = 2.5
var max_unzoom = 0.4

var drag_cursor_shape = false

func _input(event):
    if current:
        if event is InputEventMouseMotion:
            if event.button_mask == MOUSE_BUTTON_MASK_MIDDLE:
                position -= event.relative / zoom
                drag_cursor_shape = true
            else:
                drag_cursor_shape = false

        if event is InputEventMouseButton:
            if event.is_pressed():
                if event.button_index == MOUSE_BUTTON_WHEEL_UP:
                    zoom += Vector2.ONE * zoom_percentage / 100
                    if zoom.x > max_zoom:
                        zoom = Vector2.ONE * max_zoom
                        return
                    position += get_local_mouse_position() / 10
                if event.button_index == MOUSE_BUTTON_WHEEL_DOWN:
                    zoom -= Vector2.ONE * zoom_percentage / 100
                    if zoom.x < max_unzoom:
                        zoom = Vector2.ONE * max_unzoom

func _process(delta):
    if current:
        if drag_cursor_shape:
            DisplayServer.cursor_set_shape(DisplayServer.CURSOR_DRAG)

Describe the problem or limitation you are having in your project

Needing an RTS camera 2D.

Describe the feature / enhancement and how it helps to overcome the problem or limitation

This is to make the game faster by having a camera ready for 2D RTS games.

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

When you select the Camera2D, you click on create script, in the popup menu the Template line will suggest in the menu the RTS Camera 2D.

If this enhancement will not be used often, can it be worked around with a few lines of script?

It will be very useful for RTS games that need paning and/or zooming features.

Is there a reason why this should be core and not an add-on in the asset library?

It is very minimalistic, it is a .gd with few lines of code so it can be a core feature.

Zireael07 commented 1 year ago

More fitting for godot demos repo I think?

Linko-3D commented 1 year ago

More fitting for godot demos repo I think?

It is a single .gd with few lines of code.

Zireael07 commented 1 year ago

So what? Not everyone makes a RTS and even those that do, many do not need pan or zoom.

If we went with this, people would soon clamor for other camera templates, e.g. bounded and unbounded, springy 3D, etc etc.... you can see how the list goes on and on

Linko-3D commented 1 year ago

So what? Not everyone makes a RTS and even those that do, many do not need pan or zoom.

If we went with this, people would soon clamor for other camera templates, e.g. bounded and unbounded, springy 3D, etc etc.... you can see how the list goes on and on

You can remove the feature you don't want.

A good spring camera 3d template for TPS games would be very good to have. We have many nodes that could have built-in minimalist scripts, they are hidden in the pop-up script menu like for the CharacterBody2D and 3D templates, they don't bloat the interface.

Zireael07 commented 1 year ago

CharacterBody2D and 3D templates

What templates are you talking about? (Might be they are too well hidden, if we have templates already, might be worth to have some for commonly used cameras indeed)

Linko-3D commented 1 year ago

For example, here is the platformer character template, it is buit-in inside Godot 4: https://i.imgur.com/sJPbaiq.png And the script it creates: https://i.imgur.com/9jNvE8t.png

If the checkbox is enabled Godot creates a minimalist script to have a character that can move left and right, jumps with gravity. I plan to make a CharacterBody2D RTS with my RTS camera, with the right-click it rotates on move at the cursor's position and will be compatible with the new pathfinding if enabled. We have a template for the CharacterBody3D already included, it creates a basic FPS script using the latest Godot 4 classes, it moves toward the -Z direction.

It will be very useful to speed up game development and in game jams. We don't need internet to use them, nothing to import. And it creates a very clean, easy to understand and optimized code as a good starting point.

Many nodes could have templates since like I said they are hidden. VehicleBody3D with basic controls from keyboard and joystick with acceleration, braking and steering. Camera3D for RTS like Dota, flying camera, TPS. No need to import addons every time, they are just few Ko of data in a single .gd.

Zireael07 commented 1 year ago

Gotcha! Yep, I can see how such minimal templates could be useful, and since we already have some I can definitely see both a 2D RTS camera and a 3D spring arm camera as templates, as they're probably very common uses (and can be tweaked to specific needs easily such as bounded/non-bounded behavior for 2D)

kleonc commented 1 year ago

For reference, script templates this proposal refers to are located in:

ElEitchKei commented 2 months ago

I was trying to do that camera movement for hours without looking on tutorials, got one solution but was a lot heavier that yours, thank you.