T3RRYT3RR0R

Collision Detection Engine

Mar 20th, 2021 (edited)
1,915
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 10.32 KB | None | 0 0
  1. ::: AUTHOR     : T3RRY Created : 20/03/2021 Version : 1.0.0
  2. ::: PURPOSE    : Demonstrate how to implement a system of Character movement against a background
  3. :::               with collision detection against specific background characters
  4. ::: LIMITATION : Windows 10
  5. =================================================================================================
  6. :::              DEMONSTRATION SCRIPT:
  7. =================================================================================================
  8. @Echo off
  9.  
  10. :# Execute from directory script located in
  11.  CD /D "%~dp0"
  12.  
  13.  If "!" == "" (Echo(Delayed Expansion Not Permitted prior to macro definiton& Pause & Exit /b 1)
  14.  
  15.  Ver | findstr /LC:"Version 10." > nul || (Echo(Your Version of Windows does not Support Virtual Terminal Sequences)
  16.  
  17. :# Ensure Virtual Terminal sequences Enabled for VT codes. Effective after restart.
  18. :# No test exists to determine if VT support is already enabled.
  19.  
  20.  (reg add HKCU\Console /f /v VirtualTerminalLevel /t REG_DWORD /d 1 ) > Nul || (
  21.   Echo(Virtual Terminal codes Required by %~n0 not enabled on your system.
  22.   Pause
  23.   Exit
  24.  )
  25.  
  26. :# Define Escape Control Character for Virtual Terminal Code usage
  27.  
  28.  For /F %%a in ('Echo prompt $E^|cmd')Do Set "\E=%%a"
  29.  
  30.  CLS & Mode 100,30
  31.  
  32. =================:# MODIFICATION OF MACRO'S NOT RECOMMENDED [ Closed System with Dependencies ]
  33. :# Movement Macros. MODIFICATIONS SHOULD ONLY BE ATTEMPTED BY ADVANCED BATCH SCRIPTERS
  34. :#
  35. :#                  Dependencies:
  36. :#                  - Variable:        Sprite
  37. :#                  - Variable:        Collision.Characters
  38. :#                  - Array Variables: LineINTEGER
  39. :#
  40. :# Mode of operation:
  41. :#
  42. :# Tests Sprite cells at new Y/X Position along axis of intended travel against list of
  43. :# collision characters in the Defined LineY using substring modification to Determine if a move at cell Y;X
  44. :# is to be considered Out Of Bounds due to collision.
  45. :# On Collision Sets 'OOB' variable Flag as 1 [True] and Returns the Character collided with in Var: Collide.Type
  46. :# At the end of Move_Loop ; Sprite Definition is replaced with New cell values only when OOB flag is not set.
  47.  
  48. :# Components common to Move Direction macro's defined to For.Cells and Test.CollisionAXIS
  49.  
  50.  Set "For.Cells=Set "OOB="&(Set "nSprite="& For %%G in (!Sprite!)Do For /F "Tokens=1,2 Delims=;" %%Y in ("%%~G")Do For /F "Delims=" %%v in"
  51.  
  52. :# Seperate Y and X Test Collision macros required as Meta variable usage during collision testing differs for Y and X axis.
  53. :# Vertical Movement Macros output the lineY definition prior to movement to clear previous sprite cells from Display
  54. :# before sprite cell Y values are updated; as the loop uses current Sprite Y values to clear previous cell locations.
  55. :# To account for string substition being 1 indexed ; X axis offsets %%Z+0 [Right] and %%Z-2 [Left] are used.
  56. :# %%Z-2 [Left] also accounts for cmd.exe Y;X cell coordinates being 1 indexed.
  57.  
  58.  Set "Test.CollisionX=For %%K in (!Collision.Characters!)Do if "!Line%%Y:~%%v,1!" == "%%~K" (Set "OOB=1"& Set "Collide.Type=%%~K")) & Set "nSprite=!nSprite! "%%Y;!nX!"")"
  59.  Set "Test.CollisionY=For %%K in (!Collision.Characters!)Do if "!Line%%v:~%%c,1!" == "%%~K" (Set "OOB=1"& Set "Collide.Type=%%~K")) & Set "nSprite=!nSprite! "!nY!;%%Z"" & <Nul Set /P "=%\E%[%%Y;1H!Line%%Y!")"
  60.  
  61. :# Directional Macros - Right Left Up Down
  62.  
  63.  Set "Right=%For.Cells% ('Set /A %%Z+0')Do ( Set /A "nX=%%Z+1"&( %Test.CollisionX% )"
  64.  Set "Left= %For.Cells% ('set /A %%Z-2')Do ( Set /A "nX=%%Z-1"&( %Test.CollisionX% )"
  65.  Set "Up=   %For.Cells% ('set /A %%Y-1')Do For /F "Delims=" %%c in ('set /A %%Z-1')Do (Set /A "nY=%%Y-1"&( %Test.CollisionY% )"
  66.  Set "Down= %For.Cells% ('set /A %%Y+1')Do For /F "Delims=" %%c in ('set /A %%Z-1')Do (Set /A "nY=%%Y+1"&( %Test.CollisionY% )"
  67.  
  68. :# Undefine shared Macro's after incorperation in the Directional Macros
  69.  
  70.  Set "For.Cells="
  71.  Set "Test.CollisionX="
  72.  Set "Test.CollisionY="
  73.  
  74. =================================================================================================
  75.  
  76.  Setlocal EnableExtensions EnableDelayedExpansion
  77.  
  78. :# Disable Cursor Display for smooth animation
  79.  <nul set /P "=%\E%[?25l%\E%[0m"
  80.  Cls
  81.  
  82. :# Define screen Line# Array [ Example ]
  83. :# Characters in each line are passable unless defined in the Collision.Characters variable
  84. :#
  85.  
  86.  For %%G in (^
  87.                 "Line1=+-----------------------------------------------------------------------------------------+"^
  88.                 "Line2=|                                                                                         |"^
  89.                 "Line3=|                                                                                         |"^
  90.                 "Line4=|    ..                    ,                                                              |"^
  91.                 "Line5=|    ..                     ,                                                             |"^
  92.                 "Line6=|                           ,                    OO  OO                                   |"^
  93.                 "Line7=|                            ,                  O  OOc                                    |"^
  94.                 "Line8=|                                                OO  OO                                   |"^
  95.                 "Line9=|                                                                                         |"^
  96.                "Line10=|                                                                                         |"^
  97.                "Line11=|        .                                                ___                             |"^
  98.                "Line12=|       ...                                                                               |"^
  99.                "Line13=|      .....                                                                              |"^
  100.                "Line14=|     .......                                            [   ]                            |"^
  101.                "Line15=|                                                        [   ]                            |"^
  102.                "Line16=|                           Z                                                             |"^
  103.                "Line17=|                            Z                                                            |"^
  104.                "Line18=|                            rZ                           ___                             |"^
  105.                "Line19=|                           ZZ                                                            |"^
  106.                "Line20=|                                                                                         |"^
  107.                "Line21=|                                                                                         |"^
  108.                "Line22=|                                                                                         |"^
  109.                "Line23=|                                                                                         |"^
  110.                "Line24=|                                                                                         |"^
  111.                "Line25=+-----------------------------------------------------------------------------------------+"^
  112.  ) Do Set %%G
  113.  
  114. :# Output Screen Lines
  115.  
  116.  For /L %%Y in (1 1 25)Do (
  117.   Echo(!Line%%Y!
  118.  )
  119.  
  120. :# Define Collison Characters. On Collision, Character collided with is returned to !Collide.Type!
  121. :# Out of bounds flagged using boundary characters in Collision.Characters list
  122. :# Characters in any Line not defined as a collision character are passable; and are not returned to !Collide.Type!
  123. :# Each Character in the list should be Doublequoted:
  124.  
  125.  Set Collision.Characters="-" "|" "O" "_" "[" "]" "Z" ","
  126.  
  127. :# Specify Fatal Characters by Definition; Each Character Doublequoted in list form as above:
  128.  
  129.  Set Fatal="Z" ","
  130.  
  131. :# Define Sprite cell coordinates
  132. :#           sC1   sC2   sC3   sC4   sC5
  133.  Set Sprite="2;2" "2;4" "3;2" "3;3" "3;4"
  134.  
  135. :# Define Individual Sprite cell properties - VTCOLORmCHARACTER
  136.  
  137.  Set "sC1=31mU"
  138.  Set "sC2=31mU"
  139.  Set "sC3=35m#"
  140.  Set "sC4=36m@"
  141.  Set "sC5=35m#"
  142.  
  143. :# Move_Loop Choice Controller Key Definition
  144. :# Change controller Keys by redifining the below four kDirection or kQuit Values
  145.  
  146.  Set "kLeft=A"
  147.  Set "kRight=D"
  148.  Set "kUp=W"
  149.  Set "kDown=S"
  150.  Set "kQuit=0"
  151.  
  152. :# Populate choice keylist using kDirection values.
  153.  Set "MoveKeys=!kUp!!kDown!!kLeft!!kRight!"
  154.  
  155. :# kDirection variables are used to test inverse inverse of !last! if Locking out movement in opposing direction.
  156.  Set "!kUp!=!kDown!"
  157.  Set "!kDown!=!kUp!"
  158.  Set "!kLeft!=!kRight!"
  159.  Set "!kRight!=!kLeft!"
  160.  
  161.  Title Move: !MoveKeys! Exit: 0
  162.  
  163. :move.loop
  164. :# Reset Collision Flag
  165.  Set "Collide.Type="
  166.  
  167. :# Overwite Lines currently occupied by Sprite Cells with Original Line%%Y definition
  168. :# Redirected to Sprite.dat to append sprite cells in next step
  169.  
  170.  For %%G in (!Sprite!)Do For /F "Tokens=1 Delims=;" %%Y in ("%%~G") Do (
  171.   <Nul Set /P "=%\E%[%%Y;1H!Line%%Y!"
  172.  )
  173.  
  174. :# Output Current sprite cell locations and values.
  175. :# Notifies if Sprite Cell 'sC' undefined [color and character] and exits
  176. :# Output New Cell values
  177.  
  178.  Set cCol=0
  179.  For %%G in (!Sprite!)Do For /F "Tokens=1,2 Delims=;" %%Y in ("%%~G") Do (
  180.   Set /A "cCol+=1"
  181.   For %%# in (!cCol!) Do (
  182.    If "!sC%%#!" == "" (
  183.     CLS
  184.     Echo(Sprite Cell sC%%# Undefined
  185.     Timeout /T 3 /Nobreak
  186.     Endlocal
  187.     Exit /B 1
  188.    )
  189.    <nul Set /P "=%\E%[%%Y;%%ZH%\E%[!sC%%#!%\E%[0m"
  190.   )
  191.  )
  192.  
  193. :# Select Move Axis and enact Relevent Macro
  194. :# Choice command for input may be substuted with other Control Systems.
  195. :# To 'Lock out' Movement in the Opposing Direction currently being travelled:
  196. :#
  197. :# For /F "Delims=" %%C in ('Choice /N /C:!MoveKeys!!kQuit!')Do If not "!%%C!" == "!Last!" (
  198.  
  199.  For /F "Delims=" %%C in ('Choice /N /C:!MoveKeys!!kQuit!')Do (
  200.   Set "Last=%%C"
  201.   If %%C==!kQuit! (
  202.    Echo(%\E%[26;1H%\E%[?25h
  203.    Endlocal & Exit /B
  204.   )
  205.   If %%C==!kUp! (%Up%)
  206.   If %%C==!kLeft! (%Left%)
  207.   If %%C==!kDown! (%Down%)
  208.   If %%C==!kRight! (%Right%)
  209.  )
  210. :# Update Sprite Definiton if the executed Movement macro did not flag move as Out Of Bounds
  211.  
  212.  If "!OOB!" == "" (Set "Sprite=!nSprite!")
  213.  
  214. :# Example of Flagging a fatal collision.
  215.  
  216.  For %%x in (!Fatal!)Do If "!Collide.Type!" == "%%~x" (
  217.   Cls & Echo(Oh No - %%~x is Deadly^^!
  218.   Timeout /T 3 /NoBreak
  219.   Endlocal & Goto :Eof
  220.  )
  221. Goto :move.loop
  222.  
Add Comment
Please, Sign In to add comment