-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorld.cs
203 lines (166 loc) · 6.79 KB
/
World.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
using Godot;
using System;
using LD56;
public partial class World : Node2D
{
public readonly PackedScene deathParticles = GD.Load<PackedScene>("res://particles/death_particles.tscn");
[Export] public PackedScene particlePrefab;
[Export] public PackedScene playerPrefab;
private int players = 1;
[Export] public int maxMass;
[Export] public int totalMass;
[Export] public int maxTinyMass = 1000;
[Export] public int totalTinyMass;
public int maxAiPlayers = 5;
public int aiPlayers;
public int AI_ID;
public int i = 0;
[Export] public Vector2 PlayArea;
public Player authorityPlayer;
private RandomNumberGenerator random = Global.Instance.Random;
public int spawnRandomParticle(int minSize, int maxSize, bool tiny)
{
var particle = particlePrefab.Instantiate<Particle>();
var spawnPos = randomSpawnPos();
// TODO place particles outside of player range
// TODO smarter location finding
particle.GlobalPosition = spawnPos;
particle.Name = "particle_" + i++;
particle.tiny = tiny;
particle.RandomInit( random.RandiRange(minSize, maxSize));
AddChild(particle);
return particle.size;
}
private Vector2 randomSpawnPos()
{
var random = this.random;
var spawnPos = new Vector2(
random.RandfRange(-PlayArea.X / 2, PlayArea.X / 2),
random.RandfRange(-PlayArea.Y / 2, PlayArea.Y / 2)
// random.RandfRange(50f, 50f)
);
return spawnPos;
}
public void spawnToMaxMass()
{
if (totalMass < maxMass)
{
// var massContributors = GetTree().GetNodesInGroup("MassContributor");
// var players = massContributors.Where(mc => mc is Player);
// var minSize = players.Count() == 0 ? 10 : Mathf.Max(1, players.Min(p => ((Player)p).PlayerSize) - 10);
totalMass += spawnRandomParticle(20, 500, false);
}
if (totalTinyMass < maxTinyMass)
{
totalTinyMass += spawnRandomParticle(3, 20, true);
}
if (maxAiPlayers > aiPlayers)
{
SpawnAI();
}
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
if (authorityPlayer != null)
{
var cam = GetNode<Camera2D>("Camera2D");
cam.Position = cam.Position.Lerp(authorityPlayer.Position, 0.1f);
const float stepSize = 200;
const float minStep = 10;
var scaledSize = Math.Max(Mathf.Floor(authorityPlayer.PlayerSize / stepSize), minStep);
cam.Zoom = cam.Zoom.Lerp(Vector2.One / (Vector2.One * (float)Math.Log10(scaledSize + 1d)), 0.05f);
}
if (Global.Instance.State is ServerState)
{
spawnToMaxMass();
foreach (var playerInfo in Global.Instance.PlayerManager.Players)
{
if (!playerInfo.alive && playerInfo.ready)
{
SpawnPlayer(playerInfo);
}
if (playerInfo.alive && !playerInfo.ready)
{
var deadPlayer = GetNodeOrNull<Player>(playerInfo.peerId.ToString());
if (deadPlayer != null)
{
GD.Print($"Player {playerInfo.name} is dead and got removed");
deadPlayer.QueueFree();
}
}
}
var playersToRemove = Global.Instance.PlayerManager.PlayersToRemove;
while (playersToRemove.Count > 0)
{
var peerToRemove = playersToRemove.Dequeue();
GD.Print($"Removing Player {peerToRemove}");
var found = GetNodeOrNull(peerToRemove.ToString());
if (found != null)
{
RemoveChild(found);
}
}
}
}
private void SpawnAI()
{
var player = playerPrefab.Instantiate<Player>();
player.Name = "AI" + AI_ID++;
player.DisplayName = NameGenerator.RandomAIName();
var random = this.random;
player.GrowPlayer(random.RandiRange(100, 250));
player.aiControlled = true;
player.UnlockableColorName = UnlockableColors.PickRandomColorName();
aiPlayers++;
AddChild(player);
player.GlobalPosition = randomSpawnPos();
GD.Print($"{Multiplayer.GetUniqueId()}: spawned {player.Name} {player.DisplayName}");
}
private void SpawnPlayer(PlayerManager.PlayerInfo info)
{
GD.Print($"{Multiplayer.GetUniqueId()}: player spawn {info.name} {info.peerId}");
var player = playerPrefab.Instantiate<Player>();
player.Name = info.peerId.ToString();
player.DisplayName = info.name;
player.GrowPlayer();
player.UnlockableColorName = info.selectedColor;
AddChild(player);
var spawnPos = randomSpawnPos();
RpcId(info.peerId, MethodName.initPlayerOnAuthority, info.name, info.peerId, spawnPos, player.PlayerSize, info.selectedColor);
info.alive = true;
}
[Rpc(MultiplayerApi.RpcMode.AnyPeer, CallLocal = true)]
private void initPlayerOnAuthority(string displayName, long id, Vector2 position, int size, string name)
{
var existing = GetNode<Player>(id.ToString());
existing.GlobalPosition = position;
existing.PlayerSize = size;
existing.DisplayName = displayName;
existing.UnlockableColorName = name;
existing.Color = UnlockableColors.Colors[existing.UnlockableColorName];
GD.Print($"{Multiplayer.GetUniqueId()}: Player {displayName}({id}) init size: {existing.PlayerSize} auth {existing.GetMultiplayerAuthority()}");
var sprite2D = existing.GetNode<Sprite2D>("scaled/Sprite2D");
if (existing.Color.Material != null)
{
sprite2D.Material = existing.Color.Material;
}
(sprite2D.Material as ShaderMaterial) ?.SetShaderParameter("bodyColor", existing.Color.Color);
Audio.Instance.Plop(this);
authorityPlayer = existing;
var cam = GetNode<Camera2D>("Camera2D");
cam.Position = authorityPlayer.Position;
DisplayServer.WindowSetTitle($"LD56 - {displayName}");
}
public void SpawnColoredParticles(PackedScene particleSystemScene, Vector2 at, Color color)
{
var particle = particleSystemScene.Instantiate<GpuParticles2D>();
if (particle.ProcessMaterial is ParticleProcessMaterial particleProcessMaterial)
{
particleProcessMaterial.SetColor(color);
particle.GlobalPosition = at;
particle.Emitting = true;
AddChild(particle);
}
}
}