Redo pixel-perfect zoom & more

Game now uses the default viewport instead of
a separate one that's then drawn as a texture.
This makes anything involving mouse position
much easier to deal with.

- Disable camera moving depending on cursor position
- Start work on an "escape menu" with
  controls to join multiplayer sessions
- Move player to scene, instanciate on start
- Change application icon
main
copygirl 5 years ago
parent 8cdbcc283e
commit 56fdb1ddcd
  1. 17
      Camera.cs
  2. 5
      Cursor.cs
  3. 64
      Game.cs
  4. 122
      GameScene.tscn
  5. 25
      Player.tscn
  6. 55
      Viewport.cs
  7. 7
      default_env.tres
  8. BIN
      gfx/cursor.png
  9. 2
      gfx/cursor.png.import
  10. BIN
      gfx/icon.png
  11. BIN
      gfx/icon.xcf
  12. 5
      project.godot

@ -2,23 +2,20 @@ using Godot;
public class Camera : Camera2D public class Camera : Camera2D
{ {
public int MaxDistance { get; } = 80;
public Cursor Cursor { get; private set; } public Cursor Cursor { get; private set; }
private Vector2 _rawPosition = Vector2.Zero; public override void _EnterTree()
public override void _Ready()
{ {
Cursor = GetViewport().GetNode<Cursor>("Cursor"); Cursor = GetViewport().GetNode<Cursor>("Cursor");
} }
public override void _Process(float delta) public override void _Process(float delta)
{ {
var mousePos = GetTree().Root.GetMousePosition(); // TODO: Implement some kind of "zoom" mechanic?
var centerPos = OS.WindowSize / 2; // var mousePos = GetTree().Root.GetMousePosition();
var target = !Cursor.Visible ? Vector2.Zero // var centerPos = OS.WindowSize / 2;
: ((mousePos - centerPos) / 4).Clamped(MaxDistance) * 2; // var scale = ((Viewport)GetViewport()).Scale;
_rawPosition = _rawPosition.LinearInterpolate(target, 0.05F).Round(); // Position = !Cursor.Visible ? Vector2.Zero
Position = _rawPosition.Round(); // : ((mousePos - centerPos) / scale).Clamped(MaxDistance) / 2;
} }
} }

@ -4,7 +4,6 @@ public class Cursor : Node2D
{ {
public override void _Ready() public override void _Ready()
{ {
Visible = false;
Input.SetMouseMode(Input.MouseMode.Hidden); Input.SetMouseMode(Input.MouseMode.Hidden);
} }
@ -18,8 +17,6 @@ public class Cursor : Node2D
public override void _Process(float delta) public override void _Process(float delta)
{ {
var viewport = (Viewport)GetViewport(); Position = GetGlobalMousePosition();
var origin = viewport.CanvasTransform.origin;
Position = viewport.GetMousePosition() / viewport.Scale - origin;
} }
} }

@ -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);
}
}
}

@ -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://gfx/cursor.png" type="Texture" id=2]
[ext_resource path="res://Player.cs" type="Script" id=3] [ext_resource path="res://Block.tscn" type="PackedScene" id=3]
[ext_resource path="res://Viewport.cs" type="Script" id=4] [ext_resource path="res://Game.cs" type="Script" id=4]
[ext_resource path="res://Cursor.cs" type="Script" id=5] [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] [node name="Game" type="Node"]
radius = 8.0 script = ExtResource( 4 )
Player = ExtResource( 1 )
Block = ExtResource( 3 )
[node name="Game" type="ViewportContainer"] [node name="Cursor" type="Node2D" parent="."]
margin_right = 384.0 z_index = 4096
margin_bottom = 216.0 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__ = { __meta__ = {
"_edit_lock_": true,
"_edit_use_anchors_": false "_edit_use_anchors_": false
} }
[node name="ViewportTexture" type="TextureRect" parent="."] [node name="Panel" type="PanelContainer" parent="HUD/Menu"]
margin_right = 384.0 margin_left = 633.0
margin_bottom = 216.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__ = { __meta__ = {
"_edit_lock_": true "_edit_use_anchors_": false
} }
[node name="Viewport" type="Viewport" parent="."] [node name="Label" type="Label" parent="HUD/Menu/Panel/VBoxContainer"]
size = Vector2( 640, 360 ) margin_right = 168.0
handle_input_locally = false margin_bottom = 14.0
render_target_v_flip = true text = "Menu"
render_target_update_mode = 3 align = 1
script = ExtResource( 4 ) __meta__ = {
"_edit_use_anchors_": false
}
[node name="Cursor" type="Node2D" parent="Viewport"] [node name="HSeparator" type="HSeparator" parent="HUD/Menu/Panel/VBoxContainer"]
script = ExtResource( 5 ) margin_top = 18.0
margin_right = 168.0
margin_bottom = 22.0
[node name="Sprite" type="Sprite" parent="Viewport/Cursor"] [node name="StartServer" type="Button" parent="HUD/Menu/Panel/VBoxContainer"]
texture = ExtResource( 2 ) 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"] [node name="Connect" type="Button" parent="HUD/Menu/Panel/VBoxContainer/HBoxContainer2"]
position = Vector2( 320, 180 ) margin_left = 104.0
script = ExtResource( 3 ) margin_right = 168.0
margin_bottom = 24.0
text = "Connect"
[node name="Camera2D" type="Camera2D" parent="Viewport/Player"] [node name="Disconnect" type="Button" parent="HUD/Menu/Panel/VBoxContainer"]
current = true visible = false
script = ExtResource( 6 ) margin_top = 78.0
margin_right = 168.0
margin_bottom = 98.0
text = "Disconnect"
[node name="CircleShape" type="CollisionShape2D" parent="Viewport/Player"] [node name="HSeparator2" type="HSeparator" parent="HUD/Menu/Panel/VBoxContainer"]
shape = SubResource( 1 ) margin_top = 78.0
margin_right = 168.0
margin_bottom = 82.0
[node name="Sprite" type="Sprite" parent="Viewport/Player"] [node name="BackToGame" type="Button" parent="HUD/Menu/Panel/VBoxContainer"]
texture = ExtResource( 1 ) margin_top = 86.0
margin_right = 168.0
margin_bottom = 106.0
text = "Back to Game"

@ -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 )

@ -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<TextureRect>("../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<PackedScene>("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);
}
}
}

@ -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 )

Binary file not shown.

Before

Width:  |  Height:  |  Size: 307 B

After

Width:  |  Height:  |  Size: 12 KiB

@ -20,7 +20,7 @@ compress/hdr_mode=0
compress/bptc_ldr=0 compress/bptc_ldr=0
compress/normal_map=0 compress/normal_map=0
flags/repeat=0 flags/repeat=0
flags/filter=true flags/filter=false
flags/mipmaps=false flags/mipmaps=false
flags/anisotropic=false flags/anisotropic=false
flags/srgb=2 flags/srgb=2

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.2 KiB

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

@ -28,7 +28,7 @@ config/icon="res://gfx/icon.png"
window/size/width=1280 window/size/width=1280
window/size/height=720 window/size/height=720
window/stretch/aspect="expand" window/stretch/mode="viewport"
[gdnative] [gdnative]
@ -82,4 +82,5 @@ move_jump={
[rendering] [rendering]
quality/2d/use_pixel_snap=true 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

Loading…
Cancel
Save