only update the display on a value change

This commit is contained in:
Nico 2021-03-18 10:17:19 +00:00
parent 86f256ee45
commit 9a15a2d5ba
1 changed files with 23 additions and 8 deletions

31
main.fs
View File

@ -7,19 +7,33 @@
: clear-line esc[ ." K" ;
: move-right ( x -- ) esc[ 0 .R ." C" ;
\ some of these values (member cost, device count) have two cells.
\ the first cell stores the value, the second cell stores if the value has changed.
\ this is useful as it means we only redraw the display if the relevant value has changed.
\ the word "changed" sets one of these values as being changed.
: changed ( addr -- ) 1 cells + -1 swap ! ;
\ the word "cleared" sets one of these values as having been used, clearing the change flag.
: cleared ( addr -- ) 1 cells + 0 swap ! ;
\ the word changed? leaves a -1 if the value has changed since last clear, 0 otherwise.
: changed? ( addr -- flag ) 1 cells + @ ;
\ initialise some variables that encode game state.
\ the variable "counter" holds the count of the current tick. It always starts at 0.
variable counter
\ the variable "devices" holds the amount of devices currently in the pool.
variable devices
\ the variable "devices" holds the amount of devices currently in the pool. It has a change flag. It begins with the change flag set, to draw the screen the first time.
create devices 0 , -1 ,
\ the variable "devicerate" is the amount of ticks between devices getting added.
variable devicerate
2000 devicerate !
\ the variable "membercost" is the cost of hiring a new chat member. If it is zero, hiring chat members hasn't been unlocked yet.
variable membercost
\ the variable "membercost" is the cost of hiring a new chat member. If it is zero, hiring chat members hasn't been unlocked yet. It has a change flag.
create membercost 0 , 0 ,
\ the variable "membercount" is how many chat members you've hired. For each chat member, device gain delay decreases.
variable membercount
\ any-change? leaves a -1 on the stack if any value has changed, 0 otherwise
: any-change? ( -- flag ) membercost changed? devices changed? or ;
\ clear-all clears the change flag on all values.
: clear-all membercost cleared devices cleared ;
\ the word !0= tests if something is non-zero
: !0= 0= 0= ;
@ -39,7 +53,7 @@ variable membercount
\ the word game-tick runs a single tick of the game
: game-tick tick-up unlock counter @
devicerate @ mod 0= if 1 devices +! then ;
devicerate @ mod 0= if 1 devices +! devices changed then ;
\ TODO saving
\ the word exit-game exits the game.
@ -55,6 +69,7 @@ variable membercount
devicerate @ 500 - devicerate ! \ increase the device rate by 500
1 membercount +! \ add a member to the count
membercount @ 2 * 5 + membercost +! \ increase the cost of buying a new member
membercost changed
then ;
@ -66,7 +81,6 @@ variable membercount
endcase then ;
\ print-rate prints the current device output rate on the screen.
\ TODO make this be devices/s
: print-rate
2 set-precision
\ first convert to floating point, divide by 1000 to get seconds from milliseconds, then do 1/n to get the devices/second from seconds/device
@ -88,8 +102,9 @@ variable membercount
." (h)ire a chat member [" . ." Devices] (you have " membercount @ . ." chat members helping you)" cr
else drop then
1 move-right ." (q)uit" ;
: draw-screen draw-devices draw-actions ;
\ draw-screen redraws the screen, but only if there's been a change.
: draw-screen any-change? if draw-devices draw-actions clear-all then ;
\ main game loop
: game-loop begin game-tick handle-input draw-screen 0 until ;