Ooops
October 28, 2020, 06:15:49 AM

Author Topic: [bb] RTS Drag Select by zoqfotpik [ 1+ years ago ]  (Read 1332 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] RTS Drag Select by zoqfotpik [ 1+ years ago ]
« on: June 29, 2017, 12:28:43 AM »
Title : RTS Drag Select
Author : zoqfotpik
Posted : 1+ years ago

Description : This is the drag select we're all familiar with from RTS games.  Knock yourselves out.

Code :
Code: BlitzBasic
  1. ' RTS Drag Select
  2. ' By ZoqFotPik
  3.  
  4.  
  5. Type dragrect ' a mousedrag rect
  6.         Field upperleft:Int, upperright:Int, lowerleft:Int, lowerright:Int
  7. End Type
  8.  
  9. Global objects:TList = New TList
  10.  
  11. Type selectobject
  12.         Field x:Int
  13.         Field y:Int
  14.         Field selected:Int
  15.         Method draw()
  16.                 SetColor 255,255,255
  17.                 DrawRect x-5,y-5,10,10
  18.                
  19.                 If selected = 1
  20.                         SetColor 0,255,0
  21.                         DrawRect x-10,y-10,15,15
  22.                 EndIf
  23.         End Method
  24. End Type
  25.                
  26. Graphics 640, 480
  27.  
  28. For i = 1 To 100
  29.         Local tempobj:selectobject = New selectobject
  30.         tempobj.x = Rand(640)
  31.         tempobj.y = Rand(480)
  32.         objects.addlast(tempobj)
  33. Next
  34.  
  35. Global selectx:Int
  36. Global selecty:Int ' these are the x and y origins of a select box
  37. Global selectflag:Int ' is a selectbox active?
  38.  
  39. Print leastof(5,10)
  40. Print greaterof(5,10)
  41. While Not KeyHit(KEY_ESCAPE)
  42.  
  43. Cls
  44.  
  45. If MouseDown(1)
  46.         If Selectflag = 0
  47.                 selectflag = 1
  48.                 selectx = MouseX()
  49.                 selecty = MouseY()
  50.         EndIf
  51. EndIf
  52.        
  53.  
  54. Local tempobj2:selectobject = New selectobject
  55.  
  56. For tempobj2 = EachIn objects
  57.         tempobj2.draw()
  58. Next
  59.  
  60. If selectflag = 1
  61.         SetColor 0,255,0
  62.         DrawLine selectx, selecty, MouseX(), selecty
  63.         DrawLine MouseX(),selecty, MouseX(),MouseY()
  64.         DrawLine MouseX(),MouseY(), selectx,MouseY()
  65.         DrawLine selectx,MouseY(),selectx,selecty
  66.        
  67.         If Not MouseDown(1)
  68.                 selectflag = 0
  69.                 Local ux:Int = leastof (MouseX(),selectx)
  70.                 Local uy:Int = leastof(MouseY(),selecty)
  71.                 Local lx:Int = greaterof(MouseX(),selectx)
  72.                 Local ly:Int = greaterof(MouseY(),selecty)
  73.                 For tempobj2 = EachIn objects
  74.                 If tempobj2.x > ux And tempobj2.y > uy And tempobj2.x<lx And tempobj2.y <ly
  75.                         tempobj2.selected = 1
  76.                 Else
  77.                         tempobj2.selected = 0
  78.                 EndIf
  79.                 Next
  80.         EndIf
  81.                
  82. EndIf
  83.  
  84. Flip
  85. Wend
  86. 'leastof and greaterof are necessary in case of a leftward or upward drag of the select box
  87.  
  88. Function leastof(n1:Int, n2:Int)  
  89.         Local least:Int
  90.         If n1>n2 least = n2
  91.         If n2>n1 least = n1
  92.         Return least
  93. End Function
  94.  
  95. Function greaterof(n1:Int, n2:Int)
  96.         Local greater:Int
  97.         If n1>n2 greater= n1
  98.         If n2>n1 greater= n2
  99.         Return greater
  100. End Function


Comments :


virtlands(Posted 1+ years ago)

 { The files are actually *.BMX files. }Color Cycling and Procedural Critters :: <font class="tiny"> <a href="http://uploadingit.com/file/xnrveey7fjoc93mk/Color Cycling and Procedural Critters_3089.bmx" target="_blank">http://uploadingit.com/file/xnrveey7fjoc93mk/Color%20Cycling%20and%20Procedural%20Critters_3089.bmx[/url] </font>RTS Drag Effect :: <font class="tiny"> <a href="http://uploadingit.com/file/dmoxb7q8pplmotpz/RTS Drag Select_3088.bmx" target="_blank">http://uploadingit.com/file/dmoxb7q8pplmotpz/RTS%20Drag%20Select_3088.bmx[/url] </font>Image Recursion :: <font class="tiny"> <a href="http://uploadingit.com/file/kv2u1husmv3ulyob/Image Recursion_3087.bmx" target="_blank">http://uploadingit.com/file/kv2u1husmv3ulyob/Image%20Recursion_3087.bmx[/url] </font>[ RTS Drag Effect screenshot: ][img]imagizer.imageshack.us/a/img802/8319/y4bf.html">  [img]imagizer.imageshack.us/a/img571/3012/gjjc.html">


_PJ_(Posted 1+ years ago)

 What might be of note, that whenever dragging a selection rectangle, the change of state between selected and non-selected only ever occurs when the rectangle boundary intersects with the dimensions of the position of the entities.Therefore, depending on how many potential selectable entities there are, it may be quicker to check for this 'collision' rather than continuous checks for the positions of every entity.


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal