aboutsummaryrefslogtreecommitdiffstats
path: root/autoload/space_vlaze/missile.vim
blob: 1d8d0c3458070b7620874e140c28dca18396fa52 (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
" Fires all (4) missiles around an origin corrdinate (should be the player)
function! space_vlaze#missile#FireAll(y, x)
	let missiles_firing = 1
	let i = 1
	while missiles_firing
		let missiles_firing = missiles_firing && space_vlaze#missile#Move(a:y, a:x + i, 'right')
		let missiles_firing = missiles_firing && space_vlaze#missile#Move(a:y, a:x - i, 'left')
		let missiles_firing = missiles_firing && space_vlaze#missile#Move(a:y - i, a:x, 'top')
		let missiles_firing = missiles_firing && space_vlaze#missile#Move(a:y + i, a:x, 'bottom')
		
		sleep 20ms
		
		let i += 1
	endwhile
endfunction


" Move a missile starting from a set of coordinates in a specified direction.
" Clears the missile's previous location.
" 
" y: Previous Y coordinate of missile
" x: Previous X coordinate of missile
" direction: 'left', 'right', 'bottom', or 'top'
" returns: 1 if the missile was able to move, 0 if it hit a wall or object
function! space_vlaze#missile#Move(y, x, direction)
	let y = a:y
	let x = a:x
	
	if a:direction ==# 'left'
		let x -= 1
	elseif a:direction ==# 'right'
		let x += 1
	elseif a:direction ==# 'bottom'
		let y += 1
	elseif a:direction ==# 'top'
		let y -= 1
	endif
	
	call space_vlaze#missile#ClearMissile(a:y, a:x)
	
	if space_vlaze#game#IsBoardCellEmpty(y, x)
		call space_vlaze#game#SetBoardCell(
			\ y,
			\ x,
			\ space_vlaze#missile#MissileCharacter(a:direction))
		
		call space_vlaze#game#RenderBoard()
		
		return 1
	endif
	
	return 0
endfunction


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


function! space_vlaze#missile#MissileCharacter(direction)
	let horizontal = '—'
	let vertical = '|'
	
	if a:direction ==# 'left'
		return horizontal
	elseif a:direction ==# 'right'
		return horizontal
	elseif a:direction ==# 'bottom'
		return vertical
	elseif a:direction ==# 'top'
		return vertical
	endif
endfunction