#include "AFlib3_ASCii.bi"
#include "AFlib3_Miscellaneous.bi"
#include "AFlib3_Constants.bi"



Dim as Single Spr1_x, Spr1_y, Spr2_x, Spr2_y, Spr2ctrl_x, Spr2ctrl_y, Spr2_Movement
Dim as Integer GFX_h, GFX_v
Dim as String CollideDet, CollideDet_BB

GFX_h = (800 / 8)
GFX_v = (600 / 8)
AFlibIII_SetTextGFX GFX_h, GFX_v, , 2, 0

SetMouse , , 0

ScreenSet 1, 0
AFlibIII_ASCii_ClearDisplay RGB(255, 0, 255)
Randomize Timer

AFlibIII_Initialize_ASCii

AFlibIII_ASCii_PrepareSpriteTrans

AFlibIII_ASCii_Box "O", 0, 0, 19, 19, RGB(255, 255, 255), RGB(80, 80, 255), 9
AFlibIII_ASCii_FilledBox Chr$(219), 5, 5, 14, 14, RGB(0, 96, 0), RGB(255, 0, 255), 9
AFlibIII_ASCii_Line Chr$(177), 0, 0, 19, 19, RGB(255, 0, 0), RGB(255, 0, 255), 9
AFlibIII_ASCii_Line Chr$(177), 0, 19, 19, 0, RGB(0, 0, 255), RGB(255, 0, 255), 9

AFlibIII_ASCii_CaptureSprite_text 0, 0, 19, 19, 1

AFlibIII_ASCii_PrepareSpriteTrans

AFlibIII_ASCii_Globe Chr$(219), 25, 25, 1, 1, 25, _
                     RGB(0, 64, 128), RGB(255, 255, 128), _
                     RGB(255, 0, 255), 25, 9

AFlibIII_ASCii_CaptureSprite_text 0, 0, 50, 50, 2
AFlibIII_ASCii_CaptureSprite_BMP "AF3_logo80x60.bmp", 3

Do
  AFlibIII_ASCii_ClearDisplay RGB(0, 0, 32)

  AFlibIII_ASCii_SprBlit_alpha 0, 12, 48, 3

  If MultiKey(&h4B) then Spr1_x -= .8
  If MultiKey(&h4D) then Spr1_x += .8
  If MultiKey(&h48) then Spr1_y -= .8
  If MultiKey(&h50) then Spr1_y += .8
  If MultiKey(&h01) then Exit Do
  If MultiKey(&h02) then Spr2_Movement = 1
  If MultiKey(&h03) then Spr2_Movement = 0

  If Spr2_Movement = 1 then
    If Spr2ctrl_x = 0 then
      Spr2_x += 1
      If Spr2_x > (GFX_h - 20) then Spr2ctrl_x = 1
    ElseIf Spr2ctrl_x = 1 then
      Spr2_x -= 1
      If Spr2_x < -20 then Spr2ctrl_x = 0
    End if

    If Spr2ctrl_y = 0 then
      Spr2_y += 1
      If Spr2_y > (GFX_v - 20) then Spr2ctrl_y = 1
    ElseIf Spr2ctrl_y = 1 then
      Spr2_y -= 1
      If Spr2_y < -20 then Spr2ctrl_y = 0
    End if
  End if

  AFlibIII_ASCii_SprBlit Spr1_x, Spr1_y, 1
  
  AFlibIII_ASCii_SprBlit Spr2_x, Spr2_y, 2

  ' Now comes the fun part: a pixel-perfect sprite collision detection called
  ' "AFlibIII_ASCii_SprCollide", which simply checks for collisions between
  ' two (2) selected ASCii-based collision sprites in AFlib III!!  Awesome
  ' stuff, eh?  d=^-^=b
  '-------------------------------------------------------------------------- 
  If AFlibIII_ASCii_SprCollide_BB(Spr1_x, Spr1_y, 1, Spr2_x, Spr2_y, 2) then
    CollideDet_BB = "TRUE"
    AFlibIII_ASCii_Box_alpha chr(32), Spr2_x, Spr2_y, Spr2_x + 50, Spr2_y + 50, RGB(255, 255, 255), RGB(255, 255, 255), 32, 7
  else
    CollideDet_BB = "FALSE"
  End if

  If AFlibIII_ASCii_SprCollide(Spr1_x, Spr1_y, 1, Spr2_x, Spr2_y, 2) then
    CollideDet = "TRUE"
    AFlibIII_ASCii_SprBlit_firefly Spr2_x, Spr2_y, 128, 2
  else
    CollideDet = "FALSE"
  End if
  
  '-------------------------------------------------------------------------- 
  
  AFlibIII_ASCii_FilledBox chr(32), 0, 0, GFX_h, 0, RGB(0, 0, 0), RGB(0, 0, 255), 17
  AFlibIII_ASCii_Print "ASCii-based PIXEL-PERFECT COLLISION DETECTION on AFlib III!!!", 5, 0, RGB(255, 255, 0), RGB(255, 0, 255), 4
  AFlibIII_ASCii_FilledBox_alpha chr(32), 0, 1, GFX_h, 2, RGB(0, 0, 0), RGB(0, 0, 0), 168, 17
  AFlibIII_ASCii_FilledBox_alpha chr(32), 0, GFX_v, GFX_h, GFX_v - 3, RGB(0, 0, 0), RGB(0, 0, 0), 168, 17
  AFlibIII_ASCii_Print "Bounding Box collision: " + CollideDet_BB, 7, 1, RGB(255, 255, 255), RGB(255, 0, 255), 17
  AFlibIII_ASCii_Print "Pixel-Perfect collision: " + CollideDet, 7, 2, RGB(255, 255, 255), RGB(255, 0, 255), 17

  AFlibIII_ASCii_Print "Arrow keys - controls Sprite #1.", 7, GFX_v - 3, RGB(255, 255, 255), RGB(255, 0, 255), 17
  AFlibIII_ASCii_Print "[1] key - starts movement of Sprite #2.", 7, GFX_v - 2, RGB(255, 255, 255), RGB(255, 0, 255), 17
  AFlibIII_ASCii_Print "[2] key - stops movement of Sprite #2.", 7, GFX_v - 1, RGB(255, 255, 255), RGB(255, 0, 255), 17
  ScreenCopy
  AFlibIII_CustomSync fps_OPTIMAL
Loop until AFlibIII_Detect_CloseWindow



AFlibIII_ASCii_RemoveSprite_CleanSweep
AFlibIII_DeInit_ASCii

AFlibIII_EndProgram

