From 69587e1338a2c54596b0c3bbeb414479368c6b7a Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sun, 4 Oct 2015 14:16:14 -0400 Subject: Add player movement (WIP) Kind of add player movement. TODO: Update player coordinates when moving * Add a main game loop to `space_vlaze#game#Init` * Fill in player directional movement functions * game.vim: Add functions to access and set `s:` variables so these can be accessed from player.vim * Add new mappings based on https://github.com/mattn/invader-vim/blob/f04e7f5e8ac42ae87db6050e64449055206c4054/plugin/invader.vim#L44-L60 because it turns out that the mappings we previously defined don't work when out main loop is running. --- autoload/space_vlaze/game.vim | 29 +++++++++++++++++++++++++++++ autoload/space_vlaze/mappings.vim | 21 +++++++++++++++++++++ autoload/space_vlaze/player.vim | 14 +++++++++++++- 3 files changed, 63 insertions(+), 1 deletion(-) (limited to 'autoload') diff --git a/autoload/space_vlaze/game.vim b/autoload/space_vlaze/game.vim index e975bd6..0f822d1 100644 --- a/autoload/space_vlaze/game.vim +++ b/autoload/space_vlaze/game.vim @@ -1,10 +1,18 @@ function! space_vlaze#game#Init() + let s:loop = 1 let s:score = 0 let s:start_time = localtime() call space_vlaze#game#SetupWindow() call space_vlaze#colors#Initialize() call space_vlaze#game#InitializeBoard() call space_vlaze#mappings#Initialize() + + while s:loop ==# 1 + sleep 50ms + call space_vlaze#mappings#Listen() + call space_vlaze#game#RenderBoard() + redraw! + endwhile endfunction @@ -56,7 +64,28 @@ function! space_vlaze#game#RenderBoard() endfunction +function! space_vlaze#game#Board() + return s:board +endfunction + + +function! space_vlaze#game#SetBoardCell(y, x, value) + let s:board[a:y][a:x] = a:value +endfunction + + +function! space_vlaze#game#PlayerY() + return s:PLAYER_Y +endfunction + +function! space_vlaze#game#PlayerX() + return s:PLAYER_X +endfunction + + + function! space_vlaze#game#Quit() + let s:loop = -1 endfunction diff --git a/autoload/space_vlaze/mappings.vim b/autoload/space_vlaze/mappings.vim index 65cfe6e..9194eb0 100644 --- a/autoload/space_vlaze/mappings.vim +++ b/autoload/space_vlaze/mappings.vim @@ -7,3 +7,24 @@ function! space_vlaze#mappings#Initialize() nnoremap l :call space_vlaze#player#MoveRight() nnoremap :call space_vlaze#player#FireBlasters() endfunction + + +function! space_vlaze#mappings#Listen() + let c = nr2char(getchar(0)) + + if c ==# 'q' + call space_vlaze#game#Quit() + elseif c ==# 'p' + call space_vlaze#game#Pause() + elseif c ==# 'h' + call space_vlaze#player#MoveLeft() + elseif c ==# 'j' + call space_vlaze#player#MoveDown() + elseif c ==# 'k' + call space_vlaze#player#MoveUp() + elseif c ==# 'l' + call space_vlaze#player#MoveRight() + elseif c ==# '\' + call space_vlaze#player#FireBlasters() + endif +endfunction diff --git a/autoload/space_vlaze/player.vim b/autoload/space_vlaze/player.vim index 17b0178..f3340db 100644 --- a/autoload/space_vlaze/player.vim +++ b/autoload/space_vlaze/player.vim @@ -3,20 +3,32 @@ function! space_vlaze#player#PlayerCharacter() endfunction +function! space_vlaze#player#ClearPlayerCell() + call space_vlaze#game#SetBoardCell(space_vlaze#game#PlayerY(), space_vlaze#game#PlayerX(), ' ') +endfunction + + function! space_vlaze#player#MoveLeft() + call space_vlaze#player#ClearPlayerCell() + call space_vlaze#game#SetBoardCell(space_vlaze#game#PlayerY(), space_vlaze#game#PlayerX() - 1, space_vlaze#player#PlayerCharacter()) endfunction function! space_vlaze#player#MoveDown() - echom 'booya' + call space_vlaze#player#ClearPlayerCell() + call space_vlaze#game#SetBoardCell(space_vlaze#game#PlayerY() + 1, space_vlaze#game#PlayerX(), space_vlaze#player#PlayerCharacter()) endfunction function! space_vlaze#player#MoveUp() + call space_vlaze#player#ClearPlayerCell() + call space_vlaze#game#SetBoardCell(space_vlaze#game#PlayerY() - 1, space_vlaze#game#PlayerX(), space_vlaze#player#PlayerCharacter()) endfunction function! space_vlaze#player#MoveRight() + call space_vlaze#player#ClearPlayerCell() + call space_vlaze#game#SetBoardCell(space_vlaze#game#PlayerY(), space_vlaze#game#PlayerX() + 1, space_vlaze#player#PlayerCharacter()) endfunction -- cgit v1.2.3