aboutsummaryrefslogtreecommitdiffstats
path: root/autoload/space_vlaze/game.vim
blob: 9915a26b07bcf169205fafa398b4e38bdba74b32 (plain)
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
function! space_vlaze#game#Init()
	let s:loop = 1
	let s:start_time = localtime()
	let s:ticks = 1
	call space_vlaze#game#SetupWindow()
	call space_vlaze#colors#Initialize()
	call space_vlaze#game#InitializeBoard()
	call space_vlaze#mappings#Initialize()
	call space_vlaze#score#Initialize()
	call space_vlaze#life#Initialize()
	
	while s:loop ==# 1
		sleep 50ms
		call space_vlaze#mappings#Listen()
		call space_vlaze#enemy#AddEnemiesToBoard()
		call space_vlaze#game#RenderBoard()
		call space_vlaze#score#RenderScore()
		call space_vlaze#life#RenderLives()
		let s:ticks += 1
	endwhile
endfunction


function! space_vlaze#game#SetupWindow()
	setlocal buftype=nofile
	setlocal bufhidden=wipe
	setlocal noswapfile
	setlocal nonumber
	setlocal nolist
	setlocal nowrap
	setlocal nocursorline
	setlocal nocursorcolumn
	setlocal nolazyredraw
endfunction


function! space_vlaze#game#InitializeBoard()
	call space_vlaze#game#SetupBoard()
	call space_vlaze#game#RenderBoard()
endfunction


function! space_vlaze#game#SetupBoard()
	let s:board = []
	let s:BOARD_HEIGHT = 20
	let s:BOARD_WIDTH = 80
	call space_vlaze#player#SetPlayerX(s:BOARD_WIDTH / 2 - 1)
	call space_vlaze#player#SetPlayerY(s:BOARD_HEIGHT / 2)
	
	" Create 20-row by 80-column board initialised with spaces
	let i = 0
	while i < s:BOARD_HEIGHT
		let s:board = add(s:board, [])
		
		let j = 0
		while j < s:BOARD_WIDTH
			let s:board[i] = add(s:board[i], ' ')
			
			let j += 1
		endwhile
		
		let i += 1
	endwhile
	
	" Initialise player to the middle of the board
	let s:board[space_vlaze#player#PlayerY()][space_vlaze#player#PlayerX()] = space_vlaze#player#PlayerCharacter()
endfunction


function! space_vlaze#game#RenderBoard()
	let i = 1
	while i <= s:BOARD_HEIGHT
		call setline(i, join(s:board[i - 1], ''))
		
		let i += 1
	endwhile
	
	redraw!
endfunction


function! space_vlaze#game#Board()
	return s:board
endfunction


function! space_vlaze#game#BoardCell(y, x)
	return s:board[a:y][a:x]
endfunction


function! space_vlaze#game#SetBoardCell(y, x, value)
	if space_vlaze#game#IsWithinBoard(a:y, a:x)
		let s:board[a:y][a:x] = a:value
	endif
endfunction


function! space_vlaze#game#ClearBoardCell(y, x)
	call space_vlaze#game#SetBoardCell(a:y, a:x, ' ')
endfunction


function! space_vlaze#game#IsBoardCellEmpty(y, x)
	if space_vlaze#game#IsWithinBoard(a:y, a:x)
		return s:board[a:y][a:x] ==# ' '
	endif
endfunction


function! space_vlaze#game#IsWithinBoard(y, x)
	return a:y >=# 0 && a:y <# s:BOARD_HEIGHT && a:x >=# 0 && a:x <# s:BOARD_WIDTH
endfunction


function! space_vlaze#game#BoardHeight()
	return s:BOARD_HEIGHT
endfunction


function! space_vlaze#game#BoardWidth()
	return s:BOARD_WIDTH
endfunction


function! space_vlaze#game#Ticks()
	return s:ticks
endfunction


function! space_vlaze#game#Quit()
	let s:loop = -1
endfunction


function! space_vlaze#game#Pause()
endfunction


function! space_vlaze#game#GameOver()
	call space_vlaze#game#Quit()
	
	let text = [
		\ ['G', ' ', 'A', ' ', 'M', ' ', 'E'],
		\ ['O', ' ', 'V', ' ', 'E', ' ', 'R'],
	\ ]
	
	" Put "Game Over" text in the middle of the board
	let text_y = s:BOARD_HEIGHT / 2
	let text_x = s:BOARD_WIDTH / 2 - 4
	let text_x_orig = text_x
	
	for line in text
		for character in line
			call space_vlaze#game#SetBoardCell(text_y, text_x, character)
			
			let text_x += 1
		endfor
		
		let text_y += 1
		let text_x = text_x_orig
	endfor
	
	call space_vlaze#game#RenderBoard()
	call space_vlaze#leaderboard#SaveHighScore()
endfunction