diff --git a/Camera.cs b/Camera.cs index c036915..65f4cea 100644 --- a/Camera.cs +++ b/Camera.cs @@ -2,23 +2,20 @@ using Godot; public class Camera : Camera2D { - public int MaxDistance { get; } = 80; public Cursor Cursor { get; private set; } - private Vector2 _rawPosition = Vector2.Zero; - - public override void _Ready() + public override void _EnterTree() { Cursor = GetViewport().GetNode("Cursor"); } public override void _Process(float delta) { - var mousePos = GetTree().Root.GetMousePosition(); - var centerPos = OS.WindowSize / 2; - var target = !Cursor.Visible ? Vector2.Zero - : ((mousePos - centerPos) / 4).Clamped(MaxDistance) * 2; - _rawPosition = _rawPosition.LinearInterpolate(target, 0.05F).Round(); - Position = _rawPosition.Round(); + // TODO: Implement some kind of "zoom" mechanic? + // var mousePos = GetTree().Root.GetMousePosition(); + // var centerPos = OS.WindowSize / 2; + // var scale = ((Viewport)GetViewport()).Scale; + // Position = !Cursor.Visible ? Vector2.Zero + // : ((mousePos - centerPos) / scale).Clamped(MaxDistance) / 2; } } diff --git a/Cursor.cs b/Cursor.cs index 9f66c62..701490c 100644 --- a/Cursor.cs +++ b/Cursor.cs @@ -4,7 +4,6 @@ public class Cursor : Node2D { public override void _Ready() { - Visible = false; Input.SetMouseMode(Input.MouseMode.Hidden); } @@ -18,8 +17,6 @@ public class Cursor : Node2D public override void _Process(float delta) { - var viewport = (Viewport)GetViewport(); - var origin = viewport.CanvasTransform.origin; - Position = viewport.GetMousePosition() / viewport.Scale - origin; + Position = GetGlobalMousePosition(); } } diff --git a/Game.cs b/Game.cs new file mode 100644 index 0000000..cf2edf7 --- /dev/null +++ b/Game.cs @@ -0,0 +1,64 @@ +using Godot; +using System; + +public class Game : Node +{ + public Vector2 PreferredScreenSize { get; } = new Vector2(640, 360); + + [Export] public PackedScene Player { get; set; } + [Export] public PackedScene Block { get; set; } + + public override void _Ready() + { + GetTree().Connect("screen_resized", this, "OnWindowResized"); + OnWindowResized(); + + SpawnPlayer(); + SpawnBlocks(); + } + + private void OnWindowResized() + { + var viewport = GetViewport(); + + var scaleX = OS.WindowSize.x / PreferredScreenSize.x; + var scaleY = OS.WindowSize.y / PreferredScreenSize.y; + var scale = Math.Max(1, Mathf.RoundToInt(Mathf.Min(scaleX, scaleY))); + + viewport.Size = (OS.WindowSize / scale).Ceil(); + + // This prevents the viewport from being "squished" to fit the window. + // The difference is only a few pixels, but it results in distortion + // around the center horizontal and vertical lines of the screen. + viewport.SetAttachToScreenRect(new Rect2(0, 0, viewport.Size * scale)); + } + + private void SpawnPlayer() + { + var player = (Player)Player.Instance(); + player.Position = PreferredScreenSize / 2; + AddChild(player); + } + + private void SpawnBlocks() + { + void SpawnBlockAt(int x, int y) + { + var block = (Node2D)Block.Instance(); + block.Position = new Vector2(x, y); + AddChild(block); + } + + // Top and bottom. + for (var x = 16; x <= (int)PreferredScreenSize.x - 16; x += 16) { + SpawnBlockAt(x, 20); + SpawnBlockAt(x, (int)PreferredScreenSize.y - 20); + } + + // Left and right. + for (var y = 36; y <= (int)PreferredScreenSize.y - 36; y += 16) { + SpawnBlockAt(16, y); + SpawnBlockAt((int)PreferredScreenSize.x - 16, y); + } + } +} diff --git a/GameScene.tscn b/GameScene.tscn index 048609d..ee386ff 100644 --- a/GameScene.tscn +++ b/GameScene.tscn @@ -1,53 +1,105 @@ -[gd_scene load_steps=8 format=2] +[gd_scene load_steps=6 format=2] -[ext_resource path="res://gfx/player.png" type="Texture" id=1] +[ext_resource path="res://Player.tscn" type="PackedScene" id=1] [ext_resource path="res://gfx/cursor.png" type="Texture" id=2] -[ext_resource path="res://Player.cs" type="Script" id=3] -[ext_resource path="res://Viewport.cs" type="Script" id=4] +[ext_resource path="res://Block.tscn" type="PackedScene" id=3] +[ext_resource path="res://Game.cs" type="Script" id=4] [ext_resource path="res://Cursor.cs" type="Script" id=5] -[ext_resource path="res://Camera.cs" type="Script" id=6] -[sub_resource type="CircleShape2D" id=1] -radius = 8.0 +[node name="Game" type="Node"] +script = ExtResource( 4 ) +Player = ExtResource( 1 ) +Block = ExtResource( 3 ) -[node name="Game" type="ViewportContainer"] -margin_right = 384.0 -margin_bottom = 216.0 +[node name="Cursor" type="Node2D" parent="."] +z_index = 4096 +script = ExtResource( 5 ) +__meta__ = { +"_edit_group_": true +} + +[node name="Sprite" type="Sprite" parent="Cursor"] +texture = ExtResource( 2 ) + +[node name="HUD" type="CanvasLayer" parent="."] + +[node name="Menu" type="CenterContainer" parent="HUD"] +visible = false +anchor_right = 1.0 +anchor_bottom = 1.0 __meta__ = { -"_edit_lock_": true, "_edit_use_anchors_": false } -[node name="ViewportTexture" type="TextureRect" parent="."] -margin_right = 384.0 -margin_bottom = 216.0 +[node name="Panel" type="PanelContainer" parent="HUD/Menu"] +margin_left = 633.0 +margin_top = 353.0 +margin_right = 647.0 +margin_bottom = 367.0 + +[node name="VBoxContainer" type="VBoxContainer" parent="HUD/Menu/Panel"] +margin_left = 7.0 +margin_top = 7.0 +margin_right = 175.0 +margin_bottom = 113.0 __meta__ = { -"_edit_lock_": true +"_edit_use_anchors_": false } -[node name="Viewport" type="Viewport" parent="."] -size = Vector2( 640, 360 ) -handle_input_locally = false -render_target_v_flip = true -render_target_update_mode = 3 -script = ExtResource( 4 ) +[node name="Label" type="Label" parent="HUD/Menu/Panel/VBoxContainer"] +margin_right = 168.0 +margin_bottom = 14.0 +text = "Menu" +align = 1 +__meta__ = { +"_edit_use_anchors_": false +} -[node name="Cursor" type="Node2D" parent="Viewport"] -script = ExtResource( 5 ) +[node name="HSeparator" type="HSeparator" parent="HUD/Menu/Panel/VBoxContainer"] +margin_top = 18.0 +margin_right = 168.0 +margin_bottom = 22.0 -[node name="Sprite" type="Sprite" parent="Viewport/Cursor"] -texture = ExtResource( 2 ) +[node name="StartServer" type="Button" parent="HUD/Menu/Panel/VBoxContainer"] +margin_top = 26.0 +margin_right = 168.0 +margin_bottom = 46.0 +text = "Start Server" +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="HBoxContainer2" type="HBoxContainer" parent="HUD/Menu/Panel/VBoxContainer"] +margin_top = 50.0 +margin_right = 168.0 +margin_bottom = 74.0 + +[node name="Address" type="LineEdit" parent="HUD/Menu/Panel/VBoxContainer/HBoxContainer2"] +margin_right = 100.0 +margin_bottom = 24.0 +rect_min_size = Vector2( 100, 0 ) +caret_blink = true -[node name="Player" type="KinematicBody2D" parent="Viewport"] -position = Vector2( 320, 180 ) -script = ExtResource( 3 ) +[node name="Connect" type="Button" parent="HUD/Menu/Panel/VBoxContainer/HBoxContainer2"] +margin_left = 104.0 +margin_right = 168.0 +margin_bottom = 24.0 +text = "Connect" -[node name="Camera2D" type="Camera2D" parent="Viewport/Player"] -current = true -script = ExtResource( 6 ) +[node name="Disconnect" type="Button" parent="HUD/Menu/Panel/VBoxContainer"] +visible = false +margin_top = 78.0 +margin_right = 168.0 +margin_bottom = 98.0 +text = "Disconnect" -[node name="CircleShape" type="CollisionShape2D" parent="Viewport/Player"] -shape = SubResource( 1 ) +[node name="HSeparator2" type="HSeparator" parent="HUD/Menu/Panel/VBoxContainer"] +margin_top = 78.0 +margin_right = 168.0 +margin_bottom = 82.0 -[node name="Sprite" type="Sprite" parent="Viewport/Player"] -texture = ExtResource( 1 ) +[node name="BackToGame" type="Button" parent="HUD/Menu/Panel/VBoxContainer"] +margin_top = 86.0 +margin_right = 168.0 +margin_bottom = 106.0 +text = "Back to Game" diff --git a/Player.tscn b/Player.tscn new file mode 100644 index 0000000..0f070aa --- /dev/null +++ b/Player.tscn @@ -0,0 +1,25 @@ +[gd_scene load_steps=5 format=2] + +[ext_resource path="res://Camera.cs" type="Script" id=1] +[ext_resource path="res://gfx/player.png" type="Texture" id=2] +[ext_resource path="res://Player.cs" type="Script" id=3] + +[sub_resource type="CircleShape2D" id=1] +radius = 8.0 + +[node name="Player" type="KinematicBody2D"] +position = Vector2( 640, 360 ) +script = ExtResource( 3 ) +__meta__ = { +"_edit_group_": true +} + +[node name="Camera2D" type="Camera2D" parent="."] +current = true +script = ExtResource( 1 ) + +[node name="CircleShape" type="CollisionShape2D" parent="."] +shape = SubResource( 1 ) + +[node name="Sprite" type="Sprite" parent="."] +texture = ExtResource( 2 ) diff --git a/Viewport.cs b/Viewport.cs deleted file mode 100644 index 4b8667e..0000000 --- a/Viewport.cs +++ /dev/null @@ -1,55 +0,0 @@ -using Godot; -using System; - -public class Viewport : Godot.Viewport -{ - public TextureRect ViewportTexture { get; private set; } - public Vector2 DefaultSize { get; private set; } - public int Scale { get; private set; } - - public override void _Ready() - { - ViewportTexture = GetNode("../ViewportTexture"); - ViewportTexture.Texture = GetTexture(); - GetTree().Root.Connect("size_changed", this, "OnWindowResized"); - - RenderDirectToScreen = false; - DefaultSize = Size; - - OnWindowResized(); - SpawnBlocks(); - } - - private void OnWindowResized() - { - var windowSize = GetTree().Root.Size; - Scale = Math.Max(1, Mathf.RoundToInt(Mathf.Min( - OS.WindowSize.x / DefaultSize.x, - OS.WindowSize.y / DefaultSize.y))); - Size = windowSize / Scale; - ViewportTexture.RectScale = Vector2.One * Scale; - } - - private void SpawnBlocks() - { - var blockScene = GD.Load("res://Block.tscn"); - void SpawnBlockAt(int x, int y) - { - var block = (Node2D)blockScene.Instance(); - block.Position = new Vector2(x, y); - AddChild(block); - } - - // Top and bottom. - for (var x = 16; x <= (int)DefaultSize.x - 16; x += 16) { - SpawnBlockAt(x, 20); - SpawnBlockAt(x, (int)DefaultSize.y - 20); - } - - // Left and right. - for (var y = 36; y <= (int)DefaultSize.y - 36; y += 16) { - SpawnBlockAt(16, y); - SpawnBlockAt((int)DefaultSize.x - 16, y); - } - } -} diff --git a/default_env.tres b/default_env.tres deleted file mode 100644 index 20207a4..0000000 --- a/default_env.tres +++ /dev/null @@ -1,7 +0,0 @@ -[gd_resource type="Environment" load_steps=2 format=2] - -[sub_resource type="ProceduralSky" id=1] - -[resource] -background_mode = 2 -background_sky = SubResource( 1 ) diff --git a/gfx/cursor.png b/gfx/cursor.png index 12b1922..f6d3a75 100644 Binary files a/gfx/cursor.png and b/gfx/cursor.png differ diff --git a/gfx/cursor.png.import b/gfx/cursor.png.import index ce939d3..225c81f 100644 --- a/gfx/cursor.png.import +++ b/gfx/cursor.png.import @@ -20,7 +20,7 @@ compress/hdr_mode=0 compress/bptc_ldr=0 compress/normal_map=0 flags/repeat=0 -flags/filter=true +flags/filter=false flags/mipmaps=false flags/anisotropic=false flags/srgb=2 diff --git a/gfx/icon.png b/gfx/icon.png index c98fbb6..2f783d6 100644 Binary files a/gfx/icon.png and b/gfx/icon.png differ diff --git a/gfx/icon.xcf b/gfx/icon.xcf new file mode 100644 index 0000000..f9ada09 Binary files /dev/null and b/gfx/icon.xcf differ diff --git a/project.godot b/project.godot index ee2ffa5..60508e5 100644 --- a/project.godot +++ b/project.godot @@ -28,7 +28,7 @@ config/icon="res://gfx/icon.png" window/size/width=1280 window/size/height=720 -window/stretch/aspect="expand" +window/stretch/mode="viewport" [gdnative] @@ -82,4 +82,5 @@ move_jump={ [rendering] quality/2d/use_pixel_snap=true -environment/default_environment="res://default_env.tres" +quality/filters/use_nearest_mipmap_filter=true +quality/dynamic_fonts/use_oversampling=false