Skip to content

Commit

Permalink
Avoid bug with camera
Browse files Browse the repository at this point in the history
Godot Bug, Found the issue
godotengine/godot#84389

Remove mouse capture for the moment
  • Loading branch information
theludovyc committed Jul 11, 2024
1 parent afd1f0d commit f764326
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 33 deletions.
42 changes: 21 additions & 21 deletions Assets/World/Terrain2D/Camera2D.gd
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,33 @@ var pos_limit_bot_right:Vector2

# Called when the node enters the scene tree for the first time.
func _ready():
Input.mouse_mode = Input.MOUSE_MODE_CONFINED
#Input.mouse_mode = Input.MOUSE_MODE_CONFINED
pass # Replace with function body.

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
var mouse_pos = get_viewport().get_mouse_position()

var viewport_rect = get_viewport_rect()

#var mouse_pos = get_viewport().get_mouse_position()
#
#var viewport_rect = get_viewport_rect()
#
var dir:Vector2

if mouse_pos.x < Edge_Limit:
dir.x = -1

if mouse_pos.x > viewport_rect.size.x - Edge_Limit:
dir.x = 1

if mouse_pos.y < Edge_Limit:
dir.y = -1

if mouse_pos.y > viewport_rect.size.y - Edge_Limit:
dir.y = 1

dir = dir.normalized()

if dir.length() == 0:
dir = Input.get_vector("move_left", "move_right", "move_up", "move_down")
#if mouse_pos.x < Edge_Limit:
#dir.x = -1
#
#if mouse_pos.x > viewport_rect.size.x - Edge_Limit:
#dir.x = 1
#
#if mouse_pos.y < Edge_Limit:
#dir.y = -1
#
#if mouse_pos.y > viewport_rect.size.y - Edge_Limit:
#dir.y = 1
#
#dir = dir.normalized()

#if dir.length() == 0:
dir = Input.get_vector("move_left", "move_right", "move_up", "move_down")

position += dir * SPEED * delta

Expand Down
5 changes: 3 additions & 2 deletions Assets/World/Terrain2D/GUI/GUI.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ script = ExtResource("2_t3u17")

[node name="BotMenu" type="VBoxContainer" parent="Widget"]
unique_name_in_owner = true
visible = false
clip_contents = true
layout_mode = 1
anchors_preset = 5
Expand All @@ -111,19 +112,18 @@ texture = ExtResource("6_gmmb2")

[node name="PanelContainer" type="PanelContainer" parent="Widget/BotMenu"]
unique_name_in_owner = true
visible = false
layout_mode = 2
mouse_filter = 1
theme_override_styles/panel = SubResource("StyleBoxTexture_q0br5")

[node name="TabContainer" type="TabContainer" parent="Widget/BotMenu/PanelContainer"]
layout_mode = 2
current_tab = 1
clip_tabs = false
tabs_visible = false
script = ExtResource("5_sjwej")

[node name="VBoxContainer" type="VBoxContainer" parent="Widget/BotMenu/PanelContainer/TabContainer"]
visible = false
layout_mode = 2

[node name="Title" type="HBoxContainer" parent="Widget/BotMenu/PanelContainer/TabContainer/VBoxContainer"]
Expand Down Expand Up @@ -264,6 +264,7 @@ layout_mode = 2
size_flags_vertical = 4

[node name="BuildGridContainer" type="GridContainer" parent="Widget/BotMenu/PanelContainer/TabContainer"]
visible = false
layout_mode = 2
mouse_filter = 2
columns = 4
Expand Down
18 changes: 9 additions & 9 deletions Assets/World/Terrain2D/Game2D.gd
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class_name Game2D

@onready var tm := %TileMap

#@onready var cam := $Camera2D
@onready var cam := $Camera2D

@onready var node_entities := %Entities

Expand Down Expand Up @@ -55,12 +55,12 @@ func _ready():
# set camera limits
var pos_limits = tm.get_pos_limits()

#cam.pos_limit_top_left = pos_limits[0]
#cam.pos_limit_bot_right = pos_limits[1]
#
## force camera initial pos on warehouse
#cam.position = warehouse.global_position
#cam.reset_smoothing()
cam.pos_limit_top_left = pos_limits[0]
cam.pos_limit_bot_right = pos_limits[1]

# force camera initial pos on warehouse
cam.position = warehouse.global_position
cam.reset_smoothing()

# add some initial resources
money = 100
Expand Down Expand Up @@ -90,10 +90,10 @@ func _process(delta):

rtl.text += str(mouse_pos) + "\n"

#rtl.text += str(cam.get_screen_center_position()) + "\n"
rtl.text += str(cam.get_screen_center_position()) + "\n"

# may be optimized
#mouse_pos += cam.get_screen_center_position() - get_viewport().get_visible_rect().size / 2
mouse_pos += cam.get_screen_center_position() - get_viewport().get_visible_rect().size / 2

rtl.text += str(mouse_pos) + "\n"

Expand Down
7 changes: 6 additions & 1 deletion Assets/World/Terrain2D/Game2D.tscn
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
[gd_scene load_steps=8 format=3 uid="uid://bbbuj3ne5rupv"]
[gd_scene load_steps=9 format=3 uid="uid://bbbuj3ne5rupv"]

[ext_resource type="Script" path="res://Assets/World/Terrain2D/Game2D.gd" id="1_i3wk0"]
[ext_resource type="Script" path="res://Assets/World/Terrain2D/EventBus.gd" id="2_jbfdl"]
[ext_resource type="Texture2D" uid="uid://qwmj7wmlnv8d" path="res://Assets/World/Terrain2D/deep.png" id="2_ymyts"]
[ext_resource type="Script" path="res://Assets/World/Terrain2D/TheFactory.gd" id="3_6uk0m"]
[ext_resource type="Script" path="res://Assets/World/Terrain2D/TileMap.gd" id="4_lfw1e"]
[ext_resource type="TileSet" uid="uid://bhin7dsw4tvvl" path="res://Assets/World/Terrain2D/new_tile_set.tres" id="5_6up2t"]
[ext_resource type="Script" path="res://Assets/World/Terrain2D/Camera2D.gd" id="5_7hgfn"]
[ext_resource type="PackedScene" uid="uid://b3s7eqy0blp6g" path="res://Assets/World/Terrain2D/GUI/GUI.tscn" id="6_lovin"]

[node name="Game2D" type="Node"]
Expand All @@ -21,6 +22,10 @@ script = ExtResource("3_6uk0m")
wait_time = 2.0
autostart = true

[node name="Camera2D" type="Camera2D" parent="."]
position_smoothing_enabled = true
script = ExtResource("5_7hgfn")

[node name="Deep" type="Sprite2D" parent="."]
visible = false
position = Vector2(768, 448)
Expand Down

0 comments on commit f764326

Please sign in to comment.