From 95926f7638a9f364fd5faa8581e609d1be3c0717 Mon Sep 17 00:00:00 2001 From: Linus Vogel Date: Fri, 12 Dec 2025 01:08:39 +0100 Subject: [PATCH] background still missing --- Controller.cs | 8 +++--- MainScript.cs | 67 +++++++++++++++++++++++++++++++++++++------ Projection.cs | 64 +++++++++++++++++++++++++++++++++++++++-- Star.cs | 32 ++++++++++++++++++--- config.yaml | 15 ++-------- node_2d.tscn | 2 +- resources/objects.yml | 16 +++++------ scenes.yaml | 42 +++++++++++++++++++-------- 8 files changed, 194 insertions(+), 52 deletions(-) diff --git a/Controller.cs b/Controller.cs index bd1b831..74ddd40 100644 --- a/Controller.cs +++ b/Controller.cs @@ -77,10 +77,10 @@ public partial class Controller : Window this._screenReady = true; } - if (this._btnScene1.IsPressed()) mainScript.SetScene(1); - if (this._btnScene2.IsPressed()) mainScript.SetScene(2); - if (this._btnScene3.IsPressed()) mainScript.SetScene(3); - if (this._btnScene4.IsPressed()) mainScript.SetScene(4); + if (this._btnScene1.IsPressed()) mainScript.SetScene(0); + if (this._btnScene2.IsPressed()) mainScript.SetScene(1); + if (this._btnScene3.IsPressed()) mainScript.SetScene(2); + if (this._btnScene4.IsPressed()) mainScript.SetScene(3); if (this._btnLoad.IsPressed() && this._loadReady) { diff --git a/MainScript.cs b/MainScript.cs index 97f781a..cbe78b7 100644 --- a/MainScript.cs +++ b/MainScript.cs @@ -2,6 +2,7 @@ using Godot; using System; using System.Collections.Generic; using System.IO; +using System.Linq; using StarSkyPresenter; using YamlDotNet.Serialization; using FileAccess = System.IO.FileAccess; @@ -13,7 +14,7 @@ public partial class MainScript : Node2D private bool _configIsLoaded = false; private Dictionary _stars = new Dictionary(); - private List> _scenes = new List>() + private List _scenes = new List(); // Called when the node enters the scene tree for the first time. public override void _Ready() @@ -36,6 +37,16 @@ public partial class MainScript : Node2D DisplayServer.WindowSetMode(DisplayServer.WindowMode.ExclusiveFullscreen, window.GetWindowId()); } + public List GetStars() + { + return this._stars.Values.ToList(); + } + + public List GetScenes() + { + return this._scenes; + } + public int GetScene() { return this._scene; @@ -63,9 +74,9 @@ public partial class MainScript : Node2D // deserialize the values IDeserializer deser = new DeserializerBuilder().Build(); Objects obj_conf = deser.Deserialize(new StreamReader(configFile)); - List> scene_conf = deser.Deserialize>>(new StreamReader(scenesFile)); + List scene_conf = deser.Deserialize>(new StreamReader(scenesFile)); - Dictionary stars = new Dictionary(); + this._stars.Clear(); foreach (StarConfig star_config in obj_conf.stars) { @@ -85,7 +96,7 @@ public partial class MainScript : Node2D Sprite = sprite }; - stars.Add(star_config.id, star); + this._stars.Add(star_config.id, star); } foreach (Node child in projection.GetChildren()) @@ -94,23 +105,63 @@ public partial class MainScript : Node2D child.QueueFree(); } - foreach (KeyValuePair star in stars) + foreach (KeyValuePair star in this._stars) { projection.AddChild(star.Value.Sprite); + star.Value.Sprite.ZIndex = 5; } - List> scenes = new List>(); + List scenes = new List(); foreach (var scene in scene_conf) { - foreach (KeyValuePair kv in scene) + Dictionary namedStars = new Dictionary(); + foreach (KeyValuePair kv in this._stars) { - // TODO: continue here + Star star; + // check whether a given object is overridden in this scene + if (scene.stars.ContainsKey(kv.Key)) + { + // compute the combination of the scene with changes applied + var sceneStar = scene.stars[kv.Key]; + star = new Star + { + Id = kv.Value.Id, + X = sceneStar.x ?? kv.Value.X, + Y = sceneStar.y ?? kv.Value.Y, + Rot = sceneStar.rot ?? kv.Value.Rot, + Scale = sceneStar.scale ?? kv.Value.Scale, + Sprite = (Sprite2D)kv.Value.Sprite.Duplicate(), + }; + } + else + { + // copy the original if not + star = new Star + { + Id = kv.Value.Id, + X = kv.Value.X, + Y = kv.Value.Y, + Rot = kv.Value.Rot, + Scale = kv.Value.Scale, + Sprite = (Sprite2D)kv.Value.Sprite.Duplicate() + }; + } + + namedStars.Add(kv.Key, star); } + + scenes.Add(new Scene + { + transition = scene.transition, + stars = namedStars + }); } + GD.Print(scenes.Count); configFile.Close(); scenesFile.Close(); this._configIsLoaded = true; + this._scenes = scenes; } } diff --git a/Projection.cs b/Projection.cs index 4755319..dc7d0ea 100644 --- a/Projection.cs +++ b/Projection.cs @@ -1,13 +1,16 @@ using Godot; using System; using System.Collections.Generic; +using System.Numerics; +using StarSkyPresenter; using YamlDotNet.Serialization; +using Vector2 = Godot.Vector2; public partial class Projection : Window { private double _sceneTime = 0; - private int _lastScene = 1; - private int _activeScene = 1; + private int _lastScene = 0; + private int _activeScene = 0; // Called when the node enters the scene tree for the first time. @@ -16,12 +19,35 @@ public partial class Projection : Window } + private double Fapply(double input, string fname) + { + switch (fname) + { + case "lin": return input; + case "sin": return Math.Sin(input); + case "cos": return 1.0 - Math.Cos(input); + case "sin2": return Math.Sin(input)*Math.Sin(input); + default: return 0; + } + } + + private double FadeTime(bool fadeIn, bool fadeOut, double t) + { + switch ((fadeIn, fadeOut)) + { + case (true, true): return Math.Sin(t)*Math.Sin(t); + case (true, false): return 1 - Math.Cos(t); + case (false, true): return Math.Sin(t); + case (false, false): return t; + } + } + // Called every frame. 'delta' is the elapsed time since the previous frame. public override void _Process(double delta) { // get MainScript instance MainScript mainScript = (MainScript)this.GetTree().GetRoot().GetNode("Main"); - Window projectionWindow = GetNode("/root/Main/Projection"); + if (mainScript.GetScenes().Count == 0) return; // check for scene update and if so reset the scene time if (mainScript.GetScene() != this._activeScene) @@ -35,6 +61,38 @@ public partial class Projection : Window this._sceneTime += delta; } + // TODO: calculate scene weight + var transition = mainScript.GetScenes()[this._lastScene].transition; + string xFade = transition.x; + string yFade = transition.y; + double t = (Math.Clamp(this._sceneTime / transition.t, 0, 1)) * 3.14159265 / 2; + double weight = FadeTime(transition.fade_out, transition.fade_in, t); + Vector2 posWeight = new Vector2((float)Fapply(weight * 3.14159265 / 2, yFade), (float)Fapply(weight * 3.14159265 / 2, xFade)); + + GD.Print("T: " + t + ", weight: " + weight + ", posWeight: " + posWeight); + + // update background + // TODO: update background + + // update all the stars + foreach (Star star in mainScript.GetStars()) + { + var oldStar = mainScript.GetScenes()[this._lastScene].stars[star.Id]; + var newStar = mainScript.GetScenes()[this._activeScene].stars[star.Id]; + + float baseScale = ((float)this.GetSize().X / star.Sprite.GetTexture().GetWidth()) / 100; + + double activeRot = (1 - weight) * oldStar.Rot + weight * newStar.Rot; + Vector2 activePos = (Vector2.One - posWeight) * new Vector2(oldStar.X, oldStar.Y) + posWeight * new Vector2(newStar.X, newStar.Y); + float activeScale = ((1 - (float)weight) * oldStar.Scale + (float)weight * newStar.Scale) * baseScale; + + //GD.Print("Scene: " + this._activeScene + ", Time: " + this._sceneTime + ", Weight: " + weight + ", ActiveRot: " + activeRot + ", ActivePos: " + activePos + ", ActiveScale: " + activeScale); + + star.Sprite.Rotation += (float)(((2*3.14159265)*(activeRot / 365)) * delta); + star.Sprite.Position = activePos * this.GetSize(); + star.Sprite.Scale = Vector2.One * activeScale; + } + } } diff --git a/Star.cs b/Star.cs index 1539658..f0cbcec 100644 --- a/Star.cs +++ b/Star.cs @@ -1,3 +1,4 @@ +using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using Godot; @@ -17,10 +18,33 @@ public class StarConfig [SuppressMessage("ReSharper", "InconsistentNaming")] public class StarSceneData { - public float x { get; set; } - public float y { get; set; } - public float rot { get; set; } - public float scale { get; set; } + public float? x { get; set; } + public float? y { get; set; } + public float? rot { get; set; } + public float? scale { get; set; } +} + +[SuppressMessage("ReSharper", "InconsistentNaming")] +public class TransitionConfig +{ + public bool fade_in { get; set; } = false; + public bool fade_out { get; set; } = false; + public double t { get; set; } = 1; + public string x { get; set; } = "lin"; + public string y { get; set; } = "lin"; +} + +[SuppressMessage("ReSharper", "InconsistentNaming")] +public class SceneConfig +{ + public Dictionary stars { get; set; } + public TransitionConfig transition { get; set; } +} + +public class Scene +{ + public Dictionary stars { get; set; } + public TransitionConfig transition { get; set; } } public class Star diff --git a/config.yaml b/config.yaml index daa0ab2..f39f80d 100644 --- a/config.yaml +++ b/config.yaml @@ -1,18 +1,9 @@ - stars: - id: star-1 - x: 50 - y: 50 - rot: 15 + x: 0.1 + y: 0.1 scale: 5 + rot: 5 image: nice-star-1.png - - id: star-2 - x: 25 - y: 25 - rot: 10 - scale: 4 - image: nice-star-2.png - - background: background-01.jpg frameRate: 30 \ No newline at end of file diff --git a/node_2d.tscn b/node_2d.tscn index 7bb9265..0d89216 100644 --- a/node_2d.tscn +++ b/node_2d.tscn @@ -39,7 +39,7 @@ offset_left = 420.0 offset_top = 30.0 offset_right = 520.0 offset_bottom = 90.0 -text = "Scene 3" +text = "Scene 4" [node name="BtnClose" type="Button" parent="Controller"] offset_left = 42.0 diff --git a/resources/objects.yml b/resources/objects.yml index 0c4aa74..a510071 100644 --- a/resources/objects.yml +++ b/resources/objects.yml @@ -1,11 +1,11 @@ stars: - sid: star-1 - x: 0.1 - y: 0.1 - scale: 5 - alpha: 0 - rot: 5 - offset: 0 - image: nice-star-1.png + x: 0.1 + y: 0.1 + scale: 5 + alpha: 0 + rot: 5 + offset: 0 + image: nice-star-1.png background: background-01.jpg -framerate: 30 \ No newline at end of file +framerate: 30 diff --git a/scenes.yaml b/scenes.yaml index e8e8aaf..3026c76 100644 --- a/scenes.yaml +++ b/scenes.yaml @@ -1,12 +1,30 @@ -- star-1: - x: 50 - y: 50 - star-2: - x: 60 - y: 0 -- star-1: - x: 10 - y: 10 - star-2: - x: 25 - y: 25 +- stars: + star-1: + x: 1.1 + y: 1.1 + transition: + t: 5 + fade_in: true + fade_out: false + x: sin + y: cos +- stars: + star-1: + x: 0.5 + y: 0.5 + transition: + t: 5 + fade_in: false + fade_out: true + x: cos + y: sin +- stars: + star-1: + x: -0.1 + y: 1.1 + transition: + t: 5 + fade_in: true + fade_out: false + x: sin + y: cos \ No newline at end of file