-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainViewModel.cs
163 lines (136 loc) · 4.15 KB
/
MainViewModel.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
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Threading;
using MultiplayerTest.Packets;
using softaware.ViewPort.Commands;
using softaware.ViewPort.Core;
namespace MultiplayerTest
{
public class MainViewModel : NotifyPropertyChanged
{
private static readonly IReadOnlyList<string> playerColors = new[]
{
"#FF0000",
"#00FF00",
"#0000FF",
"#FFD800"
};
private string endpoint = "127.0.0.1";
private int port = 15000;
private Server server;
private Client client;
public MainViewModel(bool isServer)
{
IsServer = isServer;
StartServerCommand = new Command(StartServer);
StartGameCommand = new Command(StartGame);
ConnectCommand = new Command(Connect);
Players = new ObservableCollection<Player>();
}
public bool IsServer { get; }
public bool IsClient => !IsServer;
public string Endpoint
{
get => endpoint;
set => SetProperty(ref endpoint, value);
}
public int Port
{
get => port;
set => SetProperty(ref port, value);
}
public ObservableCollection<Player> Players { get; }
public Command StartServerCommand { get; }
public Command StartGameCommand { get; }
public Command ConnectCommand { get; }
public void SendMoveOrder(double x, double y)
{
if (IsServer)
{
server.SendOrder(x, y);
}
else
{
client.SendOrder(x, y);
}
}
private void StartServer()
{
server = new Server();
server.PlayerConnected += id =>
{
App.Current.Dispatcher.Invoke(() =>
{
AddPlayer(id);
});
};
server.OrderReceived += OrderReceived;
server.Start(Port);
}
private void StartGame()
{
AddPlayer(Players.Count);
server.StartGame();
StartGameLoop();
}
private void Connect()
{
client = new Client();
client.Connect(Endpoint, Port);
client.OrderReceived += OrderReceived;
client.GameStarted += players =>
{
App.Current.Dispatcher.Invoke(() =>
{
for (int i = 0; i < players; i++)
{
AddPlayer(i);
}
});
StartGameLoop();
};
}
private void StartGameLoop()
{
new Thread(GameLoop)
{
IsBackground = true
}.Start();
}
private void GameLoop()
{
const double Speed = 100;
var frameDuration = TimeSpan.FromSeconds(1 / 60.0);
while (true)
{
foreach (var player in Players)
{
var toTarget = (x: player.TargetX - player.X, y: player.TargetY - player.Y);
var length = Math.Sqrt(toTarget.x * toTarget.x + toTarget.y * toTarget.y);
if (length > 1)
{
player.X += toTarget.x / length * Speed * frameDuration.TotalSeconds;
player.Y += toTarget.y / length * Speed * frameDuration.TotalSeconds;
}
}
Thread.Sleep(frameDuration);
}
}
private void AddPlayer(int id)
{
Players.Add(new Player()
{
Color = playerColors[id],
X = id * 100,
TargetX = id * 100
});
}
private void OrderReceived(Order order)
{
var player = this.Players[order.Player];
player.TargetX = order.TargetX;
player.TargetY = order.TargetY;
}
}
}