source: trunk/GameClient.pas

Last change on this file was 318, checked in by chronos, 6 months ago
  • Modified: Code cleanup.
File size: 6.2 KB
Line 
1unit GameClient;
2
3interface
4
5uses
6 Classes, SysUtils, Game, Forms, Generics.Collections, GameProtocol,
7 GameServer, CommThread, Threading, CommFrame, Player;
8
9type
10 TClientConnectType = (ctLocal, ctNetwork);
11
12 { TClient }
13
14 TClient = class
15 private
16 FActive: Boolean;
17 FForm: TForm;
18 FGame: TGame;
19 FControlPlayer: TPlayer;
20 FOnChange: TNotifyEvent;
21 FOnDestroy: TNotifyEvent;
22 FOnGameEnd: TNotifyEvent;
23 FOnNextPlayer: TNotifyEvent;
24 FOnReceive: TCommandEvent;
25 FOnMove: TMoveEvent;
26 FOnTurnStart: TNotifyEvent;
27 CommThread: TCommThread;
28 CommFrame: TCommFrame;
29 procedure SetActive(AValue: Boolean);
30 procedure SetControlPlayer(AValue: TPlayer);
31 procedure SetForm(AValue: TForm);
32 procedure PlayerMove(CellFrom, CellTo: TPlayerCell; var CountOnce, CountRepeat: Integer;
33 Update: Boolean; var Confirm: Boolean);
34 procedure SetOnMove(AValue: TMoveEvent);
35 protected
36 procedure SetGame(AValue: TGame); virtual;
37 procedure ReceiveCmd(Command: TCommand; DataOut, DataIn: TStream); virtual;
38 procedure DoTurnStartHandler(Sender: TObject); virtual;
39 procedure DoTurnStart; virtual;
40 procedure DoGameEndHandler(Sender: TObject);
41 procedure DoGameEnd;
42 procedure DoNextPlayerHandler(Sender: TObject);
43 procedure DoNextPlayer;
44 procedure DoParametersChangedHandler(Sender: TObject);
45 public
46 Name: string;
47 LocalServer: TServer;
48 RemoteAddress: string;
49 RemotePort: Word;
50 ConnectType: TClientConnectType;
51 Protocol: TGameProtocolClient;
52 procedure DoChange;
53 constructor Create; virtual;
54 destructor Destroy; override;
55 property ControlPlayer: TPlayer read FControlPlayer write SetControlPlayer;
56 property Game: TGame read FGame write SetGame;
57 property Form: TForm read FForm write SetForm;
58 property Active: Boolean read FActive write SetActive;
59 property OnDestroy: TNotifyEvent read FOnDestroy write FOnDestroy;
60 property OnMove: TMoveEvent read FOnMove write SetOnMove;
61 property OnReceive: TCommandEvent read FOnReceive write FOnReceive;
62 property OnChange: TNotifyEvent read FOnChange write FOnChange;
63 property OnTurnStart: TNotifyEvent read FOnTurnStart write FOnTurnStart;
64 property OnGameEnd: TNotifyEvent read FOnGameEnd write FOnGameEnd;
65 property OnNextPlayer: TNotifyEvent read FOnNextPlayer write FOnNextPlayer;
66 end;
67
68 { TClients }
69
70 TClients = class(TObjectList<TClient>)
71 Game: TGame;
72 function New(Name: string): TClient;
73 function SearchPlayer(Player: TPlayer): TClient;
74 constructor Create(FreeObjects: Boolean = True);
75 end;
76
77
78implementation
79
80uses
81 ClientGUI;
82
83{ TClients }
84
85function TClients.New(Name: string): TClient;
86begin
87 Result := TClientGUI.Create;
88 Result.Game := Game;
89 Result.Name := Name;
90 Add(Result);
91end;
92
93function TClients.SearchPlayer(Player: TPlayer): TClient;
94var
95 Client: TClient;
96begin
97 Result := nil;
98 for Client in Self do
99 if Client.ControlPlayer = Player then begin
100 Result := Client;
101 Break;
102 end;
103end;
104
105constructor TClients.Create(FreeObjects: Boolean = True);
106begin
107 Game := nil;
108 inherited;
109end;
110
111{ TClient }
112
113procedure TClient.SetGame(AValue: TGame);
114begin
115 if FGame = AValue then Exit;
116 FGame := AValue;
117end;
118
119procedure TClient.DoChange;
120begin
121 if Assigned(FOnChange) then
122 FOnChange(Self);
123end;
124
125procedure TClient.PlayerMove(CellFrom, CellTo: TPlayerCell; var CountOnce,
126 CountRepeat: Integer; Update: Boolean; var Confirm: Boolean);
127begin
128 if Assigned(FOnMove) then
129 FOnMove(CellFrom, CellTo, CountOnce, CountRepeat, Update, Confirm);
130end;
131
132procedure TClient.SetOnMove(AValue: TMoveEvent);
133begin
134 //if FOnMove = AValue then Exit;
135 FOnMove := AValue;
136end;
137
138procedure TClient.ReceiveCmd(Command: TCommand; DataOut, DataIn: TStream);
139begin
140end;
141
142procedure TClient.DoTurnStartHandler(Sender: TObject);
143begin
144 Synchronize(DoTurnStart);
145end;
146
147procedure TClient.DoTurnStart;
148begin
149 if Assigned(FOnTurnStart) then
150 FOnTurnStart(Self);
151end;
152
153procedure TClient.DoGameEndHandler(Sender: TObject);
154begin
155 Synchronize(DoGameEnd);
156end;
157
158procedure TClient.DoGameEnd;
159begin
160 if Assigned(FOnGameEnd) then FOnGameEnd(Self);
161end;
162
163procedure TClient.DoNextPlayerHandler(Sender: TObject);
164begin
165 Synchronize(DoNextPlayer);
166end;
167
168procedure TClient.DoNextPlayer;
169begin
170 if Assigned(FOnNextPlayer) then FOnNextPlayer(Self);
171end;
172
173procedure TClient.DoParametersChangedHandler(Sender: TObject);
174begin
175 Synchronize(DoChange);
176end;
177
178procedure TClient.SetControlPlayer(AValue: TPlayer);
179begin
180 if FControlPlayer = AValue then Exit;
181 if Assigned(FControlPlayer) then
182 FControlPlayer.OnMove := nil;
183 FControlPlayer := AValue;
184 if Assigned(FControlPlayer) then begin
185 FControlPlayer.OnMove := PlayerMove;
186 end;
187end;
188
189procedure TClient.SetActive(AValue: Boolean);
190var
191 ServerClient: TServerClient;
192begin
193 if FActive = AValue then Exit;
194 if FActive then begin
195 case ConnectType of
196 ctLocal: begin
197 CommThread.Active := False;
198 CommThread.Ext.Disconnect;
199 CommThread.Pin.Disconnect;
200 end;
201 end;
202 end;
203 FActive := AValue;
204 if FActive then begin
205 case ConnectType of
206 ctLocal: if LocalServer.Active then begin
207 CommFrame.FrameDataPin.Connect(Protocol.Pin);
208 CommFrame.RawDataPin.Connect(CommThread.Pin);
209 CommThread.Active := True;
210 CommFrame.Active := True;
211 ServerClient := LocalServer.GetNewServerClient;
212 ServerClient.Player := ControlPlayer;
213 ServerClient.CommFrame.RawDataPin.Connect(CommThread.Ext);
214 end else raise Exception.Create('Local server is not active');
215 //ctNetwork: ;
216 end;
217 end else begin
218 Protocol.Pin.Disconnect;
219 end;
220end;
221
222procedure TClient.SetForm(AValue: TForm);
223begin
224 if FForm = AValue then Exit;
225 FForm := AValue;
226end;
227
228constructor TClient.Create;
229begin
230 CommThread := TCommThread.Create(nil);
231 CommFrame := TCommFrame.Create(nil);
232 FControlPlayer := nil;
233 Protocol := TGameProtocolClient.Create;
234 Protocol.OnTurnStart := DoTurnStartHandler;
235 Protocol.OnGameEnd := DoGameEndHandler;
236 Protocol.OnNextPlayer := DoNextPlayerHandler;
237 Protocol.OnParametersChanged := DoParametersChangedHandler;
238end;
239
240destructor TClient.Destroy;
241begin
242 Form := nil;
243 ControlPlayer := nil;
244 FreeAndNil(Protocol);
245 FreeAndNil(CommThread);
246 FreeAndNil(CommFrame);
247 inherited;
248end;
249
250end.
251
Note: See TracBrowser for help on using the repository browser.