This is an adaptation of the previous guide "Creating a simple lasso to
select sprites on screen." from thescenecommander [DEV].
To begin creating the lasso box you press the screen and without raising
your finger you move and extend the box; When you release the press all
the sprites inside the lasso box are choosen and their color is soften to
stand out them.
Once the lasso box is created, two command buttons are shown in the
bottom-right corner, you can press either of them (Move or Attack
buttons); The buttons commands are not really implemented but after you
press them you also must press again in some point of the screen where the
commands would take action (actually it only shows a cross in the screen
during three seconds).
You can cancel the commands buttons menu pressing in any empty part of
the screen but if you press any of the buttons you must choose a
coordinate where the cross will be painted and wait three seconds to be
closed.
The program is a little complicated because it uses several state
variables to remember where it is in his progression.
The only media resource used in this project is the image file
"item0.png" that you must copy from "\Projects\Sprites\SpriteAnim1\media"
to the media folder in your project.
TransDiv
// Project: Another RTS lasso demo
// Created: 2015-05-05
// set window properties
SetWindowTitle( "Another RTS lasso demo" )
SetWindowSize( 1024, 768, 0 )
// set display properties
SetVirtualResolution( 1024, 768 )
// set some variable for the corners of the lasso box
global topleftx=0
global toplefty=0
global bottomrighttx=0
global bottomrighty=0
// lassoState can have three states, 0 = No lassso, 1 = Building lasso, 2 = Lasso builded
global lassoState=0
global unitsInLasso = 0
// commandState can have four states, 0 = No command, 1 = Show command menu, 2 = Attack command choosen, 3 = Move command choosen
global commandState=0
// timeCommand# is used to control the show of the command cross for three seconds
global timeCommand# = 0.0
// where to paint the intersection of the command cross
global crossX = 0
global crossY = 0
// paint or not the command cross
global paintCross = 0
// timeCross# is used to control the show of the command cross for three seconds
global timeCross# = 0.0
// create a type for the various elements of 'The units'
type sampleunittype
x as float
y as float
angle as integer
sprite as float
lasttime as float
choosen as integer
endtype
// color of the box
colour1=makecolor(255,255,255)
colour2=makecolor(255,255,255)
colour3=makecolor(255,255,255)
colour4=makecolor(255,255,255)
filled=0
// set up an array for the units.
dim units[10] as sampleunittype
// text to show during the command cross
txtCross = CreateText("3")
SetTextSize(txtCross,20)
SetTextVisible(txtCross,0)
// Attack command button
btnAttack = CreateSprite(0)
SetSpriteSize( btnAttack, 80, 25 )
SetSpriteColor( btnAttack, 255, 255, 0, 255 )
SetSpriteDepth(btnAttack,9)
SetSpritePosition(btnAttack,930,730)
txtAttack = CreateText("Attack")
SetTextSize(txtAttack,20)
SetTextColor(txtAttack,0,0,0,255)
SetTextPosition(txtAttack,930+2,730+2)
SetSpriteVisible(btnAttack,0)
SetTextVisible(txtAttack,0)
// Move command button
btnMove = CreateSprite(0)
SetSpriteSize( btnMove, 80, 25 )
SetSpriteColor( btnMove, 255, 255, 0, 255 )
SetSpriteDepth(btnMove,9)
SetSpritePosition(btnMove,830,730)
txtMove = CreateText("Move")
SetTextSize(txtMove,20)
SetTextColor(txtMove,0,0,0,255)
SetTextPosition(txtMove,830+15,730+2)
SetSpriteVisible(btnMove,0)
SetTextVisible(txtMove,0)
//load in a sample image so we can see the sprite. This is the default balloon from SpriteAnim1 example that we've used in other demo's.
loadimage(1,"item0.png")
//Make our sprites
for makesprites=1 to 10
//create a sprite with image 1
createsprite(makesprites,1)
//randomly place them on screen
units[makesprites].x=random(30,990)
units[makesprites].y=random(30,710)
// we will be moving them around the screen at random angles to simulate movement
units[makesprites].angle=random(1,360)
//assign a sprite number
units[makesprites].sprite=makesprites
units[makesprites].choosen=0
next makesprites
//
// main loop
do
// Attack command button pressed
if GetPointerPressed() = 1 and commandState = 1 and GetSpriteHitTest(btnAttack,GetpointerX(),GetPointery() )
SetSpriteColor( btnAttack, 255, 0, 0, 255 )
timeCommand# = timer()
commandState = 2
endif
// Move command button pressed
if GetPointerPressed() = 1 and commandState = 1 and GetSpriteHitTest(btnMove,GetpointerX(),GetPointery() )
SetSpriteColor( btnMove, 0, 255, 0, 255 )
timeCommand# = timer()
commandState = 3
endif
// if we press buttons Attack or Move get the coordinates where to paint the cross, also timeCommand# is used to create half a
// second delay to avoid paint the cross over the buttons.
if GetPointerPressed() = 1 and paintCross = 0 and (timer() - timeCommand# > 0.5 ) and ( commandState = 2 or commandState = 3 )
crossX = GetpointerX()
crossY = GetpointerY()
timeCross# = timer()
paintCross = 1
endif
// paint the command cross or no?
if paintCross = 1
// will show the command cross for three seconds
if timer() - timeCross# < 3
if commandState = 2
Drawline( 0,crossY,1024,crossY,255,0,0)
Drawline( crossX,0, crossX,768,255,0,0)
SetTextColor(txtCross,255,0,0,255)
endif
if commandState = 3
Drawline( 0,crossY,1024,crossY,0,255,0)
Drawline( crossX,0, crossX,768,0,255,0)
SetTextColor(txtCross,0,255,0,255)
endif
// besides the cross, we will show a decreasing number from 3 to 1
SetTextPosition(txtCross,crossX+2,CrossY+2)
SetTextString(txtCross,Str( Ceil(3 - (timer() - timeCross#) ) ))
SetTextVisible(txtCross,1)
else
// three seconds have passed, go back command menu to yellow color, reinit command menu and
// indicate not to paint the comand cross
SetSpriteColor( btnAttack, 255, 255, 0, 255 )
SetSpriteColor( btnMove, 255, 255, 0, 255 )
SetTextVisible(txtCross,0)
paintCross = 0
commandState=1
endif
endif
// begin creating the lasso box, also if command buttons are shown, deactivate them.
if GetPointerPressed() = 1 and ( commandState = 0 or commandState = 1 )
topleftx=GetpointerX()
toplefty=GetPointery()
lassoState=1
SetSpriteVisible(btnAttack,0)
SetTextVisible(txtAttack,0)
SetSpriteVisible(btnMove,0)
SetTextVisible(txtMove,0)
commandState=0
endif
// if lasso box is being created draw a box around it
if GetpointerState()=1 and lassoState=1 and commandState = 0
bottomrightx=GetpointerX()
bottomrighty=GetPointery()
DrawBox(topleftx,toplefty,bottomrightx,bottomrighty,colour1,colour2,colour3,colour4,filled)
endif
// if we finish creating the lasso box
if GetpointerState()=0 and lassoState=1 and commandState = 0
//Correct corners
if topleftx < bottomrightx
temp = topleftx
topleftx = bottomrightx
bottomrightx = temp
endif
if toplefty < bottomrighty
temp = toplefty
toplefty = bottomrighty
bottomrighty = temp
endif
// Paint the unit's sprites.
unitsInLasso = 0
for displaysprites=1 to 10
// If the sprite in within the lasso box change his color and set the choosen variable of the unit to 1
If GetSpriteinBox(units[displaysprites].sprite,topleftx,toplefty,bottomrightx,bottomrighty) or GetSpriteinBox(units[displaysprites].sprite,bottomrightx,bottomrighty,topleftx,toplefty)
SetSpriteColorAlpha(units[displaysprites].sprite,80)
units[displaysprites].choosen=1
inc unitsInLasso // count the number of units in lasso box
else
// If not in lasso box, set his color back to normal and choosen variable to 0
SetSpriteColorAlpha(units[displaysprites].sprite,255)
units[displaysprites].choosen=0
endif
next displaysprites
lassoState=2
// if more than zero units in lasso box activate the command buttons
if unitsInLasso > 0
SetSpriteVisible(btnAttack,1)
SetTextVisible(txtAttack,1)
SetSpriteVisible(btnMove,1)
SetTextVisible(txtMove,1)
commandState=1
else
commandState=0
endif
sync()
endif
// display and move sprites. Use loop to check if sprite is in the box and colour if covered
for displaysprites=1 to 10
// move sprite by angle
units[displaysprites].x=units[displaysprites].x+sin(units[displaysprites].angle)
units[displaysprites].y=units[displaysprites].y+cos(units[displaysprites].angle)
// change direction every 3 seconds. This sample doesn't check to prevent sprites leaving the screen.
if timer()-units[displaysprites].lasttime>3
//reset the timer
units[displaysprites].lasttime=timer()
// set a new random angle
units[displaysprites].angle=random(1,360)
endif
// Set the sprites new position
SetSpritePosition(units[displaysprites].sprite,units[displaysprites].x,units[displaysprites].y)
next displaysprites
//update screen
sync()
loop
TransDiv