diff options
| author | Teddy Wing | 2015-10-07 04:48:22 -0400 | 
|---|---|---|
| committer | Teddy Wing | 2015-10-07 04:48:22 -0400 | 
| commit | 3f8fc7ff1758fc9276d163026212d1cbb08d9bbf (patch) | |
| tree | a266493a92c44574b721e9cd54743daef7dcb276 /autoload | |
| parent | bb54411c76a7765af8a06d15ee748251c2a1dcc4 (diff) | |
| download | vim-space-vlaze-3f8fc7ff1758fc9276d163026212d1cbb08d9bbf.tar.bz2 | |
Decrement player lives on collision with an enemy
Additionally extract `IsEnemyAtCell` function that determines whether an
enemy is located at a given cell.
Diffstat (limited to 'autoload')
| -rw-r--r-- | autoload/space_vlaze/enemy.vim | 34 | ||||
| -rw-r--r-- | autoload/space_vlaze/player.vim | 4 | 
2 files changed, 30 insertions, 8 deletions
| diff --git a/autoload/space_vlaze/enemy.vim b/autoload/space_vlaze/enemy.vim index 9b49231..cbe03f3 100644 --- a/autoload/space_vlaze/enemy.vim +++ b/autoload/space_vlaze/enemy.vim @@ -58,17 +58,35 @@ function! space_vlaze#enemy#DropEnemyAtRandomCoordinates()  endfunction +function! space_vlaze#enemy#IsEnemyAtCell(y, x) +	if space_vlaze#game#IsWithinBoard(a:y, a:x) +		return space_vlaze#game#BoardCell(a:y, a:x) ==# space_vlaze#enemy#EnemyCharacter() +	endif +endfunction + +  " Calling this function tells us that coordinates y, x were hit by a missile.  " If an enemy lives at y, x, it needs to disappear and update the score.  function! space_vlaze#enemy#HandleEnemyHitAt(y, x) -	if space_vlaze#game#IsWithinBoard(a:y, a:x) -		if space_vlaze#game#BoardCell(a:y, a:x) ==# space_vlaze#enemy#EnemyCharacter() -			call space_vlaze#game#ClearBoardCell(a:y, a:x) -			call space_vlaze#score#IncrementScore( -				\ s:ENEMY_POINT_BASE * space_vlaze#enemy#PointMultiplier()) -			 -			return 1 -		endif +	if space_vlaze#enemy#IsEnemyAtCell(a:y, a:x) +		call space_vlaze#game#ClearBoardCell(a:y, a:x) +		call space_vlaze#score#IncrementScore( +			\ s:ENEMY_POINT_BASE * space_vlaze#enemy#PointMultiplier()) +		 +		return 1 +	endif +endfunction + + +" Determines whether the player has been hit by an enemy. If so decrements a +" life from player. +"  +" Should be used on player and enemy movement. +function! space_vlaze#enemy#HandlePlayerCollision() +	if space_vlaze#enemy#IsEnemyAtCell( +		\ space_vlaze#player#PlayerY(), +		\ space_vlaze#player#PlayerX()) +		call space_vlaze#life#DecrementLives()  	endif  endfunction diff --git a/autoload/space_vlaze/player.vim b/autoload/space_vlaze/player.vim index 969e9d3..dce2f04 100644 --- a/autoload/space_vlaze/player.vim +++ b/autoload/space_vlaze/player.vim @@ -31,6 +31,7 @@ endfunction  function! space_vlaze#player#MoveLeft()  	call space_vlaze#player#ClearPlayerCell()  	call space_vlaze#player#SetPlayerX(space_vlaze#player#PlayerX() - 1) +	call space_vlaze#enemy#HandlePlayerCollision()  	call space_vlaze#game#SetBoardCell(space_vlaze#player#PlayerY(), space_vlaze#player#PlayerX(), space_vlaze#player#PlayerCharacter())  endfunction @@ -38,6 +39,7 @@ endfunction  function! space_vlaze#player#MoveDown()  	call space_vlaze#player#ClearPlayerCell()  	call space_vlaze#player#SetPlayerY(space_vlaze#player#PlayerY() + 1) +	call space_vlaze#enemy#HandlePlayerCollision()  	call space_vlaze#game#SetBoardCell(space_vlaze#player#PlayerY(), space_vlaze#player#PlayerX(), space_vlaze#player#PlayerCharacter())  endfunction @@ -45,6 +47,7 @@ endfunction  function! space_vlaze#player#MoveUp()  	call space_vlaze#player#ClearPlayerCell()  	call space_vlaze#player#SetPlayerY(space_vlaze#player#PlayerY() - 1) +	call space_vlaze#enemy#HandlePlayerCollision()  	call space_vlaze#game#SetBoardCell(space_vlaze#player#PlayerY(), space_vlaze#player#PlayerX(), space_vlaze#player#PlayerCharacter())  endfunction @@ -52,6 +55,7 @@ endfunction  function! space_vlaze#player#MoveRight()  	call space_vlaze#player#ClearPlayerCell()  	call space_vlaze#player#SetPlayerX(space_vlaze#player#PlayerX() + 1) +	call space_vlaze#enemy#HandlePlayerCollision()  	call space_vlaze#game#SetBoardCell(space_vlaze#player#PlayerY(), space_vlaze#player#PlayerX(), space_vlaze#player#PlayerCharacter())  endfunction | 
