Headless server should do less unnecessary work now, also tracking host and display concerns should be consistent now. Also redid movement so that balance radius can be calculated easily.

This commit is contained in:
Zergling_man 2025-07-07 08:27:29 +10:00
parent b0e5e89126
commit be986c1d26
13 changed files with 44 additions and 23 deletions

3
globals.gd Normal file
View File

@ -0,0 +1,3 @@
extends Node
var multiplayer_type=0

1
globals.gd.uid Normal file
View File

@ -0,0 +1 @@
uid://b8o32xf6kknhu

View File

@ -25,7 +25,7 @@ func start(id,given_name):
func add_player(pid):
players[pid]={}
var a
if multiplayer.get_unique_id()==1:
if Globals.multiplayer_type&1: # hosting
a=PLAYER_SCENE.instantiate()
a.name=str(pid)
$fuck_layouts/player_list.add_child(a)
@ -50,6 +50,7 @@ func _on_send_message_pressed(message=""):
@rpc("any_peer","call_local")
func receive_message(pid,message):
if !Globals.multiplayer_type&2: return # displaying
var a=Label.new()
a.text=str(pid)+": "+message
var scroll=$fuck_layouts/menu_shiz/chats/elder
@ -75,7 +76,7 @@ func _start_clicked():
@rpc("any_peer","call_local")
func gamestart(players):
emit_signal('game_start',players)
emit_signal('game_start',players,Globals.multiplayer_type)
hide()
func _idk_clicked():

13
menu.gd
View File

@ -14,9 +14,11 @@ func _ready():
if DisplayServer.get_name() == "headless":
# Should attempt to pull port from config file
print("Automatically starting dedicated server.")
_on_host_pressed.call_deferred(true)
_on_host_pressed.call_deferred()
else:
Globals.multiplayer_type|=2 # displaying
func _on_host_pressed(headless=false):
func _on_host_pressed():
# Start as server.
var peer = ENetMultiplayerPeer.new()
var port = $Net/Options/port.text
@ -25,8 +27,9 @@ func _on_host_pressed(headless=false):
if peer.get_connection_status() == MultiplayerPeer.CONNECTION_DISCONNECTED:
OS.alert("Failed to start multiplayer server.")
return
Globals.multiplayer_type|=1 # hosting
multiplayer.multiplayer_peer = peer
start_game(headless)
start_game()
func _on_join_pressed():
# Start as client.
@ -44,7 +47,7 @@ func _on_join_pressed():
multiplayer.multiplayer_peer = peer
start_game() # Can't run a headless client lol what are you trying to bot the game
func start_game(headless=false):
if headless: return
func start_game():
if !Globals.multiplayer_type&2: return # displaying
emit_signal("lobby_join",multiplayer.get_unique_id(),$Net/pname/Name.text)
hide()

View File

@ -15,6 +15,10 @@ run/main_scene="res://ui.tscn"
config/features=PackedStringArray("4.4", "GL Compatibility")
config/icon="res://icon.svg"
[autoload]
Globals="*res://globals.gd"
[dotnet]
project/assembly_name="net-test"

View File

@ -19,10 +19,11 @@ Game::Game()
}
Game::~Game(){}
void Game::start(const Dictionary &players)
void Game::start(const Dictionary &players, const int p_multiplayer_type)
{
show();
if (get_multiplayer()->get_unique_id()!=1) return;
multiplayer_type=p_multiplayer_type;
if (multiplayer_type&2) show(); // displaying
if (!(multiplayer_type&1)) return; // hosting
Array keys=players.keys();
for (int i=0; i<keys.size(); i++)
{
@ -40,6 +41,6 @@ Node* Game::kyaraspawn(const Variant &data)
kya->set_name(dat["name"]);
kya->multiplayerowner=dat["pid"];
kya->set_position(Vector2(50,50));
kya->set_texture(ImageTexture::create_from_image(Image::load_from_file("res://icon.svg")));
if (multiplayer_type&2) kya->set_texture(ResourceLoader::get_singleton()->load("res://icon.svg")); // displaying
return kya;
}

View File

@ -6,8 +6,7 @@
#include "kyara.h"
#include <godot_cpp/classes/multiplayer_api.hpp>
#include <godot_cpp/classes/multiplayer_spawner.hpp>
#include <godot_cpp/classes/image_texture.hpp>
#include <godot_cpp/classes/image.hpp>
#include <godot_cpp/classes/resource_loader.hpp>
namespace godot
{
@ -15,13 +14,14 @@ class Game : public Node2D
{
GDCLASS(Game, Node2D)
private:
int multiplayer_type;
MultiplayerSpawner *spawn;
protected:
static void _bind_methods();
public:
Game();
~Game();
void start(const Dictionary &players);
void start(const Dictionary &players, const int p_multiplayer_type);
Node* kyaraspawn(const Variant &data);
};
}

View File

@ -23,6 +23,7 @@ Character::Character()
set_motion_mode(MOTION_MODE_FLOATING);
speed=1.0;
balance_radius=64.0;
balance_radius_cur=8.0;
face=memnew(Sprite2D);
face->set_name("_face");
add_child(face,false,INTERNAL_MODE_BACK);
@ -42,10 +43,9 @@ Character::~Character(){}
void Character::_process(double delta)
{
// これも嫌い
if (input->flags&1) move(0.0,-speed);
if (input->flags&2) move(0.0,speed);
if (input->flags&4) move(-speed,0.0);
if (input->flags&8) move(speed,0.0);
balance_radius_cur-=3.0;
balance_radius_cur=godot::Math::max(balance_radius_cur,8.0);
move(input->flags);
look_at(input->target);
// Not sure if I should do this. Issues could occur if I'm wrong either side.
//CharacterBody2D::_process(delta);
@ -65,9 +65,15 @@ void Character::_enter_tree()
input->set_multiplayer_authority(multiplayerowner);
}
void Character::move(double x,double y)
void Character::move(int flags)
{
// Collision detection needs to go in here, as well as adjusting and displaying balance radius, buncha shit
if (!flags) return;
// Collision detection needs to go in here, buncha shit
balance_radius_cur+=6.0;
balance_radius_cur=godot::Math::min(balance_radius_cur,balance_radius);
double x=speed*(((flags&8)>>3)-((flags&4)>>2));
double y=speed*(((flags&2)>>1)-((flags&1)>>0));
// Very C
set_position(get_position()+Vector2(x,y));
}

View File

@ -21,7 +21,8 @@ class Character : public CharacterBody2D
private:
double speed;
double balance_radius;
void move(double x,double y);
double balance_radius_cur;
void move(int flags);
Sprite2D *face;
Controller *input;
MultiplayerSynchronizer *sync;

View File

@ -2,7 +2,7 @@
#include "kyara.h"
#include "controller.h"
#include "game.h"
#include "nodespawner.h"
//#include "nodespawner.h"
#include <gdextension_interface.h>
#include <godot_cpp/core/defs.hpp>
#include <godot_cpp/godot.hpp>
@ -15,7 +15,7 @@ void initialize_character_module(ModuleInitializationLevel p_level)
GDREGISTER_RUNTIME_CLASS(Character);
GDREGISTER_RUNTIME_CLASS(Controller);
GDREGISTER_RUNTIME_CLASS(Game);
GDREGISTER_RUNTIME_CLASS(NodeSpawner);
// GDREGISTER_RUNTIME_CLASS(NodeSpawner);
}
void uninitialize_character_module(ModuleInitializationLevel p_level) {

View File

@ -139,6 +139,7 @@ _spawnable_scenes = PackedStringArray("uid://b25q27admm4le")
spawn_path = NodePath("..")
[node name="Game" type="Game" parent="."]
visible = false
[connection signal="lobby_join" from="main_menu" to="Lobby" method="start"]
[connection signal="pressed" from="main_menu/Net/buttons/Host" to="main_menu" method="_on_host_pressed"]