This repository was archived by the owner on Aug 18, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclientGame.ml
More file actions
157 lines (141 loc) · 5.03 KB
/
clientGame.ml
File metadata and controls
157 lines (141 loc) · 5.03 KB
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
open OcsfmlWindow
open OcsfmlGraphics
open OcsfmlNetwork
open Camera2d
open MorpionDef
open Morpion
open Common
open HostClientCommon
let morpion_width = 500.
let morpion_pos = 150.0,50.0
let main_one_player_client app (socket : #tcp_socket) =
let morpion = creer_grille () in
let camera2D = camera2D app (float app#get_width) (float app#get_height) in
let ck = new my_clock in
let time_before_exit = new Jauge.jauge ~tmax:playing_time ~position:(50.,50.) ~bg_color:Color.blue (300., 50.) in
socket#set_blocking false ;
let sh = new shader ~fragment:(ShaderSource.file "grey.fs") () in
let process_quit_event b evt =
let open Event in
match evt with
| Closed -> app#close ; Some Exit
| KeyPressed { code = KeyCode.Escape ; _ } -> Some Exit
| _ -> None
in
let process_camera_event b evt =
let open Event in
match evt with
| KeyPressed { code = KeyCode.Left ; _ } -> camera2D#activate_horizontal (-1) ; Some b
| KeyPressed { code = KeyCode.Right ; _ } -> camera2D#activate_horizontal 1 ; Some b
| KeyPressed { code = KeyCode.Up ; _ } -> camera2D#activate_vertical (-1) ; Some b
| KeyPressed { code = KeyCode.Down ; _ } -> camera2D#activate_vertical 1 ; Some b
| KeyReleased { code = KeyCode.Left ; _ } -> camera2D#activate_horizontal 0 ; Some b
| KeyReleased { code = KeyCode.Right ; _ } -> camera2D#activate_horizontal 0 ; Some b
| KeyReleased { code = KeyCode.Up ; _ } -> camera2D#activate_vertical 0 ; Some b
| KeyReleased { code = KeyCode.Down ; _ } -> camera2D#activate_vertical 0 ; Some b
| MouseWheelMoved ( k , { x ; y } ) -> camera2D#scroll k x y ; Some b
| _ -> None
in
let process_enter_pause_event b evt =
let open Event in
match evt with
| KeyPressed { code = KeyCode.Space ; _ } ->
ignore (send `EnterPause socket) ;
ck#pause ;
Some Pause
| _ -> None
in
let process_quit_pause_event b evt =
let open Event in
match evt with
| KeyPressed { code = KeyCode.Space ; _ } ->
ignore (send `ResumePause socket) ;
ck#resume ;
Some Playing
| _ -> None
in
let process_playing_event b evt =
let open Event in
match evt with
| MouseButtonPressed (_, {x ; y}) ->
let pos = app#convert_coords (x,y) in
let path = get_position_coup_vide morpion_pos morpion_width morpion pos in
begin match path with
| None -> ()
| Some l -> ignore (send (`Played l) socket) ;
modify_at_path (l, morpion) (fun _ -> creer_joueur_tmp Cercle)
end ;
Some b
| _ -> None
in
let display () =
app#clear ~color:Color.white () ;
draw app 0 morpion_pos morpion_width morpion ;
camera2D#disable ;
app#draw time_before_exit ;
camera2D#enable ;
app#display
in
let process_remote_messages state =
match receive socket with
| Some (`AddGrid chemin) ->
modify_at_path (chemin, morpion) (fun _ -> creer_grille ()) ;
ignore ck#restart ; state
| Some (`AddNormal (chemin_croix, chemin_cercle)) ->
(match chemin_croix with
| Some l1 -> modify_at_path (l1, morpion) (fun _ -> creer_joueur Croix)
| None -> ()) ;
(match chemin_cercle with
| Some l2 -> modify_at_path (l2, morpion) (fun _ -> creer_joueur Cercle)
| None -> ()) ;
ignore ck#restart ; state
| Some `EnterPause -> ck#pause ; Pause
| Some `ResumePause -> ck#resume ; Playing
| Some `Quit -> Exit
| None -> state
in
let display_pause () =
app#clear ~color:(Color.rgb 178 178 178) () ;
draw app ~shader:sh 0 morpion_pos morpion_width morpion ;
camera2D#disable ;
app#draw ~shader:sh time_before_exit ;
app#draw (new rectangle_shape ~size:(100.,100.) ~position:(100.,100.) ~fill_color:Color.red ~outline_color:Color.black ~outline_thickness:2.0 ()) ;
camera2D#enable ;
app#display
in
let display_finished j =
display ()
in
let rec main_loop state =
let state = process_remote_messages state in
match state with
| Playing ->
if ck#get_time >= playing_time
then (ignore (send `EndTimer socket) ; ignore ck#restart) ;
let state = while_opt (fun () -> app#poll_event)
(process_event [
process_quit_event ;
process_camera_event ;
process_playing_event ;
process_enter_pause_event])
Playing in
time_before_exit#update ck#get_time ;
camera2D#update ;
display () ;
if app#is_open then main_loop state
else send `Quit socket
| Pause ->
let state = while_opt (fun () -> app#poll_event) (process_event [process_quit_event ; process_quit_pause_event]) Pause in
display_pause () ;
if app#is_open then main_loop state
else send `Quit socket
| Finished j ->
let state = while_opt (fun () -> app#poll_event) (process_event [process_quit_event]) (Finished j) in
display_finished j ;
if app#is_open then main_loop state
else send `Quit socket
| Exit -> send `Quit socket
in
ignore (main_loop Playing) ;
camera2D#disable ;
Some (EntryPoint)