first commit

This commit is contained in:
2022-07-02 21:23:45 +08:00
commit 7cdcaf1e2f
102 changed files with 7208 additions and 0 deletions

37
scripts/GameRoot.cs Normal file
View File

@ -0,0 +1,37 @@
using Godot;
using System;
public class GameRoot : Node {
MenuManager mMenuManager;
StalkerCore mStalkerCore;
public override void _Ready() {
mMenuManager = GetNode<MenuManager>("UILayer/MenuManager");
mStalkerCore = GetNode<StalkerCore>("StalkerCore");
mMenuManager.Connect(nameof(MenuManager.SetMouseCapture), this, nameof(Proc_MenuManager_SetMouseCapture));
mMenuManager.Connect(nameof(MenuManager.ExitGame), this, nameof(Proc_MenuManager_ExitGame));
// raw executing this func
// to set proper status
Proc_MenuManager_SetMouseCapture();
}
public override void _Input(InputEvent @event) {
}
private void Proc_MenuManager_SetMouseCapture() {
if (mMenuManager.Visible) {
Input.SetMouseMode(Input.MouseMode.Visible);
} else {
Input.SetMouseMode(Input.MouseMode.Captured);
}
}
private void Proc_MenuManager_ExitGame() {
GetTree().Notification(MainLoop.NotificationWmQuitRequest);
}
}

View File

@ -0,0 +1,63 @@
using BallanceStalker;
using Godot;
using System;
public class MenuManager : Control {
public enum MenuPage : int {
Main,
LoadLevel,
LoadSky,
Multiplayer,
About
}
[Signal]
public delegate void SetMouseCapture();
[Signal]
public delegate void ExitGame();
private MenuMain mMenuMain;
private MenuPage mCurrentPage;
// Called when the node enters the scene tree for the first time.
public override void _Ready() {
mCurrentPage = MenuPage.Main;
mMenuMain = GetNode<MenuMain>("MenuMain");
mMenuMain.Connect(nameof(MenuMain.MenuMain_GotoPage), this, nameof(Proc_MenuMain_GotoPage));
mMenuMain.Connect(nameof(MenuMain.MenuMain_Back), this, nameof(Proc_MenuMain_Back));
mMenuMain.Connect(nameof(MenuMain.MenuMain_Exit), this, nameof(Proc_MenuMain_Exit));
}
public override void _Input(InputEvent @event) {
if (Input.IsActionJustPressed("ballance_esc")) {
if (mCurrentPage == MenuPage.Main) {
// we are in main menu, we need switch visible
this.Visible = !this.Visible;
EmitSignal(nameof(SetMouseCapture));
} else {
// otherwise, back from sub menu
RefreshMenuPage(MenuPage.Main);
}
}
}
private void RefreshMenuPage(MenuPage page_type) {
;//todo:
//mCurrentPage = page_type;
}
private void Proc_MenuMain_GotoPage(MenuPage menu_type) {
RefreshMenuPage(menu_type);
}
private void Proc_MenuMain_Back() {
this.Visible = false;
EmitSignal(nameof(SetMouseCapture));
}
private void Proc_MenuMain_Exit() {
EmitSignal(nameof(ExitGame));
}
}

View File

@ -0,0 +1,195 @@
using Godot;
using System;
// Reference: https://github.com/godotengine/godot-demo-projects/blob/master/3d/waypoints
public class PlayerBall : Spatial {
static readonly float MARGIN = 16f; // set it as half of arrow image
static readonly float TEXT_RADIUS = MARGIN + 16f;
public string Playername {
get { return mPlayername.Text; }
set { mPlayername.Text = value; }
}
public bool AlwaysTracking { get; set; }
Control mCtl2D;
Spatial mTextArchor;
TextureRect mPlayerArrow;
Label mPlayername;
Camera mSpectatorCamera = null;
public override void _Ready() {
mCtl2D = GetNode<Control>("TextArchor/Ctl2D");
mTextArchor = GetNode<Spatial>("TextArchor");
mPlayerArrow = GetNode<TextureRect>("TextArchor/Ctl2D/PlayerArrow");
mPlayername = GetNode<Label>("TextArchor/Ctl2D/Playername");
AlwaysTracking = true;
Playername = "";
}
public override void _Process(float delta) {
if (mSpectatorCamera is null || !mSpectatorCamera.Current) {
mSpectatorCamera = GetViewport().GetCamera();
}
if (mSpectatorCamera is null) return;
var parent_translation = mTextArchor.GlobalTransform.origin;
var camera_transform = mSpectatorCamera.GlobalTransform;
var camera_translation = camera_transform.origin;
var unprojected_position = mSpectatorCamera.UnprojectPosition(parent_translation);
// `get_size_override()` will return a valid size only if the stretch mode is `2d`.
// Otherwise, the viewport size is used directly.
var viewport_base_size = GetViewport().GetSizeOverride() > Vector2.Zero ? GetViewport().GetSizeOverride() : GetViewport().Size;
// unprojected_position will return some illegal number, such as inf or nan, so we need to check them here
// if something bad happend, we need hide all UI elements and return immediately
if (Mathf.IsInf(unprojected_position.x) || Mathf.IsInf(unprojected_position.y) ||
Mathf.IsNaN(unprojected_position.x) || Mathf.IsNaN(unprojected_position.y)) {
mCtl2D.Visible = false;
return;
} else {
mCtl2D.Visible = true;
}
// We would use "camera.is_position_behind(parent_translation)", except
// that it also accounts for the near clip plane, which we don't want.
var is_behind = camera_transform.basis.z.Dot(parent_translation - camera_translation) > 0f;
// Fade the waypoint when the camera gets close.
//var distance = camera_translation.DistanceTo(parent_translation);
//mCtl2D.Modulate.a = Godot.Mathf.Clamp(RangeLerp range_lerp(distance, 0, 2, 0, 1), 0, 1);
if (!AlwaysTracking) {
// For non-sticky waypoints, we don't need to clamp and calculate
// the position if the waypoint goes off screen.
mCtl2D.RectPosition = unprojected_position;
mCtl2D.Visible = !is_behind;
return;
}
// calculate corner position
// We use Liang - Barsky clip to implement this function
var viewport_center = viewport_base_size / 2f;
if (is_behind) {
unprojected_position = viewport_center + (viewport_center - unprojected_position);
}
var relative_pos = unprojected_position - viewport_center;
/*
Liang-Barsky clip
wx_left <= x1+u(x2-x1) <= wx_right
wy_top <= y1+u(y2-y1) <= wy_bottom
p1 = x1-x2;
p2 = x2-x1;
p3 = y1-y2;
p4 = y2-y1;
assume x1 = y1 = 0
left: u(-x2) <= -wx_left
right: u(x2) <= wx_right
bottom: u(y2) <= wy_bottom
top: u(-y2) <= -wy_top
when <= become ==, solve u to get corner data:
left: u1 = wx_left / x2
right: u2 = wx_right / x2
bottom: u3 = wy_bottom / y2
top: u4 = wy_top / y2
u = 0 and 1 indicate the start point and end point of this line.
but we don't need it when `is_behind` is true.
so we modify LB clip method in there
we dont need check requirement: uone > utwo, because it is impossible
that line is outside of viewport entirely.
*/
float u2 = (viewport_center.x - MARGIN) / relative_pos.x,
u1 = -u2;
float u3 = (viewport_center.y - MARGIN) / relative_pos.y,
u4 = -u3;
// becasue p1 = -p2, p3 = -p4
// we need move number to let u1 become negative one and u2 become positive one.
// also the u3, u4.
float cache;
if (Mathf.Sign(relative_pos.x) < 0) {
cache = u1;
u1 = u2;
u2 = cache;
}
if (Mathf.Sign(relative_pos.y) > 0) {
cache = u3;
u3 = u4;
u4 = cache;
}
// we don't need uone, because uone is a const
float uone = 0f, utwo = 1f;
if (relative_pos.x == 0f) {
// only y is valid
if (relative_pos.y == 0f) {
// something goes wrong.
mCtl2D.Visible = false;
return;
} else {
// pick
uone = u3;
utwo = u4;
}
} else {
uone = Mathf.Max(u1, u3);
utwo = Mathf.Min(u2, u4);
}
bool outside_window;
if (!is_behind) {
// if object is not behind the camera, we need to restore the real position of it.
// because previous process assume that the start point and end point of line is ignored.
uone = Mathf.Max(0f, uone);
utwo = Mathf.Min(1f, utwo);
outside_window = utwo != 1f;
} else {
outside_window = true;
}
mCtl2D.RectPosition = (relative_pos * utwo) + viewport_center;
// if player ball is bebind us camera, we need to show a arrow and a playername with it
// otherwise, we only need display username
mPlayerArrow.Visible = outside_window;
if (outside_window) {
// Used to display a diagonal arrow when the waypoint is displayed in
// one of the screen corners.
var deg = Mathf.Rad2Deg(mCtl2D.RectPosition.AngleToPoint(viewport_center));
mPlayerArrow.RectRotation = deg;
var text_pos = mCtl2D.RectPosition.DirectionTo(viewport_center) * TEXT_RADIUS;
text_pos.x -= RangeLerp(mCtl2D.RectPosition.x, MARGIN, viewport_base_size.x - MARGIN, 0f, mPlayername.RectSize.x);
text_pos.y -= RangeLerp(mCtl2D.RectPosition.y, MARGIN, viewport_base_size.y - MARGIN, 0f, mPlayername.RectSize.y);
mPlayername.RectPosition = text_pos;
} else {
mPlayername.RectPosition = (-mPlayername.RectSize) / 2f;
}
}
//private static float AngelDiff(float from, float to) {
// var diff = (to - from) % Mathf.Tau;
// return ((2.0f * diff) % Mathf.Tau) - diff;
//}
private static float RangeLerp(float value, float istart, float istop, float ostart, float ostop) {
return ostart + (ostop - ostart) * (value - istart) / (istop - istart);
}
}

View File

@ -0,0 +1,71 @@
using Godot;
using System;
using System.Collections.Generic;
public class ShadowBall {
public UInt32 mIdentifier;
public string mName;
public PlayerBall mGodotNode;
}
public class ShadowBallManager : Spatial {
private Dictionary<UInt32, ShadowBall> mBallDict = new Dictionary<UInt32, ShadowBall>();
private PackedScene mTemplateShadowBall;
// Called when the node enters the scene tree for the first time.
public override void _Ready() {
mTemplateShadowBall = ResourceLoader.Load<PackedScene>("res://scenes/stages/PlayerBall.tscn");
int len = 10;
Quat quad = Quat.Identity;
Vector3 vec = new Vector3(0, 0, 0);
for (int x = 0; x < len; x++) {
for (int y = 0; y < len; y++) {
AddBall((uint)(x * len + y), $"Swung0x{x * len + y}");
vec.x = x * 50;
vec.z = y * 50;
SetBallState((uint)(x * len + y), vec, quad);
}
}
}
// // Called every frame. 'delta' is the elapsed time since the previous frame.
// public override void _Process(float delta)
// {
//
// }
public void AddBall(UInt32 ballid, string name) {
if (mBallDict.TryGetValue(ballid, out ShadowBall entity)) {
// update old entity for alternative
entity.mName = name;
entity.mGodotNode.Playername = name;
} else {
entity = new ShadowBall() {
mIdentifier = ballid,
mName = name,
mGodotNode = mTemplateShadowBall.Instance<PlayerBall>()
};
mBallDict.Add(ballid, entity);
AddChild(entity.mGodotNode);
entity.mGodotNode.Playername = entity.mName;
}
}
public void SetBallState(UInt32 ballid, Vector3 pos, Quat quad) {
if (mBallDict.TryGetValue(ballid, out ShadowBall entity)) {
entity.mGodotNode.Translation = pos;
entity.mGodotNode.Rotation = quad.GetEuler();
}
}
public void RemoveBall(UInt32 ballid) {
if (mBallDict.TryGetValue(ballid, out ShadowBall entity)) {
RemoveChild(entity.mGodotNode);
entity.mGodotNode.QueueFree();
mBallDict.Remove(ballid);
}
}
}

View File

@ -0,0 +1,70 @@
using Godot;
using System;
public class StalkerCamera : Spatial
{
Spatial originFreeCam;
Camera freeCam;
private static float BASIC_MOVEMENT = 0.2f;
object lockMotionMouse = new object();
Vector2 motionMouse = new Vector2(0, 0);
int movementScale = 1;
public override void _Ready() {
freeCam = GetNode<Camera>("FreeCam");
originFreeCam = GetNode<Spatial>("FreeCamOrigin");
//Input.SetMouseMode(Input.MouseMode.Captured);
}
public override void _Process(float delta) {
if (Input.GetMouseMode() != Input.MouseMode.Captured) return;
Vector3 motion3d = new Vector3(0, 0, 0);
if (Input.IsActionPressed("ballance_forward")) motion3d.z -= 1f;
if (Input.IsActionPressed("ballance_backward")) motion3d.z += 1f;
if (Input.IsActionPressed("ballance_left")) motion3d.x -= 1f;
if (Input.IsActionPressed("ballance_right")) motion3d.x += 1f;
if (Input.IsActionPressed("ballance_down")) motion3d.y -= 1f;
if (Input.IsActionPressed("ballance_up")) motion3d.y += 1f;
freeCam.Translate(motion3d * (BASIC_MOVEMENT * movementScale));
Vector2 copiedMotion2d;
lock (lockMotionMouse) {
copiedMotion2d = motionMouse;
motionMouse = new Vector2(0, 0);
}
float window_x = GetViewport().GetVisibleRect().Size.x;
Vector3 camRot = freeCam.Rotation;
camRot.x = Mathf.Clamp(camRot.x - copiedMotion2d.y * 2 / window_x, -90f, 90f);
camRot.y = camRot.y - copiedMotion2d.x * 2 / window_x;
camRot.z = 0;
freeCam.Rotation = camRot;
}
public override void _Input(InputEvent @event) {
if (Input.GetMouseMode() != Input.MouseMode.Captured) return;
// mouse wheel
if (@event is InputEventMouseButton) {
InputEventMouseButton emb = (InputEventMouseButton)@event;
if (emb.IsPressed()) {
if (emb.ButtonIndex == (int)ButtonList.WheelUp) {
movementScale = Mathf.Clamp(movementScale + 1, 1, 50);
}
if (emb.ButtonIndex == (int)ButtonList.WheelDown) {
movementScale = Mathf.Clamp(movementScale - 1, 1, 50);
}
}
}
// mouse motion
if (@event is InputEventMouseMotion) {
var mov = (InputEventMouseMotion)@event;
lock (lockMotionMouse) {
motionMouse += mov.Relative;
}
}
}
}

View File

@ -0,0 +1,20 @@
using Godot;
using System;
public class StalkerCore : Spatial {
// Declare member variables here. Examples:
// private int a = 2;
// private string b = "text";
// Called when the node enters the scene tree for the first time.
public override void _Ready() {
}
// // Called every frame. 'delta' is the elapsed time since the previous frame.
// public override void _Process(float delta)
// {
//
// }
}

View File

@ -0,0 +1,34 @@
using BallanceStalker;
using Godot;
using System;
public class MenuMain : Control {
[Signal]
public delegate void MenuMain_GotoPage(MenuManager.MenuPage menu_type);
[Signal]
public delegate void MenuMain_Back();
[Signal]
public delegate void MenuMain_Exit();
// Called when the node enters the scene tree for the first time.
public override void _Ready() {
GetNode<ButtonCommon>("MenuList/BtnLoadLevel").Connect("pressed", this, nameof(Proc_GotoPage), new Godot.Collections.Array() { MenuManager.MenuPage.LoadLevel });
GetNode<ButtonCommon>("MenuList/BtnLoadSky").Connect("pressed", this, nameof(Proc_GotoPage), new Godot.Collections.Array() { MenuManager.MenuPage.LoadSky });
GetNode<ButtonCommon>("MenuList/BtnMultiplayer").Connect("pressed", this, nameof(Proc_GotoPage), new Godot.Collections.Array() { MenuManager.MenuPage.Multiplayer });
GetNode<ButtonCommon>("MenuList/BtnAbout").Connect("pressed", this, nameof(Proc_GotoPage), new Godot.Collections.Array() { MenuManager.MenuPage.About });
GetNode<ButtonCommon>("MenuList/BtnBack").Connect("pressed", this, nameof(Proc_Back));
GetNode<ButtonCommon>("MenuList/BtnExit").Connect("pressed", this, nameof(Proc_Exit));
}
private void Proc_GotoPage(MenuManager.MenuPage menu_type) {
EmitSignal(nameof(MenuMain_GotoPage), menu_type);
}
private void Proc_Back() {
EmitSignal(nameof(MenuMain_Back));
}
private void Proc_Exit() {
EmitSignal(nameof(MenuMain_Exit));
}
}

View File

@ -0,0 +1,62 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BallanceStalkerCore {
public enum BmmoClientStatus : UInt32 {
Ready,
Initializing,
Running,
Stopping,
Stopped
}
public struct VxVector {
float x, y, z;
}
public struct VxQuaternion {
float x, y, z, w;
}
public struct VxBallState {
UInt32 player_id;
VxVector pos;
VxQuaternion quad;
}
public class PlayerEntity {
public string mPlayerName;
public UInt32 mPlayerGnsUid;
public bool mPlayerCheated;
}
public class BmmoClient {
public BmmoClient(LogManager logger) {
mLogger = logger;
}
// receive message in godot, you should use call_defer functions to
// reflact data delivered by event on engine
#region event system
public event Action<VxBallState[]> EventBallState;
public event Action<string, string> EventChat;
public event Action<PlayerEntity> EventPlayerConnect;
public event Action<UInt32> EventPlayerDisconnect;
#endregion
private LogManager mLogger;
private Dictionary<UInt32, PlayerEntity> mPlayersDict;
public void Start(string host, UInt16 port) {
}
public void Stop() {
}
}
}

View File

@ -0,0 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BallanceStalkerCore {
public class LogManager {
}
}

View File

@ -0,0 +1,21 @@
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BallanceStalkerCore {
public class StalkerManager {
public static StalkerManager Singleton = new StalkerManager();
private StalkerManager() {
}
public BmmoClient mBmmoClient;
public LogManager mLogger;
}
}

View File

@ -0,0 +1,16 @@
using Godot;
using System;
public class ButtonCommon : Button
{
AudioStreamPlayer audio;
public override void _Ready() {
this.Connect("pressed", this, nameof(event_Btn_pressed));
audio = GetNode<AudioStreamPlayer>("AudioClick");
}
private void event_Btn_pressed() {
audio.Play();
}
}

View File

@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BallanceStalker {
public class GodotPollutedObject : Godot.Object {
public GodotPollutedObject() {
Data = null;
}
public GodotPollutedObject(object deliver) {
Data = deliver;
}
private object Data;
public T GetData<T>() where T : class {
return Data as T;
}
}
}