Monday 9 April 2012

Merging Functions

Now we have two almost identical functions where one would do.  ShowTrashCan() and ShowObjectUnder().

We are going to replace these with HighlightSprite(), which will have two parameters, the sprite as before and now a frame number.

To keep things tidy, first we define a couple of constants for the frames at the start of the program.

#constant FRAME_HIGHLIGHT 251
#constant FRAME_TRASHCAN 252

We are also going to rename the HideTrashCan() function to HideHighLight().
Delete one of the functions - it doesn't matter which one, and change the other to.

function HighlightSprite( thisSprite , thisFrame )
   if thisSprite > 0 and thisFrame > 0 and thisFrame < 253
      if getSpriteExists ( thisSprite )
         HideHighLight()
         display.highSprite = cloneSprite( thisSprite )
         setSpriteFrame( HighSprite() , thisFrame )
         setSpriteDepth( HighSprite() , getSpriteDepth( HighSprite() ) - 1)
      endif
   endif
endfunction


Now we change the calls from this;

if WorldToScreenY( thisY#) >= getTextY( panelText() )
   ShowTrashCan( thisSpritePicked )
else
   thisObjectUnder = WorkObjectIsOver( thisObjectPicked )
   if thisObjectUnder > 0
      ShowObjectUnder( WorkObjectSprite( thisObjectUnder ) )
   else
      HideTrashCan()
   endif
endif


to this.

if WorldToScreenY( thisY#) >= getTextY( panelText() )
   HighlightSprite( thisSpritePicked , FRAME_TRASHCAN )
else
   thisObjectUnder = WorkObjectIsOver( thisObjectPicked )
   if thisObjectUnder > 0
       HighlightSprite( WorkObjectSprite( thisObjectUnder ) , FRAME_HIGHLIGHT )
   else
      HideHighLight()
   endif
endif

and don't forget to rename

function HideTrashCan()

to

function HideHighLight()

Job done.
Since it's been a while, here's a code check

Unformatted etc..

// April 9th 2012 - Tabs converted to 3 Spaces
#constant PANEL_ROWS   3
#constant PANEL_COLS   4

#constant DEPTH_SCORE   20
#constant DEPTH_PANEL   30
#constant DEPTH_WORK   40

#constant MAX_WORKOBJECTS   100

#constant FRAME_HIGHLIGHT   251
#constant FRAME_TRASHCAN   252

type displayType
   physWidth#
   physHeight#
   virtWidth#
   virtHeight#
   iconSize#
   textHeight#
   lastFPS#
   iconImage
   pointDX#
   pointDY#
   pointWX#
   pointWY#
   highSprite
endtype

global display as displayType
display.physWidth# = getDeviceWidth()
display.physHeight# = getDeviceHeight()
display.iconSize# = PhysWidth() / PANEL_COLS
display.textHeight# = PhysHeight() / 20.0
display.highSprite = 0

setVirtualResolution( PhysWidth() , PhysHeight() )
display.virtWidth# = getVirtualWidth()
display.virtHeight# = getVirtualHeight()
display.iconImage = SafeloadImage( "objects.png" )

type panelType
   height#
   speed#
   isOpen
   image
   sprite
   text
   sound
   iconMax
endtype

global panel as panelType
panel.height# = IconSize() * PANEL_ROWS
panel.speed# = 0.0
panel.isOpen = 0
panel.iconMax = PANEL_ROWS * PANEL_COLS

panel.Image = SafeloadImage( "panel.jpg" )
setImageWrapU( PanelImage() , 1 )
setImageWrapV( PanelImage() , 1 )

panel.sprite = createSprite( PanelImage() )
uScale# = getImageWidth( PanelImage() ) / PhysWidth()
vScale# = getImageHeight( PanelImage() ) / PanelHeight()
setSpriteUVScale( PanelSprite() , uScale# , vScale# )
setSpriteSize( PanelSprite() , PhysWidth() , PanelHeight() )
setSpritePosition( PanelSprite() , 0 , PhysHeight() )
setSpriteDepth( PanelSprite() , DEPTH_PANEL)

panel.text = createText( "Objects" )
setTextSize( panelText() , textHeight() )
setTextAlignMent( panelText() , 1)
setTextPosition( panelText() , PhysWidth() / 2.0 , PhysHeight() - TextHeight() )
setTextDepth( panelText() , DEPTH_PANEL - 1 )

panel.sound = SafeLoadSound("panel.wav")

dim panelIconSprite[ panelIconMax() ]
for icon = 1 to panelIconMax()
   panelIconSprite[ icon ] = createSprite( 0 )
   setSpriteSize( panelIconSprite[ icon ] , IconSize() - 4, IconSize() - 4)
   setSpriteColor( panelIconSprite[ icon ] , 255 , 255 , 255 , 127 )
next icon

type WorkObjectType
   isFree
   objectID
   sprite
endtype

dim workObject[ MAX_WORKOBJECTS ] as WorkObjectType
for thisObject = 1 to MAX_WORKOBJECTS
   workObject[ thisObject ].isFree = 1
   workObject[ thisObject ].objectID = 0
   workObject[ thisObject ].sprite = 0
next thisObject
global numWorkObjects = 0

imageRef = SafeloadImage( "finger.png" )
spriteRef = createSprite( imageRef )

PositionPanelIcons()

testObject1 = AddWorkObject( 1 , 0 )
CreateWorkObjectSprite( testObject1 , 100 , 100 )
testObject2 = AddWorkObject( 10 , 0 )
CreateWorkObjectSprite( testObject2 , 200 , 200 )

thisSpritePicked = 0
thisObjectPicked = 0
thisPickOffsetX# = 0
thisPickOffsetY# = 0

do
   LoopStart()
   print( "FPS: " + str ( lastFPS() ) )

   if getPointerPressed() > 0
      if PointDY() >= getTextY( panelText() ) and PointDY() < getSpriteY( panelSprite() )
         ToggleTrayOpen()
      elseif PointDY() < getTextY( panelText() )
         thisSpritePicked = getSpriteHit( PointWX() , PointWY() )
         if thisSpritePicked > 0
            thisObjectPicked = FindWorkObjectBySprite( thisSpritePicked )
            if thisObjectPicked > 0
               thisPickOffsetX# = PointWX() - getSpriteXByOffset( thisSpritePicked )
               thisPickOffsetY# = PointWY() - getSpriteYByOffset( thisSpritePicked )
            endif
         endif
      endif
   elseif getPointerReleased() > 0

   elseif getPointerState() > 0
      if thisObjectPicked > 0
         thisX# = PointWX() - thisPickOffsetX#
         thisY# = PointWY() - thisPickOffsetY#
         setSpritePositionByOffset( thisSpritePicked , thisX# , thisY# )
         setSpriteDepth( thisSpritePicked , DEPTH_PANEL - 5 )
         if WorldToScreenY( thisY#) >= getTextY( panelText() )
            HighlightSprite( thisSpritePicked , FRAME_TRASHCAN )
         else
            thisObjectUnder = WorkObjectIsOver( thisObjectPicked )
            if thisObjectUnder > 0
               HighlightSprite( WorkObjectSprite( thisObjectUnder ) , FRAME_HIGHLIGHT )
            else
               HideHighLight()
            endif
         endif
      endif
   endif

   if panelIsOpen() = 1
      if getSpriteY( panelSprite() ) > ( PhysHeight() - panelHeight() )
         MovePanel( -panelSpeed() )
      endif
   else
      if getSpriteY( panelSprite() ) < PhysHeight()
         MovePanel( panelSpeed() )
      endif
   endif

   setSpritePosition( spriteRef , PointDX() , PointDY() )
   printc( "Physical: " )
   print( str( PhysWidth() ) + " x " + str( PhysHeight() ) )
   printc( "Pointer (Display): " )
   print( str( PointDX() ) + " x " + str( PointDY() ) )
   printc( "Pointer (World): " )
   print( str( PointWX() ) + " x " + str( PointWY() ) )
   printc( "Sprite: ")
   print( str( getSpriteHit( PointDX() , PointDY() )))
   printc( "Sprite Picked: ")
   printc( str( thisSpritePicked ))
   printc( ", Object Picked: ")
   printc( str( thisObjectPicked ))
   printc( ", Over : " )
   print( str( WorkObjectIsOver( thisObjectPicked ) ))
   sync()
loop

function LoopStart()
   display.lastFPS# = screenFPS()
   panel.speed# = panelHeight() / (lastFPS() * 1.25)
   display.pointDX# = getPointerX()
   display.pointDY# = getPointerY()
   display.pointWX# = ScreentoWorldX(PointDX())
   display.pointWY# = ScreentoWorldY(PointDY())
endfunction

function ToggleTrayOpen()
   panel.isOpen = 1 - panelIsOpen()
   if panelSound() > 0 then playSound( panelSound() )
endfunction

function MovePanel( distance# )
   setSpriteY( panelSprite() , getSpriteY( panelSprite() ) + distance# )
   setTextY( panelText() , getTextY( panelText() ) + distance# )
   PositionPanelIcons()
endfunction

function PositionPanelIcons()
   iconDepth = getSpriteDepth( PanelSprite() ) - 1
   for icon = 1 to panelIconMax()
      offset = icon - 1
      col = offset mod PANEL_COLS
      row = offset / PANEL_COLS
      x = col * IconSize() + 2
      y = row * IconSize() + getSpriteY( PanelSprite() ) + 2
      setSpritePosition( panelIconSprite[ icon ] , x , y )
      setSpriteDepth( panelIconSprite[ icon ] , iconDepth )
   next icon
endfunction

function SafeLoadImage( thisFileName$ )
   if getFileExists( thisFileName$ )
      thisImageRef = loadImage( thisFileName$ )
   else
      thisImageRef = 0
   endif
endfunction thisImageRef

function SafeLoadSound( thisFileName$ )
   if getFileExists( thisFileName$ )
      thisSoundRef = loadSound( thisFileName$ )
   else
      thisSoundRef = 0
   endif
endfunction thisSoundRef

// Work Object Functions

function TopWorkObject()
endfunction numWorkObjects

function WorkObjectValid( thisObject )
   thisBool = ( thisObject > 0 and thisObject <= TopWorkObject() )
endfunction thisBool

function FirstFreeWorkObject()
   for thisObject = 1 to TopWorkObject()
      if WorkObjectIsFree( thisObject ) > 0 then exit
   next thisObject
   if thisObject > MAX_WORKOBJECTS then thisObject = 0
endfunction thisObject

function AddWorkObject( thisObjectID, thisSprite )
   thisObject = FirstFreeWorkObject()
   if thisObject > TopWorkObject() then numWorkObjects = thisObject
   if WorkObjectValid( thisObject )
      workObject[ thisObject ].isFree = 0
      workObject[ thisObject ].objectID = thisObjectID
      workObject[ thisObject ].sprite = thisSprite
   endif
endfunction thisObject

function DelWorkObject( thisObject )
   if WorkObjectValid( thisObject )
      workObject[ thisObject ].isFree = 1
      if WorkObjectSprite( thisObject ) > 0
         thisSprite = WorkObjectSprite( thisObject )
         workObject[ thisObject ].sprite = 0
         if getSpriteExists( thisSprite ) then deleteSprite( thisSprite )
      endif
      while WorkObjectIsFree( TopWorkObject() ) = 1 and TopWorkObject() >= 0
         dec numWorkObjects , 1
      endwhile
   endif
endfunction

function CreateWorkObjectSprite( thisObject , thisX , thisY )
   if WorkObjectIsFree( thisObject ) = 0
      if WorkObjectSprite( thisObject ) = 0 then workObject[ thisObject ].sprite = createSprite( IconImage() )
      thisSprite = WorkObjectSprite( thisObject )
      fixSpriteToScreen( thisSprite , 0 )
      setSpriteSize( thisSprite , IconSize() , - 1)
      setSpriteOffset( thisSprite , iconSize() / 2.0 , iconSize() / 2.0 )
      setSpritePositionByOffset( thisSprite , thisX , thisY )
      setSpriteDepth( thisSprite , DEPTH_WORK - 1 )
      setSpriteAnimation( thisSprite , 64 , 64 , 252 )
      setSpriteFrame( thisSprite , WorkObjectID( thisObject ) )
   endif
endfunction

function WorkObjectIsOver( thisWorkObject )
   thisSprite = WorkObjectSprite( thisWorkObject )
   if thisSprite > 0
      thisX# = getSpriteXByOffset( thisSprite )
      thisY# = getSpriteYByOffset( thisSprite )
      for thisObject = 1 to TopWorkObject()
         if thisObject <> thisWorkObject
            thisSprite = WorkObjectSprite( thisObject )
            if thisSprite > 0
               diffX# = abs( getSpriteXByOffset( thisSprite ) - thisX# )
               diffY# = abs( getSpriteYByOffset( thisSprite ) - thisY# )
               if diffX# < iconSize() and diffY# < iconSize() then exit
            endif
         endif
      next thisObject
      if thisObject > TopWorkObject() then thisObject = 0
   else
      thisObject = -1
   endif
endfunction thisObject

function FindWorkObjectBySprite( thisSprite )
   for thisObject = 1 to TopWorkObject()
      if WorkObjectSprite( thisObject ) = thisSprite then exit
   next thisObject
   if thisObject > TopWorkObject() then thisObject = 0
endfunction thisObject

function HideHighLight()
   if HighSprite() > 0
      if getSpriteExists( HighSprite() ) then deleteSprite( HighSprite() )
      display.highSprite = 0
   endif
endfunction

function HighlightSprite( thisSprite , thisFrame )
   if thisSprite > 0 and thisFrame > 0 and thisFrame < 253
      if getSpriteExists ( thisSprite )
         HideHighLight()
         display.highSprite = cloneSprite( thisSprite )
         setSpriteFrame( HighSprite() , thisFrame )
         setSpriteDepth( HighSprite() , getSpriteDepth( HighSprite() ) - 1)
      endif
   endif
endfunction

// Return Functions - WorkObject Array

function WorkObjectIsFree( thisObject )
   if WorkObjectValid( thisObject )
      thisIsFree = workObject[ thisObject ].isFree
   else
      thisIsFree = -1
   endif
endfunction thisIsFree

function WorkObjectID( thisObject )
   if WorkObjectValid( thisObject )
      thisObjectID = workObject[ thisObject ].objectID
   else
      thisObjectID = -1
   endif
endfunction thisObjectID

function WorkObjectSprite( thisObject )
   if WorkObjectValid( thisObject )
      thisSprite = workObject[ thisObject ].sprite
   else
      thisSprite = -1
   endif
endfunction thisSprite

// Return Functions  - Display

function PhysWidth()
endfunction display.physWidth#
function PhysHeight()
endfunction display.physHeight#
function VirtWidth()
endfunction display.virtWidth#
function VirtHeight()
endfunction display.virtHeight#
function IconSize()
endfunction display.iconSize#
function TextHeight()
endfunction display.textHeight#
function LastFPS()
endfunction display.lastFPS#
function IconImage()
endfunction display.iconImage
function PointDX()
endfunction display.pointDX#
function PointDY()
endfunction display.pointDY#
function PointWX()
endfunction display.pointWX#
function PointWY()
endfunction display.pointWY#
function HighSprite()
endfunction display.highSprite

// Return Functions  - Panel

function PanelHeight()
endfunction panel.height#
function PanelSpeed()
endfunction panel.speed#
function PanelIsOpen()
endfunction panel.isOpen
function PanelImage()
endfunction panel.image
function PanelSprite()
endfunction panel.sprite
function PanelText()
endfunction panel.text
function PanelSound()
endfunction panel.sound
function PanelIconMax()
endfunction panel.iconMax
//

No comments:

Post a Comment