Created
January 18, 2020 11:23
-
-
Save lucatronica/b0972d23fd88ac12eb52bfa67220c451 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--The state of the vine is represented by x, y and a. | |
--x,y is the center of the base of the vine. | |
--a is the angle the vine is facing. It will always be a multiple of 1/4. | |
--(PICO-8 angles are 0..1) | |
--Each part of the vine is 10x10 pixels. | |
x=0 | |
y=60 | |
a=0 | |
--We only need to clear the screen once. | |
cls(7) | |
--Alias to save chars. | |
f=circfill | |
--Begin render loop (this is just a goto label). | |
::_:: | |
--Save chars. We no longer need the current value of a. | |
c=cos(a) | |
s=sin(a) | |
--Shift a by -1/4 or +1/4. You can consider this line the expansion of: | |
-- (flr(rnd(2))*2-1)/4 | |
a+=flr(rnd(2))/2-1/4 | |
--Save chars. | |
n=cos(a) | |
m=sin(a) | |
--clip() sets a rectangular clipping region. We use it to draw a single | |
--quadrant of a circle. | |
--clip() uses x,y,w,h so we need the top left position. First find the center | |
--of the current cell with (cos(a)*5, sin(a)*5), then subtract 5 from each. | |
clip(x+c*5-5,y+s*5-5,10,10) | |
--u,v is the center of the circle. It should be in a corner of the cell. | |
--We can find it by moving 5 units in the new direction. | |
--We need to shift u,v to make sute the center sits *inside* the current cell, | |
--rather than one pixel adjacent to it. | |
--First we use 4.5 instead of 5: | |
-- flr(5 + 5) == 10 ❌ | |
-- flr(5 + 4.5) == 9 ⭕ | |
-- flr(5 - 4.5) == 0 ⭕ | |
--Next we account for when x,y is outside of the cell: | |
-- a==1/4 -> y is below cell -> sin(a) == -1 -> dy = min(s,0) == min(s) | |
-- a==2/4 -> x is right of cell -> cos(a) == -1 -> dx = min(c,0) == min(c) | |
--We can omit the 0 from min() since the paramters will default to 0. | |
u=x+n*4.5+min(c) | |
v=y+m*4.5+min(s) | |
--Now we can draw the circles! | |
--1. is the outer outline. | |
--2. is the highlight. "a and b or c" is Lua's version of the ternary operator. | |
-- I just guessed the "s-1==m" condition, don't know why it works lol. | |
--3. is the green fill. | |
--4. is the inner outline. | |
f(u,v,8,1) | |
f(u,v,7,11) | |
f(u,v,1,1) | |
circ(u,v,7,s-1==m and 3 or 10) | |
--To get to the new position, move 5 units in the old direction (to the center | |
--of the cell) and then 5 units in the new direction. | |
x=(x+n*5+c*5)%140 | |
y=(y+m*5+s*5)%140 | |
--Swap the buffer and jump to the start of the loop. | |
flip()goto _ | |
--🦝 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment