; This example demo's SpriteHitMap and SpriteHitMapPixels ; functions together, as such, it can be one of the more ; complex examples to follow, since it's trying to show everything ; in one program. ; ; KEYS ; ==== ; ; SPACE = Toggle Collision Methods ; ENTER = Toggle Map Debug mode ; M = Step through sprite collsiion modes ; ; MOUSE ; ----- ; ; Left Button = Scale ; Middle Button = Rotate ; ; ----------------------------------------------------- ; --------->> SET UP <<------------ ; ----------------------------------------------------- SetFPS 75 ; Load a bigger font LoadFont "Verdana",1,32 ; create the random map from some circles Map,Level=Build_Random_Map(32,32) ; Load the ship image for our sprite ShipImage=LoadNewImage(ProgramDir$()+"Help/Commands/Media/ship.bmp",2) ; Loads the defiend vector shape that represents this image ; and attaches it to the ShipIMage #Include "shape" ShipShape=LoadNewShape("..\../Media/ship.Shape") ShiftShape ShipShape,-2,0 ImageShape ShipIMage,ShipShape ; Create the sprite Sprite=NewSprite(0,0,ShipIMage) PositionSpriteZ Sprite,10 ; Set up sprites display for this demo SpriteDrawMode Sprite,2+16 SpriteCollisionDebug Sprite,on SpriteFilter Sprite,on SpriteMaskColourCompression Sprite,off AutoCenterSpriteHandle sprite,true ; set sprites collision mode SpriteCollisionMode Sprite,1 ; rotated rect is the collision default SpriteCollisionRadius Sprite,30 ; Set Sprites Scale and rotation angle Scale#=1 Angle#=45 ; make a backdrop picture BackDrop=NewImage(800,600,2) RenderToImage Backdrop ; create a camera to view the scene with (attached to backdrop image) Cam=NewCamera() CameraCls Cam,off ; Randomly pick some Backdrop colours c1=RndRGB() c2=RndRGB() ; ----------------------------------------------------- ; --------->> MAIN LOOP OF PROGRAM <<------------ ; ----------------------------------------------------- Do ;------------------------------------------- ; Get the camera current position ;------------------------------------------- CamX=GetCameraX(cam) CamY=GetCameraY(cam) ;------------------------------------------- ; Get the mouse position in World space ;------------------------------------------- MX=MouseX()+CamX MY=MouseY()+CamY ;------------------------------------------- ; tell PB to capture the following drawing commands ;------------------------------------------- CaptureToScene ;------------------------------------------- ; clear the scene buffer so it's empty ;------------------------------------------- ClsScene ;------------------------------------------- ; set the capture depth to 100 and then ; capture a shadebox at the camera position ;------------------------------------------- CaptureDepth 100 ShadeBox CamX,CamY,CamX+800,CamY+600,c1,c1,c2,c2 ;------------------------------------------- ; draw the map level as depth 50 ;------------------------------------------- CaptureDepth 50 DrawMap map,Level,0,0 ;------------------------------------------- ; Check if sprite hits the map ;------------------------------------------- If MidMouseButton() Angle#=WrapAngle(Angle#,1) EndIf If LeftMouseButton() scale#+=0.01 If Scale#>3 Then Scale#=0.5 EndIf RotateSprite sprite,angle# ScaleSprite sprite,scale# PositionSprite Sprite,MX,my ; ------------------------------------------ ; Check for the impacts with the map ; ------------------------------------------ If CollisionMethod=0 Collision=SpriteHitMap(Map,Level,Sprite,MX,MY,PixelMode) Else Collision=SpriteHitMapPixels(Map,Level,Sprite,MX,MY) EndIf If Collision col=$f06070 Else Col=$3040ff EndIf SpriteTint Sprite,Col DrawSprite Sprite If Collision Colour=$ff0000 Message$="Hit" Else Colour=$0000ff Message$="Missed" EndIf ;------------------------------------------- ; Draw the scene with this camera ;------------------------------------------- DrawCamera cam RenderToScreen DrawImage backdrop,0,0,false ;------------------------------------------- ; check if the ENTER key was pressed ;------------------------------------------- If EnterKey() ; If so, toggle Debug mode on the map MapDebug Map,1-GetMapDebug(Map) FlushKeys EndIf ;------------------------------------------- ; check if the SPACE key was pressed ;------------------------------------------- If SpaceKey() CollisionMethod=1-CollisionMethod FlushKeys EndIf ;------------------------------------------- ; check if M or P keys are pressed ;------------------------------------------- If CollisionMethod=0 Select ScanCode() ; If M is pressed, step to the next collision mode Case 50 SpriteCollisionMode Sprite,Mod(GetSpriteCollisionMode(Sprite)+1,6) FlushKeys ; if P ios press toggle Pixel Mode collisions on or off Case 25 pixelMode=1-PixelMode FlushKeys EndSelect EndIf ;------------------------------------------- ; check if the users wanted to move the camera ;------------------------------------------- If LeftKey() Then MoveCamera Cam,-2,0 If RightKey() Then MoveCamera Cam,2,0 If UpKey() Then MoveCamera Cam,0,-2 If DownKey() Then MoveCamera Cam,0,2 SetCursor 0,0 Ink $ffffff Ink -1 If CollisionMethod=0 Print "Method:SpriteitMap" Print "Sprite Collision Mode:"+GetSpriteCOllisionModeName(Sprite) Print "Pixel Mode:"+Str$(pixelMode) Else Print "Method:SpriteitMapPixels" EndIf Ink colour Print "Collision:"+Message$ Sync Loop Function GetSpriteCOllisionModeName(ThisSprite) Name$="Unknown" If GetSpriteStatus(ThisSPrite) Select GetSpriteCollisionMode(ThisSprite) Case 0 Name$="Static Rect (Vector)" Case 1 Name$="Rotated Rect (Vector)" Case 2 Name$="Circle (Vector)" Case 3 Name$="Shape (Vector)" Case 4 Name$="Sliding Mode (World Sliding)" Case 5 Name$="Sticky Mode (World Sliding)" Case 6 Name$="Pixel Perfect" EndSelect EndIf EndFunction Name$ ; ----------------------------------------------------- ; ------------->> Build Random Map Scene <<------------ ; ----------------------------------------------------- Function Build_Random_Map(BlockWidth,BlockHeight) BackdropColour=$008f00 screen=NewFXImage(2400,1600) RenderToImage screen Cls BackdropColour CircleC 800,400,400,true,$8f8f8f CircleC 1700,400,400,true,$0f8faf For lp=0 To 150 x=Rnd(2400) y=Rnd(1600) rx=RndRange(50,150) ry=RndRange(10,50) EllipseC x,y,rx,ry,true, BackdropColour Next Map=NewMap(50) ; Create 1024 FX BLocks for this map CreateMapGFX Map,BlockWidth,BlockHeight,1024,BackdropColour,2 BlocksX=GetSurfaceWidth()/BlockWidth BlocksY=GetSurfaceHeight()/BlockWidth Level=NewLevel(Map,BlocksX,BlocksY) LevelTransparent Map,Level,0 GetMapBlk Map,Tile,0,0 Tile=1 For ylp=0 To GetLevelHeight(map,level)-1 Ypos=ylp*BlockHeight If Ypos+BlockHeight<=GetSurfaceHeight() For xlp=0 To GetLevelWidth(map,level)-1 Xpos=xlp*BlockWidth GetMapBlk Map,Tile,Xpos,ypos If GetMapBlockTransparent(Map,Tile)>-1 PokeLevelTile Map,Level,xlp,ylp,tile tile++ EndIf If Tile=>GetMapBlocks(Map) Then ExitFor ylp Next EndIf Next MapDebug Map,true RenderToScreen EndFunction Map,Level |