Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ' SBPE - SmileBASIC Platforming Engine
- ' ====================================
- ' Copyright © Nitai Sasson
- ' All rights reserved
- ' DO NOT UPLOAD THIS FILE
- ' OR ANY DERIVATIVES THEREOF
- ' TO SMILEBOOM SERVERS WITHOUT
- ' MY EXPLICIT PERMISSION
- ' For proper functionality of this library, you must NEVER:
- ' 1. Use SPOFS or SPCOLVEC on an active sprite
- ' 2. Use sprites from different screens - it works on either the lower screen or the top screen, not both.
- ' Most functions exist 4 times:
- ' 1. ___FUNC (has an extra _) - Internal function, not to be used by library users.
- ' 2. __FUNC - Regular function, the one you should use in most cases. Uses integer mode, which means coordinates are identical to
- ' 3. __FUNC% - Same as __FUNC, but explicitly uses integer mode. In practice it is in every way identical to __FUNC.
- ' 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.
- OPTION STRICT
- GOSUB @INIT
- 'TERMINATE < just for notepad++ to not bug out
- END
- @INIT
- ' Back-and-forth system between internal indexing of sprites and the actual management numbers:
- DIM __TOINDEX%[512], __TOMNG%[0], __EMPTYINDEX%[0]
- ' vaiables for each sprite:
- DIM __X%[0], __Y%[0], __VX%[0], __VY%[0], __SPMODE%[0]
- FILL __TOINDEX%, -1 ' Start with no sprites defined
- 'FILL __SPMODE%, -1 ' Start with no sprites defined
- VAR __RES% = 1 ' resolution - sub-pixels per pixel. Default to 1 to ease confusion.
- RETURN
- COMMON DEF __SPMODE MNGID%, TYPE%
- ' Available types:
- ' -1 = off (remove from SBPE)
- ' 0 = ghost - goes through walls
- ' 2 = sticky
- IF TYPE% == -1 THEN
- ___REMOVE_MNG MNGID%
- RETURN
- ENDIF
- VAR INDX% = __TOINDEX%[MNGID%]
- IF INDX% == -1 THEN
- INDX% = ___NEWINDEX%(MNGID%)
- ENDIF
- __SPMODE%[INDX%] = TYPE%
- END
- DEF ___REMOVE_MNG MNGID%
- VAR INDX% = __TOINDEX%[MNGID%]
- IF INDX% == -1 THEN RETURN ' Already removed
- __TOMNG%[INDX%] = -1 ' Remove from internal index
- __SPMODE%[INDX%] = -1 ' Used in internal loop
- PUSH __EMPTYINDEX%, INDX% ' Remember unused index
- __TOINDEX%[MNGID%] = -1 ' Remove from management index
- END
- DEF ___REMOVE_INDEX INDX% ' I wonder if I'll need this
- VAR MNGID% = __TOMNG%[INDX%]
- IF MNGID% == -1 THEN RETURN ' Already removed
- __TOINDEX%[MNGID%] = -1 ' Remove from management index
- PUSH __EMPTYINDEX%, INDX% ' Remember unused index
- __TOMNG%[INDX%] = -1 ' Remove from management index
- __SPMODE%[INDX%] = -1 ' Used in internal loop
- END
- DEF ___NEWINDEX%(MNGID%)
- VAR INDX%
- IF LEN(__EMPTYINDEX%) == 0 THEN
- INDX% = LEN(__TOMNG%)
- PUSH __TOMNG%, MNGID%
- PUSH __X%, 0
- PUSH __Y%, 0
- PUSH __VX%, 0
- PUSH __VY%, 0
- PUSH __SPMODE%, 0
- ELSE
- INDX% = POP(__EMPTYINDEX%)
- __TOMNG%[INDX%] = MNGID%
- __X%[INDX%] = 0
- __Y%[INDX%] = 0
- __VX%[INDX%] = 0
- __VY%[INDX%] = 0
- __SPMODE%[INDX%] = 0
- ENDIF
- __TOINDEX%[MNGID%] = INDX%
- RETURN INDX%
- END
- '''''''''''''''''''''''''''''''''''''''''
- ' SPVEC - Sets a sprite's velocity '
- '''''''''''''''''''''''''''''''''''''''''
- DEF ___SPVEC INDX%, VX%, VY%
- ' save new velocity
- __VX%[INDX%] = VX%
- __VY%[INDX%] = VY%
- ' set sprite's COLVEC for collision - we use it and the dev can also use it
- SPCOLVEC MNGID%, VX%/__RES%, VY%/__RES%
- END
- COMMON DEF __SPVEC MNGID%, VX%, VY%
- VAR INDX% = __TOINDEX%[MNGID%] ' get internal index
- IF INDX% == -1 THEN RETURN ' abort if we're not managing this sprite or it doesn't exist
- ___SPVEC INDX%, VX%, VY%
- END
- COMMON DEF __SPVEC% MNGID%, VX%, VY%
- __SPVEC MNGID%, VX%, VY%
- END
- ' The above could instead use the glitch found by IAmAPersson:
- ' http://smilebasicsource.com/forum?ftid=294
- ' but what I wrote is the "correct" way so I'll keep it.
- COMMON DEF __SPVEC# MNGID%, VX#, VY#
- VAR INDX% = __TOINDEX%[MNGID%] ' get internal index
- IF INDX% == -1 THEN RETURN ' abort if we're not managing this sprite or it doesn't exist
- ___SPVEC INDX%, ROUND(VX# * __RES%), ROUND(VY# * __RES%)
- END
- '''''''''''''''''''''''''''''''''''''''''
- ' GETVEC - Gets a sprite's velocity '
- '''''''''''''''''''''''''''''''''''''''''
- ' Internal function, assumes INDX% is valid:
- DEF ___GETVEC INDX% OUT VX%, VY%
- VX% = __VX%[INDX%]
- VY% = __VY%[INDX%]
- END
- COMMON DEF __GETVEC MNGID% OUT VX%, VY%
- VAR INDX% = __TOINDEX%[MNGID%]
- IF INDX% == -1 THEN RETURN ' abort if we're not managing this sprite or it doesn't exist
- ___GETVEC INDX% OUT VX%, VY%
- END
- COMMON DEF __GETVEC% MNGID% OUT VX%, VY%
- __GETVEC MNGID% OUT VX%, VY% ' just make this func a mirror of the other one
- END
- COMMON DEF __GETVEC# MNGID% OUT VX#, VY#
- VAR INDX% = __TOINDEX%[MNGID%]
- IF INDX% == -1 THEN RETURN ' abort if we're not managing this sprite or it doesn't exist
- VAR VX%, VY%
- ___GETVEC INDX% OUT VX%, VY%
- VX# = VX% / __RES%
- VY# = VY% / __RES%
- END
- '''''''''''''''''''''''''''''''''''''''''
- ' SPOFS - Sets a sprite's position '
- '''''''''''''''''''''''''''''''''''''''''
- DEF ___SPOFS INDX%, X%, Y%
- __X%[INDX%] = X%
- __Y%[INDX%] = Y%
- END
- COMMON DEF __SPOFS MNGID%, X%, Y%
- VAR INDX% = __TOINDEX%[MNGID%] ' get internal index
- IF INDX% == -1 THEN RETURN ' abort if we're not managing this sprite or it doesn't exist
- ___SPOFS INDX%, X%, Y%
- ' reset velocity
- ___SPVEC INDX%, 0, 0
- END
- COMMON DEF __SPOFS% MNGID%, X%, Y%
- __SPOFS MNGID%, X%, Y%
- END
- COMMON DEF __SPOFS# MNGID%, X#, Y#
- VAR INDX% = __TOINDEX%[MNGID%] ' get internal index
- IF INDX% == -1 THEN RETURN ' abort if we're not managing this sprite or it doesn't exist
- ___SPOFS INDX%, ROUND(X# * __RES%), ROUND(Y# * __RES%)
- ' reset velocity
- ___SPVEC INDX%, 0, 0
- END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement