'---- ROTATIVE SQUARES DEMO ----


'-- INITIALIZE ROTATIVE SQUARES DEMO --

SUB INIT_DEMO_ROTATIVE_SQUARES

 SPDX=8:SPDY=-8: XS=64:YS=80: DIST_RU=20

 'ONLY WHEN RUNNING A DEMO SECTION
 IF DEMOSEC=1 THEN
  PLAY STOP
  PLAY MODFILE "./ASSETS/excellate_pt2.mod",22050
 ENDIF

 PAGE WRITE 1: CLS
 PAUSETIMER=0: TIMENOW=0: Timer=0

END SUB


'-- ROTATE SQUARED WALL --

SUB ROTATE_SQUARED_WALL(DG,DIST)

 LOCAL X,Y,CNT,CS,SI,XX,YY,T

 'PRECALCULATE COS AND SIN
 CS=COS(RAD(DG))
 SI=SIN(RAD(DG))

 'SQUARES DISTANCE
 T=DIST/2.2

 'KEEP THE ROTATION
 DG=ABS(DG)

 FOR X=-DIST*3 TO DIST*3 STEP DIST
  'RANDOM COLORS
  CNT=RND(DG)*255

  FOR Y=-DIST*3 TO DIST*3 STEP DIST

   'COORDINATES OF CENTER
   XX=CS*X-SI*Y-46+MAXX/2: YY=CS*Y+SI*X-46+MAXY/2
   XX=XX+46:YY=YY+46

   'TO ANIMATE COLORS
   CCNT=CCNT+2
   TT=(255-(CCNT MOD 255))

   IF XX>-DIST AND XX<MAXX+DIST AND YY>-DIST AND YY<MAXY+DIST THEN
    'ROTATE SQUARE
    CA=COS(RAD(DG))*T:     SA=SIN(RAD(DG))*T
    CB=COS(RAD(DG+90))*T:  SB=SIN(RAD(DG+90))*T
    CC=COS(RAD(DG+180))*T: SC=SIN(RAD(DG+180))*T
    CD=COS(RAD(DG+270))*T: SD=SIN(RAD(DG+270))*T
    
    'POLYGON COORDINATES
    XP%(0)=XX-CA-SA: YP%(0)=YY+CA-SA
    XP%(1)=XX-CB-SB: YP%(1)=YY+CB-SB
    XP%(2)=XX-CC-SC: YP%(2)=YY+CC-SC
    XP%(3)=XX-CD-SD: YP%(3)=YY+CD-SD

    'MOVE COLORS
    IF CCNT MOD 16=0 THEN
     POLYGON 4,XP%(),YP%(),RGB(255-TT,ABS(CNT),TT),RGB(255-TT,ABS(CNT),TT)
    ELSE
     POLYGON 4,XP%(),YP%(),RGB(255-TT,ABS(CNT),TT)
    ENDIF

   ENDIF

 NEXT Y,X

END SUB


'-- RUN ROTATIVE SQUARES DEMO (SOFT VERSION) --

SUB RUN_DEMO_ROTATIVE_SQUARES_SOFT

 LOCAL X,Y,SI,XX,YY,XB,YB,DG,DIST,T,I,TT,CNT,CCNT,SEC,DIST_RU,SPDY,ROTX,ROTY,ANI

 'LOAD SPACESHIP
 LOADPLY("ARWING SNES.PLY",0)

 'SET COLOR RANGE OF SPACESHIP
 PLY_RGB(0,2,2,4)
 PLY_ADJUST_COLOR(0,40)

 SEC=0: X=-5: DIST=20
 XX=MAXX/2:YY=MAXY+200: ROTX=0: ROTY=0: ROTZ=0: ANI=0

 'SET PAGE BUFFER
 PAGE WRITE 1

 'RESET TIMER
 PAUSETIMER=0: TIMENOW=0: TIMER=0

 'RUN ANIMATION UNTIL 4.6 SECONDS
 DO WHILE SEC<=4.6

  'CLEAR PAGE BUFFER
  BOX 0,0,MAXX,MAXY,0,,RGB(0,0,0)

  'MOVE WALL HORIZONTALLY
  X=X+1.67*3.75/1.5

  'APPROACHING THE WALL
  DIST=DIST-0.38/2: IF DIST<0.6 THEN DIST=0.6

  'DRAW THE APPROACHING WALL
  FOR T=0 TO MAXX STEP MAXX/DIST
   I=255-DIST*12.5
   TT=5-DIST/5
   LINE MAXX/2+T-X,0,MAXX/2+T-X,MAXY,TT,RGB(I,I,255)
   LINE MAXX/2-T-X,0,MAXX/2-T-X,MAXY,TT,RGB(I,I,255)
   LINE 0,MAXY/2+T-X,MAXX,MAXY/2+T-X,TT,RGB(I,I,255)
   LINE 0,MAXY/2-T-X,MAXX,MAXY/2-T-X,TT,RGB(I,I,255)
  NEXT T

  'SPACESHIP MOVEMENT
  '(DON'T USED SECONDS BECAUSE THIS ROUTINE IS VERY SENSITIVE TO FRAMERATE)  
  IF ANI<40 THEN 
   ANI=ANI+1
   SPDY=COS(RAD(X/1.2))*70-1 : T=-5
  ELSE
   IF SPDY>10 THEN SPDY=SPDY-5
   IF SPDY<5 THEN SPDY=SPDY+5
   ROTX=ROTX+0.2: XX=XX-3: YY=YY-1: T=0
  ENDIF
  YY=YY-SPDY/2

  'IF ANI<20 THEN 
  ' ANI=ANI+1
  ' SPDY=COS(RAD(X/1.1))*70-1 : T=-30
  'ELSE
  ' IF SPDY>0 THEN SPDY=SPDY-2 ELSE SPDY=SPDY+2
  ' ROTX=ROTX+0.2: XX=XX-2: YY=YY-3: T=0
  'ENDIF
  'YY=YY-SPDY/1.2
   
  'DRAW SPACESHIP
  TT=(20-DIST)
  IF DIST>0.6 THEN
   PLY_MOVE(0,XX,YY,(40-TT*2)/11)
   PLY_ROT(0,180+ROTX,165-YY/15+ABS(30-SPDY))
   IF TT<0 THEN TT=0
   CIRCLE XX+2,YY-(10-SPDY/2.1)+10+T,DIST*2+SW,3-(20-DIST)/10,1,RGB(255-TT*5,255-TT*5,0),RGB(255-TT*5,0,0)
  ENDIF

  'MOVE THE SPACESHIP TURBINE
  IF SW=0 THEN SW=DIST/2 ELSE SW=0

  'FIX BORDERS
  BOX 0,0,MAXX,MAXY,1,RGB(0,0,0)

  'SET A SLOW FPS TO NOT CORRUPT THE ANIMATION IN FUTURE FASTER CMMs
  FPS(30)
  SEC=Timer/1000

  'COPY TO MAIN PAGE
  PAGE COPY 1 TO 0,I

 LOOP

END SUB


'-- ROTATE SQUARED TUNNEL --

SUB ROTATE_SQUARED_TUNNEL(DG,DIST_RU,SEC)

 LOCAL CS,SI,T,X,Y,XX,YY,DIST,FSPD,CNT,R,B,EXITING

 DIST=0: FSPD=0: RU=DIST_RU: EXITING=0

 'MAKE "PONG" WITH THE START OF TUNNEL
 XS=XS+SPDX:YS=YS+SPDY
 IF XS<-96 THEN SPDX= (5+RND(1)*9)
 IF XS> 96 THEN SPDX=-(5+RND(1)*9)
 IF YS<-96 THEN SPDY= (5+RND(1)*9)
 IF YS> 96 THEN SPDY=-(5+RND(1)*9)

 CNT=0

 'KEEP THE SQUARES UNTIL DISTANCE OF 500
 DO WHILE DIST<500

  'SPEED AND DISTANCE OF BOXES
  FSPD=FSPD+2

  'FOR THE ENTER EFFECT TUNNEL
  RU=RU-1

  'ANIMATE THE ENDING OF TUNNEL AFTER 26 SECONDS
  IF SEC>26 THEN EXITING=(SEC-26)*25

  CNT=CNT+1

  'TUNNEL DEGREE   
  DG=DG+SIN(RAD(INIT_RAD))*4

  'PRECALCULATE SIN AND COS
  CS=COS(RAD(DG))
  SI=SIN(RAD(DG))

  'SPEED OF DISTANCE   
  DIST=DIST+FSPD   

  IF DIST>0 AND CNT<ABS(RU) AND CNT>EXITING THEN

   'DEFINE DISTANCE
   T=DIST

   'ROTATE WITH PRECALCULATED COS AND SIN
   X=CS*80: Y=SI*80

   'MAIN COORDINATES OF SQUARES
   XX=CS*X-SI*Y+MAXX/2: YY=CS*Y+SI*X+MAXY/2
   XX=XX+XS:YY=YY+YS

   'ROTATE SQUARES
   CA=COS(RAD(DG))*T:     SA=SIN(RAD(DG))*T
   CB=COS(RAD(DG+90))*T:  SB=SIN(RAD(DG+90))*T
   CC=COS(RAD(DG+180))*T: SC=SIN(RAD(DG+180))*T
   CD=COS(RAD(DG+270))*T: SD=SIN(RAD(DG+270))*T

   'POLYGON COORDINATES
   XP%(0)=XX-CA-SA: YP%(0)=YY+CA-SA
   XP%(1)=XX-CB-SB: YP%(1)=YY+CB-SB
   XP%(2)=XX-CC-SC: YP%(2)=YY+CC-SC
   XP%(3)=XX-CD-SD: YP%(3)=YY+CD-SD

   'CHANGE TUNNEL COLOR AFTER 20.3 SECONDS
   IF SEC<20.3 THEN 
    R=1:B=0
   ELSE
    R=0.3:B=1
   ENDIF

   'FAKE TUNNEL MOVEMENT USING COLORS
   SELECT CASE (RU MOD 6)
    CASE 0: POLYGON 4,XP%(),YP%(),RGB(255*R,0,255*B)
    CASE 1: POLYGON 4,XP%(),YP%(),RGB(200*R,0,200*B)
    CASE 2: POLYGON 4,XP%(),YP%(),RGB(150*R,0,150*B)
    CASE 3: POLYGON 4,XP%(),YP%(),RGB(100*R,0,100*B)
    CASE 4: POLYGON 4,XP%(),YP%(),RGB(255,255,255)
    CASE 5: POLYGON 4,XP%(),YP%(),RGB(100*R,0,100*B)
    CASE 6: POLYGON 4,XP%(),YP%(),RGB(150*R,0,150*B)
   END SELECT

  ENDIF

 LOOP
 
END SUB


'-- RUN ROTATIVE SQUARES DEMO (DANCE VERSION) --

SUB RUN_DEMO_ROTATIVE_SQUARES_DANCE

 LOCAL X,Y,SI,XX,YY,XB,YB,DG,DIST,T,I,TT,CNT,CCNT,SEC,DIST_RU

 DG=1: CNT=0: CCNT=0: RP=1: DIST_RU=0: SEC=0: DIST=1000: DIST_DG=200

 'RUN DEMO UNTIL 40 SECONDS
 DO WHILE SEC<27

  'CLEAR PAGE BUFFER
  BOX 0,0,MAXX,MAXY,0,,RGB(0,0,0)
  
  'SHOW ROTATE SQUARED WALL UNTIL 13.5 SECONDS  
  IF SEC<13.5 THEN
   'DEFINE ZOOM EFFECT
   DIST_DG=DIST_DG+10

   'DEFINE ROTATON
   DG=DG+5: IF DG>360 THEN DG=DG-360

   'ANIMATION SEQUENCE
   SELECT CASE SEC
    'APPEARING
    CASE <1: DIST=DIST-30
    'ZOOM ANIMATION AND ROTATION
    CASE 1 TO 12: DIST=160+COS(RAD(DIST_DG))*50
    'APPROACHING
    CASE >=12: DIST=DIST+24: IF DIST>900 THEN DIST=900
   END SELECT
 
   'CHANGE ROTATION SIDE AFTER 6.7 SECONDS 
   IF SEC<6.7 THEN
    ROTATE_SQUARED_WALL(DG,DIST)
   ELSE
    ROTATE_SQUARED_WALL(-DG,DIST)
   ENDIF  

  ELSE
   'SHOW ROTATED SQUARED TUNNEL AFTER 13.5 SECONDS

   'CHANGE INITIAL ROTATON OF THE TUNNEL
   INIT_RAD=INIT_RAD+4
   INIT_DG=COS(RAD(INIT_RAD))*100

   'APPROACH TUNNEL 
   DIST_RU=DIST_RU+1
 
   ROTATE_SQUARED_TUNNEL(INIT_DG,DIST_RU,SEC)

  ENDIF

  'FIX BORDERS
  BOX 0,0,MAXX,MAXY,1,RGB(0,0,0)

  'KEEP 30 FPS
  FPS(30)
  SEC=Timer/1000

  PAGE COPY 1 TO 0,B

 LOOP

END SUB



