Advertisement
NeatNit

Untitled

Jan 6th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ' SBPE - SmileBASIC Platforming Engine
  2. ' ====================================
  3.  
  4. ' Copyright © Nitai Sasson
  5. ' All rights reserved
  6.  
  7. ' DO NOT UPLOAD THIS FILE
  8. ' OR ANY DERIVATIVES THEREOF
  9. ' TO SMILEBOOM SERVERS WITHOUT
  10. ' MY EXPLICIT PERMISSION
  11.  
  12.  
  13.  
  14. ' For proper functionality of this library, you must NEVER:
  15. ' 1. Use SPOFS or SPCOLVEC on an active sprite
  16. ' 2. Use sprites from different screens - it works on either the lower screen or the top screen, not both.
  17.  
  18.  
  19. ' Most functions exist 4 times:
  20. ' 1. ___FUNC (has an extra _) - Internal function, not to be used by library users.
  21. ' 2. __FUNC - Regular function, the one you should use in most cases. Uses integer mode, which means coordinates are identical to
  22. ' 3. __FUNC% - Same as __FUNC, but explicitly uses integer mode. In practice it is in every way identical to __FUNC.
  23. ' 4. __FUNC# - Same as __FUNC and __FUNC#, but accepts (or returns) floating-point values. This converts to/from the integers used in the other functions, based on the sub-pixel resolution set to __RES%. It might be most convenient for beginners to use while setting the resolution to something useful like 4. If you know you might tweak the resolution later in development, use this version, so later you won't need to re-adjust every single value.
  24.  
  25.  
  26.  
  27.  
  28.  
  29. OPTION STRICT
  30.  
  31. GOSUB @INIT
  32. 'TERMINATE < just for notepad++ to not bug out
  33. END
  34.  
  35. @INIT
  36.     ' Back-and-forth system between internal indexing of sprites and the actual management numbers:
  37.     DIM __TOINDEX%[512], __TOMNG%[0], __EMPTYINDEX%[0]
  38.    
  39.     ' vaiables for each sprite:
  40.     DIM __X%[0], __Y%[0], __VX%[0], __VY%[0], __SPMODE%[0]
  41.    
  42.     FILL __TOINDEX%, -1    ' Start with no sprites defined
  43.     'FILL __SPMODE%, -1    ' Start with no sprites defined
  44.  
  45.     VAR __RES% = 1   ' resolution - sub-pixels per pixel. Default to 1 to ease confusion.
  46. RETURN
  47.  
  48. COMMON DEF __SPMODE MNGID%, TYPE%
  49.     ' Available types:
  50.     ' -1 = off (remove from SBPE)
  51.     ' 0 = ghost - goes through walls
  52.     ' 2 = sticky
  53.    
  54.     IF TYPE% == -1 THEN
  55.         ___REMOVE_MNG MNGID%
  56.         RETURN
  57.     ENDIF
  58.    
  59.     VAR INDX% = __TOINDEX%[MNGID%]
  60.     IF INDX% == -1 THEN
  61.         INDX% = ___NEWINDEX%(MNGID%)
  62.     ENDIF
  63.    
  64.     __SPMODE%[INDX%] = TYPE%
  65. END
  66.  
  67.  
  68. DEF ___REMOVE_MNG MNGID%
  69.     VAR INDX% = __TOINDEX%[MNGID%]
  70.     IF INDX% == -1 THEN RETURN ' Already removed
  71.     __TOMNG%[INDX%] = -1  ' Remove from internal index
  72.     __SPMODE%[INDX%] = -1 ' Used in internal loop
  73.     PUSH __EMPTYINDEX%, INDX% ' Remember unused index
  74.     __TOINDEX%[MNGID%] = -1   ' Remove from management index
  75. END
  76.  
  77. DEF ___REMOVE_INDEX INDX%  ' I wonder if I'll need this
  78.     VAR MNGID% = __TOMNG%[INDX%]
  79.     IF MNGID% == -1 THEN RETURN ' Already removed
  80.     __TOINDEX%[MNGID%] = -1   ' Remove from management index
  81.     PUSH __EMPTYINDEX%, INDX% ' Remember unused index
  82.     __TOMNG%[INDX%] = -1  ' Remove from management index
  83.     __SPMODE%[INDX%] = -1 ' Used in internal loop
  84. END
  85.  
  86. DEF ___NEWINDEX%(MNGID%)
  87.     VAR INDX%
  88.     IF LEN(__EMPTYINDEX%) == 0 THEN
  89.         INDX% = LEN(__TOMNG%)
  90.         PUSH __TOMNG%, MNGID%
  91.         PUSH __X%, 0
  92.         PUSH __Y%, 0
  93.         PUSH __VX%, 0
  94.         PUSH __VY%, 0
  95.         PUSH __SPMODE%, 0
  96.     ELSE
  97.         INDX% = POP(__EMPTYINDEX%)
  98.         __TOMNG%[INDX%] = MNGID%
  99.         __X%[INDX%] = 0
  100.         __Y%[INDX%] = 0
  101.         __VX%[INDX%] = 0
  102.         __VY%[INDX%] = 0
  103.         __SPMODE%[INDX%] = 0
  104.     ENDIF
  105.     __TOINDEX%[MNGID%] = INDX%
  106.     RETURN INDX%
  107. END
  108.  
  109.  
  110.  
  111. '''''''''''''''''''''''''''''''''''''''''
  112. '   SPVEC - Sets a sprite's velocity    '
  113. '''''''''''''''''''''''''''''''''''''''''
  114.  
  115. DEF ___SPVEC INDX%, VX%, VY%
  116.     ' save new velocity
  117.     __VX%[INDX%] = VX%
  118.     __VY%[INDX%] = VY%
  119.    
  120.     ' set sprite's COLVEC for collision - we use it and the dev can also use it
  121.     SPCOLVEC MNGID%, VX%/__RES%, VY%/__RES%
  122. END
  123.  
  124. COMMON DEF __SPVEC MNGID%, VX%, VY%
  125.     VAR INDX% = __TOINDEX%[MNGID%]  ' get internal index
  126.     IF INDX% == -1 THEN RETURN  ' abort if we're not managing this sprite or it doesn't exist
  127.    
  128.     ___SPVEC INDX%, VX%, VY%
  129. END
  130.  
  131. COMMON DEF __SPVEC% MNGID%, VX%, VY%
  132.     __SPVEC MNGID%, VX%, VY%
  133. END
  134.     ' The above could instead use the glitch found by IAmAPersson:
  135.     ' http://smilebasicsource.com/forum?ftid=294
  136.     ' but what I wrote is the "correct" way so I'll keep it.
  137.  
  138. COMMON DEF __SPVEC# MNGID%, VX#, VY#
  139.     VAR INDX% = __TOINDEX%[MNGID%]  ' get internal index
  140.     IF INDX% == -1 THEN RETURN  ' abort if we're not managing this sprite or it doesn't exist
  141.    
  142.     ___SPVEC INDX%, ROUND(VX# * __RES%), ROUND(VY# * __RES%)
  143. END
  144.  
  145.  
  146.  
  147. '''''''''''''''''''''''''''''''''''''''''
  148. '   GETVEC - Gets a sprite's velocity   '
  149. '''''''''''''''''''''''''''''''''''''''''
  150.  
  151. ' Internal function, assumes INDX% is valid:
  152. DEF ___GETVEC INDX% OUT VX%, VY%
  153.     VX% = __VX%[INDX%]
  154.     VY% = __VY%[INDX%]
  155. END
  156.  
  157. COMMON DEF __GETVEC MNGID% OUT VX%, VY%
  158.     VAR INDX% = __TOINDEX%[MNGID%]
  159.     IF INDX% == -1 THEN RETURN  ' abort if we're not managing this sprite or it doesn't exist
  160.     ___GETVEC INDX% OUT VX%, VY%
  161. END
  162.  
  163. COMMON DEF __GETVEC% MNGID% OUT VX%, VY%
  164.     __GETVEC MNGID% OUT VX%, VY%    ' just make this func a mirror of the other one
  165. END
  166.  
  167. COMMON DEF __GETVEC# MNGID% OUT VX#, VY#
  168.     VAR INDX% = __TOINDEX%[MNGID%]
  169.     IF INDX% == -1 THEN RETURN  ' abort if we're not managing this sprite or it doesn't exist
  170.    
  171.     VAR VX%, VY%
  172.     ___GETVEC INDX% OUT VX%, VY%
  173.     VX# = VX% / __RES%
  174.     VY# = VY% / __RES%
  175. END
  176.  
  177.  
  178.  
  179. '''''''''''''''''''''''''''''''''''''''''
  180. '   SPOFS - Sets a sprite's position    '
  181. '''''''''''''''''''''''''''''''''''''''''
  182.  
  183. DEF ___SPOFS INDX%, X%, Y%
  184.     __X%[INDX%] = X%
  185.     __Y%[INDX%] = Y%
  186. END
  187.  
  188. COMMON DEF __SPOFS MNGID%, X%, Y%
  189.     VAR INDX% = __TOINDEX%[MNGID%]  ' get internal index
  190.     IF INDX% == -1 THEN RETURN  ' abort if we're not managing this sprite or it doesn't exist
  191.    
  192.     ___SPOFS INDX%, X%, Y%
  193.    
  194.     ' reset velocity
  195.     ___SPVEC INDX%, 0, 0
  196. END
  197.  
  198. COMMON DEF __SPOFS% MNGID%, X%, Y%
  199.     __SPOFS MNGID%, X%, Y%
  200. END
  201.  
  202. COMMON DEF __SPOFS# MNGID%, X#, Y#
  203.     VAR INDX% = __TOINDEX%[MNGID%]  ' get internal index
  204.     IF INDX% == -1 THEN RETURN  ' abort if we're not managing this sprite or it doesn't exist
  205.    
  206.     ___SPOFS INDX%, ROUND(X# * __RES%), ROUND(Y# * __RES%)
  207.    
  208.     ' reset velocity
  209.     ___SPVEC INDX%, 0, 0
  210. END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement