-
Notifications
You must be signed in to change notification settings - Fork 1
/
PlayerSelectTrigger.cs
122 lines (108 loc) · 4.96 KB
/
PlayerSelectTrigger.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
using System;
using System.Collections.Generic;
using Celeste;
using MadelineParty.Board;
using MadelineParty.Multiplayer;
using MadelineParty.Multiplayer.General;
using Microsoft.Xna.Framework;
using Monocle;
namespace MadelineParty
{
public class PlayerSelectTrigger : Trigger, IComparable {
private Level level;
private List<PlayerSelectTrigger> otherChoices = new List<PlayerSelectTrigger>();
public bool occupied;
public int playerID {
private set;
get;
} = 0;
public PlayerSelectTrigger(EntityData data, Vector2 offset)
: base(data, offset) {
AddTag(Tags.PauseUpdate);
AddTag(Tags.FrozenUpdate);
}
public override void OnEnter(Player player) {
base.OnEnter(player);
occupied = true;
GameData.Instance.currentPlayerSelection = this;
MultiplayerSingleton.Instance.Send(new Party { respondingTo = -1, playerSelectTrigger = playerID });
// -1 so it doesn't count me as a player
int left = GameData.Instance.playerNumber - 1;
foreach (KeyValuePair<uint, int> kvp1 in GameData.Instance.playerSelectTriggers) {
// Check if another player is trying to choose the same spot
bool duplicate = false;
foreach (KeyValuePair<uint, int> kvp2 in GameData.Instance.playerSelectTriggers) {
duplicate |= (kvp2.Key != kvp1.Key && kvp2.Value == kvp1.Value);
}
if (!duplicate && kvp1.Value != -1 && kvp1.Value != playerID) {
left--;
}
}
if (left <= 0) {
AllTriggersOccupied();
}
}
private void MultiplayerOccupiedAction(int seed) {
GameData.Instance.players[playerID] = new PlayerData(playerID, MultiplayerSingleton.Instance.CurrentPlayerID());
foreach (KeyValuePair<uint, int> pair in GameData.Instance.playerSelectTriggers) {
if (pair.Value != playerID && pair.Value >= 0) {
GameData.Instance.players[pair.Value] = new PlayerData(pair.Value, pair.Key);
}
}
// Host determines the random seeds for the game
// Seeds are determined in advance to avoid duplicate rolls when it matters
if (GameData.Instance.celesteNetHost) {
MultiplayerSingleton.Instance.Send(new RandomSeed { seed = seed });
}
}
// Only called on the Trigger that the player at this computer has selected
public void AllTriggersOccupied() {
// Store playerID
GameData.Instance.realPlayerID = playerID;
Random rand = new Random();
// If not the host, the random seed will be changed by a recieved communication
var seed = rand.Next();
GameData.Instance.Random = new Random(seed);
BoardController.GenerateTurnOrderRolls();
if (MultiplayerSingleton.Instance.BackendConnected()) {
MultiplayerOccupiedAction(seed);
} else {
GameData.Instance.players[playerID] = new PlayerData(playerID);
}
ModeManager.Instance.AfterPlayerSelect(level);
}
public override void OnLeave(Player player) {
base.OnLeave(player);
if (GameData.Instance.realPlayerID == -1) {
occupied = false;
MultiplayerSingleton.Instance.Send(new Party { respondingTo = -1, playerSelectTrigger = -1 });
GameData.Instance.currentPlayerSelection = null;
}
}
public override void Added(Scene scene) {
base.Added(scene);
level = SceneAs<Level>();
level.CanRetry = false;
otherChoices = scene.Entities.FindAll<PlayerSelectTrigger>();
otherChoices.Sort();
for (int i = 0; i < otherChoices.Count; i++) {
if (otherChoices[i] == this) {
PlayerToken token = new PlayerToken(i, BoardController.TokenPaths[i], ScreenCoordsFromGameCoords(Position, new Vector2(Width, Height) * 3), new(.25f), -900000000, new BoardController.BoardSpace());
level.Add(token);
playerID = i;
}
}
otherChoices.Remove(this);
}
private Vector2 ScreenCoordsFromGameCoords(Vector2 gameCoords) {
return ScreenCoordsFromGameCoords(gameCoords, Vector2.Zero);
}
private Vector2 ScreenCoordsFromGameCoords(Vector2 gameCoords, Vector2 offsetInPxls) {
return new Vector2((-level.LevelOffset.X + gameCoords.X) * 6 + offsetInPxls.X, (-level.LevelOffset.Y + gameCoords.Y) * 6 + offsetInPxls.Y);
}
public int CompareTo(object obj) {
if (obj == null) return 1;
return obj is PlayerSelectTrigger other ? X.CompareTo(other.X) : 1;
}
}
}