-
Notifications
You must be signed in to change notification settings - Fork 22
/
GridViewAdapter.cls
191 lines (147 loc) · 6.16 KB
/
GridViewAdapter.cls
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
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "GridViewAdapter"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = True
'@PredeclaredId
'@Exposed
'@Folder("Battleship.View")
Option Explicit
Implements IGridViewCommands
Implements IGridViewEvents
Public Enum ViewMode
NewGame
MessageShown
FleetPosition
Player1
Player2
GameOver
End Enum
Public Event OnCreatePlayer(ByVal gridId As PlayGridId, ByVal pt As PlayerType, ByVal difficulty As AIDifficulty)
Public Event OnPreviewCurrentShipPosition(ByVal gridId As PlayGridId, ByVal position As IGridCoord)
Public Event OnRotateCurrentShipPosition(ByVal gridId As PlayGridId, ByVal position As IGridCoord)
Public Event OnConfirmCurrentShipPosition(ByVal gridId As PlayGridId, ByVal position As IGridCoord)
Public Event OnPlayerReady()
Public Event OnAttackPosition(ByVal gridId As PlayGridId, ByVal position As IGridCoord)
Public Event OnHit(ByVal gridId As PlayGridId, ByVal position As IGridCoord, ByVal hitShip As IShip)
Public Event OnMiss(ByVal gridId As PlayGridId, ByVal position As IGridCoord)
Public Event OnGameOver(ByVal winner As IPlayer)
Private Type TAdapter
ShipsToPosition As Byte
GridView As IGridViewCommands
End Type
Private this As TAdapter
Private Sub Class_Initialize()
this.ShipsToPosition = PlayerGrid.ShipsPerGrid
End Sub
Public Function Create(ByVal View As IGridViewCommands) As GridViewAdapter
With New GridViewAdapter
Set .GridView = View
Set View.Events = .Self
Set Create = .Self
End With
End Function
Public Property Get Self() As GridViewAdapter
Set Self = Me
End Property
'@Description("Gets/sets a reference that exposes commands to send to the view.")
Public Property Get GridView() As IGridViewCommands
Attribute GridView.VB_Description = "Gets/sets a reference that exposes commands to send to the view."
Set GridView = this.GridView
End Property
Public Property Set GridView(ByVal value As IGridViewCommands)
Set this.GridView = value
End Property
Private Sub Class_Terminate()
Debug.Print TypeName(Me) & " is terminating"
End Sub
':IGridViewEvents
':Messages sent from the view
':***************************
Private Sub IGridViewEvents_AttackPosition(ByVal gridId As PlayGridId, ByVal position As IGridCoord)
RaiseEvent OnAttackPosition(gridId, position)
End Sub
Private Sub IGridViewEvents_ConfirmShipPosition(ByVal gridId As PlayGridId, ByVal position As IGridCoord)
RaiseEvent OnConfirmCurrentShipPosition(gridId, position)
End Sub
Private Sub IGridViewEvents_CreatePlayer(ByVal gridId As PlayGridId, ByVal pt As PlayerType, ByVal difficulty As AIDifficulty)
RaiseEvent OnCreatePlayer(gridId, pt, difficulty)
End Sub
Private Sub IGridViewEvents_HumanPlayerReady()
RaiseEvent OnPlayerReady
End Sub
Private Sub IGridViewEvents_PreviewRotateShip(ByVal gridId As PlayGridId, ByVal position As IGridCoord)
RaiseEvent OnRotateCurrentShipPosition(gridId, position)
End Sub
Private Sub IGridViewEvents_PreviewShipPosition(ByVal gridId As PlayGridId, ByVal position As IGridCoord)
RaiseEvent OnPreviewCurrentShipPosition(gridId, position)
End Sub
':IGridViewCommands
':Messages sent from the controller
':*********************************
Private Property Set IGridViewCommands_Events(ByVal value As IGridViewEvents)
Err.Raise 5, TypeName(Me), "Invalid use of property"
End Property
Private Property Get IGridViewCommands_Events() As IGridViewEvents
Set IGridViewCommands_Events = Me
End Property
Private Sub IGridViewCommands_OnBeginAttack(ByVal currentPlayerGridId As PlayGridId)
this.GridView.OnBeginAttack currentPlayerGridId
End Sub
Private Sub IGridViewCommands_OnBeginShipPosition(ByVal CurrentShip As IShip, ByVal player As IPlayer)
this.GridView.OnLockGrid IIf(player.PlayGrid.gridId = 1, 2, 1)
this.GridView.OnBeginShipPosition CurrentShip, player
End Sub
Private Sub IGridViewCommands_OnConfirmShipPosition(ByVal player As IPlayer, ByVal newShip As IShip)
If player.PlayerType = ComputerControlled Then Exit Sub
this.GridView.OnConfirmShipPosition player, newShip
this.ShipsToPosition = this.ShipsToPosition - 1
End Sub
Private Sub IGridViewCommands_OnGameOver(ByVal winningGrid As PlayGridId)
this.GridView.OnGameOver winningGrid
Set this.GridView.Events = Nothing
End Sub
Private Sub IGridViewCommands_OnHit(ByVal gridId As PlayGridId)
this.GridView.OnHit gridId
End Sub
Private Sub IGridViewCommands_OnInvalidShipPosition()
this.GridView.OnInvalidShipPosition
End Sub
Private Sub IGridViewCommands_OnKnownPositionAttack()
this.GridView.OnKnownPositionAttack
End Sub
Private Sub IGridViewCommands_OnLockGrid(ByVal gridId As PlayGridId)
this.GridView.OnLockGrid gridId
End Sub
Private Sub IGridViewCommands_OnMiss(ByVal gridId As PlayGridId)
this.GridView.OnMiss gridId
End Sub
Private Sub IGridViewCommands_OnNewGame()
this.GridView.OnNewGame
End Sub
Private Sub IGridViewCommands_OnPreviewShipPosition(ByVal player As IPlayer, ByVal newShip As IShip)
If player.PlayerType = ComputerControlled Then Exit Sub
this.GridView.OnPreviewShipPosition player, newShip
End Sub
Private Sub IGridViewCommands_OnRefreshGrid(ByVal grid As PlayerGrid)
this.GridView.OnRefreshGrid grid
End Sub
Private Sub IGridViewCommands_OnSelectPosition(ByVal gridId As PlayGridId, ByVal position As IGridCoord)
this.GridView.OnSelectPosition gridId, position
End Sub
Private Sub IGridViewCommands_OnSink(ByVal gridId As PlayGridId)
this.GridView.OnSink gridId
End Sub
Private Sub IGridViewCommands_OnUpdateFleetStatus(ByVal player As IPlayer, ByVal hitShip As IShip, Optional ByVal showAIStatus As Boolean = False)
this.GridView.OnUpdateFleetStatus player, hitShip, showAIStatus
End Sub
Private Sub IGridViewCommands_OnBeginWaitForComputerPlayer()
this.GridView.OnBeginWaitForComputerPlayer
End Sub
Private Sub IGridViewCommands_OnEndWaitForComputerPlayer()
this.GridView.OnEndWaitForComputerPlayer
End Sub