TGC Codebase Backup



S by RUCCUS

22nd Apr 2005 19:38
Summary

Snap objects to a grid while moving them with the mouse. Useful for Game Makers.



Description

Comments in source. Move objects around the map with the mouse using a "snapping" type movement. This version requires no media.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    `````````````````````````````````````````````````````````````````
`Snap-To-Grid Movement System (S-GMS)
`Programmed by RUCCUS
``````````````````````````````````````````````````````````````````
`S-GMS is a function created for Dark Basic Pro that allows the
`user to move any selected object with the mouse in a "snapping"
`way, much like proffesional map maker programs.
`The bones of the program uses part of my GMS function to move
`the object with the mouse in combination with some new code I
`created to move the object in sections.
`Use the code in any way you wish, no royalties are required but
`as always with all my code a simple mention of my name would be
`nice, and possibly a link to my site, www.ruccus.net. Goodluck,
`and if you have any questions feel free to email me at
`ruccus@ruccus.net.
``````````````````````````````````````````````````````````````````
`Standard Program Setup
SYNC ON:SYNC RATE 0:SET TEXT FONT "ARIAL":POSITION MOUSE 319.5,237
`Create a few objects for us to move
MAKE OBJECT BOX 1,30,30,30
POSITION OBJECT 1,-90,0,0

MAKE OBJECT BOX 2,30,30,30
POSITION OBJECT 2,90,0,0

MAKE OBJECT BOX 3,30,30,30
POSITION OBJECT 3,0,0,0
`IMPORTANT: The height at which you position the camera is vital.
`I find 435 to be about right for the GMS, any higher or lower
`can affect how the movement of the objects work. Experiment with
`it if you wish.
POSITION CAMERA 0,435,0
POINT CAMERA 0,0,0
`Start our main loop
DO
`Display some nifteh' text on the screen...
SET TEXT TO BOLD
CENTER TEXT 319.5,0,"Grid Movement System"
SET TEXT TO NORMAL
CENTER TEXT 319.5,15,"Programmed by RUCCUS"
SET TEXT TO BOLD
CENTER TEXT 319.5,450,"Special thanks to APEXnow, [Jimmy] and TheSturgeon for programming help!"
SET TEXT TO NORMAL
`Call the Move Object With Mouse function
MOVE_OBJECT_WITH_MOUSE()
`Refresh the screen and end the loop
SYNC
LOOP

`Move Object With Mouse Function

`Breakdown:
`The function stores the object's position for later use. Then
`checks if the mouse is being clicked. If it is, and the mouse is
`at the same coordinates as the object in question, then check
`where the mouse is next. If it's to the left of the object,
`move the object left, if it's to the right, move the object
`right, if it's infront of it, move the object forward. And
`finally if it's behind it, move it backward.
`If the object is off of the screen, reposition it at it's old
`coordinates to disallow moving it off of the screen.

`What to change for different purposes:
`Change what the for checks for for more objects. I.E If you want
`to check for object's between 1 and 10, use
`for i = 1 to 10 Step 1.
`Change the amount at which the object's move to a greater or
`lesser number for larger or lesser movement. Depending on the
`size of your grid and objects, you may want to change this.
`If all your objects are the same width and length, you could
`change the 30 to the object's width and length.
`As a rule of thumb, keep object sizes divisible by the amount
`you're moving the object.

FUNCTION MOVE_OBJECT_WITH_MOUSE()
FOR i = 1 TO 3 STEP 1
OLDX#=OBJECT POSITION X(i)
OLDY#=OBJECT POSITION Y(i)
OLDZ#=OBJECT POSITION Z(i)
IF MOUSECLICK()=1
IF MOUSEX()>=(OBJECT SCREEN X(i)-(OBJECT SIZE X(i)/2)-5) AND MOUSEX()<=OBJECT SCREEN X(i)+(OBJECT SIZE X(i)/2)+5 AND MOUSEY() >=(OBJECT SCREEN Y(i)-(OBJECT SIZE Y(i)/2))-10 AND MOUSEY()<=(OBJECT SCREEN Y(i)+(OBJECT SIZE Y(i)/2))+10
IF MOUSEY()< (OBJECT SCREEN Y(i)-(OBJECT SIZE Y(i)/2)+5) THEN MOVE OBJECT i,32
IF MOUSEY()> (OBJECT SCREEN Y(i)+(OBJECT SIZE Y(i)/2)-5) THEN MOVE OBJECT i,-32
IF MOUSEX()<= (OBJECT SCREEN X(i)-(OBJECT SIZE X(i)/2)+5) THEN MOVE OBJECT LEFT i,32
IF MOUSEX()> (OBJECT SCREEN X(i)+(OBJECT SIZE X(i)/2)-5) THEN MOVE OBJECT RIGHT i,32
IF OBJECT IN SCREEN (i)=0 THEN POSITION OBJECT i,OLDX#,OLDY#,OLDZ#
ENDIF
ENDIF
NEXT i
ENDFUNCTION
`````````````````````````````````````````````````````````````````
`Special Thanks
`````````````````````````````````````````````````````````````````
`APEXnow     : For helping me overcome my problem with moving the
`              object up and down.
`[Jimmy]     : For helping me understand the basics of implementing
`              background pictures, i.e the grid.
`TheSturgeon : For trying to help me :P
`````````````````````````````````````````````````````````````````
`Contact me at ruccus@ruccus.net or on the db irc channel for
`more information and help on this or any of my other code
`snippets and tutorials.
`````````````````````````````````````````````````````````````````
`Thanks
` - RUCCUS
`````````````````````````````````````````````````````````````````