March 05, 2021, 06:54:25 AM

Author Topic: [bmx] LinkObjects Module by JoshK [ 1+ years ago ]  (Read 458 times)

Offline BlitzBot

[bmx] LinkObjects Module by JoshK [ 1+ years ago ]
« on: June 29, 2017, 12:28:40 AM »
Title : LinkObjects Module
Author : JoshK
Posted : 1+ years ago

Description : Sometimes you need to store an object storing information about the interaction between two other objects.  A linear search for the object pair would be very slow.  This module allows you to link to objects in a way that is fast enough for heavy usage in real time.

LinkObjects(o1,o2,o3) will link object o1 and o2 with o3.  When ObjectsLink(o1,o2) is called, o3 will be returned.  This routine is fast enough to be used in real-time many times per frame.


Code :
Code: BlitzMax
  1. Import brl.map
  2.  
  3. Rem
  4. bbdoc:
  5. EndRem
  6. Module leadwerks.LinkObjects
  7.  
  8. Type TObjectLink
  9.         Global map:TMap=New TMap
  10.  
  11.         Field o1:Object
  12.         Field o2:Object
  13.  
  14.         Method Compare:Int(with:Object)
  15.                 p2:TObjectLink=TObjectLink(with)
  16.                 If o1.compare(p2.o1)=0
  17.                         If o2.compare(p2.o2)>0 Return 1
  18.                         If o2.compare(p2.o2)<0 Return -1
  19.                 Else
  20.                         If o1.compare(p2.o1)>0 Return 1
  21.                         If o1.compare(p2.o1)<0 Return -1
  22.                 EndIf
  23.                 Return 0
  24.         EndMethod
  25.        
  26.         Method Destroy()
  27.                 o1=Null
  28.                 o2=Null
  29.         EndMethod
  30.        
  31.         Method Delete()
  32.                 Destroy()
  33.         EndMethod
  34.        
  35.         Function Create:TObjectLink(o1:Object,o2:Object)
  36.                 If o1=o2 Return Null
  37.                 p:TObjectLink=New TObjectLink
  38.                 p.o1=o1
  39.                 p.o2=o2
  40.                 If p.o1.compare(o2)<0
  41.                         o3:Object=p.o2
  42.                         p.o2=p.o1
  43.                         p.o1=o3
  44.                 EndIf
  45.                 Return p
  46.         EndFunction
  47.        
  48. EndType
  49.  
  50. Rem
  51. bbdoc:
  52. EndRem
  53. Function ClearObjectLinks(o:Object)
  54.         For ol:TObjectLink=EachIn TObjectLink.map.keys()
  55.                 If ol.o1=o Or ol.o2=o TObjectLink.map.remove ol
  56.         Next
  57. EndFunction
  58.  
  59. Rem
  60. bbdoc:
  61. EndRem
  62. Function LinkObjects(o1:Object,o2:Object,link:Object)
  63.         p:TObjectLink=TObjectLink.Create(o1,o2)
  64.         TObjectLink.map.Insert p,link
  65. EndFunction
  66.  
  67. Rem
  68. bbdoc:
  69. EndRem
  70. Function ObjectsLink:Object(o1:Object,o2:Object)
  71.         p:TObjectLink=TObjectLink.Create(o1,o2)
  72.         o:Object=TObjectLink.map.ValueForKey(p)
  73.         Return o
  74. EndFunction


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal