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 iconmain
parent
8cdbcc283e
commit
56fdb1ddcd
12 changed files with 188 additions and 114 deletions
@ -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 ) |
|
Before Width: | Height: | Size: 307 B After Width: | Height: | Size: 12 KiB |
Before Width: | Height: | Size: 3.2 KiB After Width: | Height: | Size: 22 KiB |
Binary file not shown.
Loading…
Reference in new issue