-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathProject2D.ahk
351 lines (297 loc) · 9.67 KB
/
Project2D.ahk
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
; Script Information ===========================================================
; Name: Project 2D
; Description: Example of a tile-based game engine using AutoHotkey
; AHK Version: AHK_L 1.1.25.01 (Unicode 32-bit) - March 5, 2017
; OS Version: Windows 2000+
; Language: English - United States (en-US)
; Author: Weston Campbell <westoncampbell@gmail.com>
; Website: https://autohotkey.com/boards/viewtopic.php?f=6&t=7348
; ==============================================================================
; Revision History =============================================================
; Revision 2 (March 13, 2017)
; * Complete rewrite
; ------------------------------------------------------------------------------
; Revision 1 (June 08, 2016)
; * Initial Release
; ==============================================================================
; Auto-Execute =================================================================
#SingleInstance, Force ; Allow only one running instance of script
#Persistent ; Keep the script permanently running until terminated
#NoEnv ; Avoid checking empty variables for environment variables
#NoTrayIcon ; Disable the tray icon of the script
#Include include\displayOut.ahk
SendMode, Input ; The method for sending keystrokes and mouse clicks
SetWorkingDir, %A_ScriptDir% ; Set the working directory of the script
SetBatchLines, -1 ; The speed at which the lines of the script are executed
SetWinDelay, -1 ; The delay to occur after modifying a window
SetControlDelay, -1 ; The delay to occur after modifying a control
OnExit("OnUnload") ; Run a subroutine or function when exiting the script
SetTimer,Redraw,16
return ; End automatic execution
; ==============================================================================
; Labels =======================================================================
;GuiEscape:
GuiClose:
ExitSub:
ExitApp ; Terminate the script unconditionally
return
; ==============================================================================
; Functions ====================================================================
OnLoad() {
Global ; Assume-global mode
Static Init := OnLoad() ; Call function
OnMessage(0x0100, "WM_KEYDOWN")
}
OnUnload(ExitReason, ExitCode) {
Global ; Assume-global mode
}
WM_KEYDOWN(wParam, lParam, Msg, Hwnd) {
Global ; Assume-global mode
Static VK_UP := 26, VK_LEFT := 25, VK_DOWN := 28, VK_RIGHT := 27,
VK_KEY_W := 57, VK_KEY_A := 41, VK_KEY_S := 53, VK_KEY_D := 44,
VK_F1 := 70, VK_RETURN := "D", VK_ESCAPE := "1B"
;If (lParam & 0x40000000) {
; return ; Disable auto-repeat
;}
VK := Format("{:x}", wParam)
If (VK = VK_UP) || (VK = VK_KEY_W) {
Hero.Move(0, -1, true)
}
If (VK = VK_LEFT) || (VK = VK_KEY_A) {
Hero.Move(-1, 0, true)
}
If (VK = VK_DOWN) || (VK = VK_KEY_S) {
Hero.Move(0, 1, true)
}
If (VK = VK_RIGHT) || (VK = VK_KEY_D) {
Hero.Move(1, 0, true)
}
If (VK = VK_RETURN) {
GridAction("VK_RETURN")
}
If (VK = VK_F1) {
GuiControlGet, DebugVisible, Visible, Debug
GuiControl, % (!DebugVisible ? "Show" : "Hide"), Debug
}
If (VK = VK_ESCAPE) {
GoSub, ExitSub
}
}
GuiCreate() {
Global ; Assume-global mode
Static Init := GuiCreate() ; Call function
Menu, Tray, Icon, resources\images\icon.ico
Gui, +LastFound -Resize +HWNDhProject2D
Gui, Margin, 10, 10
Gui, Add, Edit, x0 y0 w0 h0 0x800 ; Focus
;Gui, Add, Text, x10 y10 w620 r3 cFFFFFF vDebug BackgroundTrans
;GuiControl, Hide, Debug
Gui, Show, w640 h640, Project 2D
displayOut := new display( hProject2D, [ 20, 20 ] )
Stage := new Stage( displayOut )
Stage.Color("000000")
Stage.Background("bg_title.png")
Stage.Map("map_0000.map")
Hero := new Player( displayOut )
Hero.Move(-1, -1, false)
}
GridAction(Action := "") {
Global ; Assume-global mode
If (GridType = 1) {
Hero.Move(PX, PY, false)
return
}
If (Map = "map_0000.map") {
If (Action = "VK_RETURN") {
Stage.Background("bg_0001.png")
Stage.Map("map_0001.map")
Hero.Move(9, 11, false)
return
}
}
If (Map = "map_0001.map") {
If (GridType = 2) { ; Go to next map
Stage.Background("bg_0002.png")
Stage.Map("map_0002.map")
Hero.Move(PX, 19, false)
return
}
}
If (Map = "map_0002.map") {
If (GridType = 2) { ; Go to previous map
Stage.Background("bg_0001.png")
Stage.Map("map_0001.map")
Hero.Move(PX, 0, false)
return
}
If (GridType = 3) { ; Go to next map
Stage.Background("bg_0003.png")
Stage.Map("map_0003.map")
Hero.Move(PX, 19, false)
return
}
}
If (Map = "map_0003.map") {
If (GridType = 2) { ; Go to previous map
Stage.Background("bg_0002.png")
Stage.Map("map_0002.map")
Hero.Move(PX, 0, false)
return
}
If (GridType = 3) { ; Fell in hole
Hero.Move(10, 19, false)
return
}
If (GridType = 4) { ; Go to next map
Stage.Background("bg_0004.png")
Stage.Map("map_0004.map")
Hero.Move(16, 15, false)
return
}
}
If (Map = "map_0004.map") {
If (GridType = 2) { ; Go to previous map
Stage.Background("bg_0003.png")
Stage.Map("map_0003.map")
Hero.Move(8, 10, false)
return
}
If (GridType = 3) { ; AutoHotkey "A" icon
Stage.Background("bg_0004_2.png")
Stage.Map("map_0004_2.map")
return
}
}
If (Map = "map_0004_2.map") {
If (GridType = 2) { ; Go to next map
Stage.Background("bg_0005.png")
Stage.Map("map_0005.map")
Hero.Move(19, PY, false)
return
}
}
If (Map = "map_0005.map") {
If (GridType = 2) { ; Go to previous map
Stage.Background("bg_0004_2.png")
Stage.Map("map_0004_2.map")
Hero.Move(0, PY, false)
return
}
If (GridType = 3) { ; Key item
Stage.Background("bg_0005_2.png")
Stage.Map("map_0005_2.map")
return
}
}
If (Map = "map_0005_2.map") {
If (GridType = 2) { ; AutoHotkey "H" icon
Stage.Background("bg_0005_3.png")
Stage.Map("map_0005_3.map")
return
}
}
If (Map = "map_0005_3.map") {
If (GridType = 2) { ; Go to next map
Stage.Background("bg_0006.png")
Stage.Map("map_0006.map")
Hero.Move(PX, 0, false)
return
}
}
If (Map = "map_0006.map") {
If (GridType = 2) { ; Go to previous map
Stage.Background("bg_0005_3.png")
Stage.Map("map_0005_3.map")
Hero.Move(PX, 19, false)
return
}
If (GridType = 3) { ; Key item
Stage.Background("bg_0006_2.png")
Stage.Map("map_0006_2.map")
return
}
}
If (Map = "map_0006_2.map") {
If (GridType = 2) { ; AutoHotkey "K" icon
Stage.Background("bg_0006_3.png")
Stage.Map("map_0006_3.map")
return
}
}
If (Map = "map_0006_3.map") {
If (GridType = 2) { ; Go to next map
Stage.Background("bg_0007.png")
Stage.Map("map_0007.map")
Hero.Move(9, 13, false)
return
}
}
If (Map = "map_0007.map") {
If (GridType = 2) { ; AutoHotkey icon
Stage.Background("bg_0008.png")
Stage.Map("map_0008.map")
Hero.Move(-1, -1, false)
return
}
}
}
Class Stage {
__New( displayOut ) {
size := displayOut.getFieldSize()
this.Stage := displayOut.addPicture( "resources\images\bg_title.png", [ size.1/2 + 0.5, size.2/2 + 0.5 ,1 ], [ size.1, size.2 ] )
this.Stage.setVisible( 1 )
}
Background(File) {
this.Stage.setFile( "resources\images\" . File )
}
Map(File) {
Global ; Assume-global mode
GridMap := []
FileRead, MapData, % "resources\maps\" (Map := File)
For Each, Line In StrSplit(MapData, "`n", "`r") {
GridMap.Push(StrSplit(Line))
}
}
}
Class Player {
__New( displayOut ) {
this.Player := displayOut.addPicture( "resources\images\player_up.png", [ 1, 1, 2 ], [ 0.9, 0.9 ] )
this.Visible()
}
Visible(Show := true) {
this.Player.setVisible( Show )
}
Image(File) {
this.Player.setFile( "resources\images\" . File )
}
Move(X, Y, Relative := 0) {
Global ; Assume-global mode
pos := this.Player.getPosition()
PX := pos.1 - 1
PY := pos.2 - 1
X := (Relative ? PX + X : X), Y := (Relative ? PY + Y : Y)
GridType := GridMap[Y + 2, X + 2]
GuiControl,, Debug, %X%, %Y%`n%Background%`n%Map% ; Debug info
if Relative
{
If (Y < PY) {
this.Image("player_up.png")
}
If (X < PX) {
this.Image("player_left.png")
}
If (Y > PY) {
this.Image("player_down.png")
}
If (X > PX) {
this.Image("player_right.png")
}
}
this.Player.setPosition( [ X + 1, Y + 1, 2 ] )
GridAction("Move")
}
}
; ==============================================================================
Redraw:
displayOut.draw()
return