microwerx

Entity Component Systems

Apr 11th, 2020
501
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.32 KB | None | 0 0
  1. import sqlite3
  2.  
  3. conn=sqlite3.connect(":memory:")
  4. if not conn:
  5.     print('oh no!')
  6. c=conn.cursor()
  7.  
  8. c.execute('CREATE TABLE entities (entityID integer primary key, desc text)')
  9. c.execute('CREATE TABLE components (componentID integer primary key, name text not null, desc text, tableName text not null)')
  10. c.execute('CREATE TABLE entityComponents (entityID int, componentID int, rowID int)')
  11. c.execute('CREATE TABLE positions (rowID integer primary key, x real,y real)')
  12. c.execute('CREATE TABLE physics (rowID integer primary key, vx real, vy real)')
  13.  
  14.  
  15. t=(None,'Position','Location of entity','positions')
  16. c.execute('INSERT INTO components (componentID,name,desc,tableName) VALUES(?,?,?,?)',t)
  17. positionsID=c.lastrowid
  18.  
  19. t=(None,'Physics','Velocity of entity','physics')
  20. c.execute('INSERT INTO components (componentID, name,desc,tableName) VALUES(?,?,?,?)',t)
  21. physicsID=c.lastrowid
  22.  
  23. # Insert Island Entity
  24. t=(None,'Island')
  25. c.execute('INSERT INTO entities (entityID,desc) VALUES (?,?)',t)
  26. id=c.lastrowid
  27.  
  28. # Insert Yacht Entity
  29. t=(None,'Yacht')
  30. c.execute('INSERT INTO entities (entityID,desc) VALUES (?,?)',t)
  31. id=c.lastrowid
  32.  
  33. # Insert positions component for the last entity
  34. t=(None,-5,5)
  35. c.execute('INSERT INTO positions (rowID,x,y) VALUES (?,?,?)',t)
  36. rowID=c.lastrowid
  37. t=(id,positionsID,rowID)
  38. c.execute('INSERT INTO entityComponents (entityID,componentID,rowID) VALUES (?,?,?)',t)
  39.  
  40. # Insert pysics component for the last entity
  41. t=(None,-1,2)
  42. c.execute('INSERT INTO physics (rowID,vx,vy) VALUES (?,?,?)',t)
  43. rowID=c.lastrowid
  44. t=(id,physicsID,rowID)
  45. c.execute('INSERT INTO entityComponents (entityID,componentID,rowID) VALUES(?,?,?)',t)
  46.  
  47. # Print out summary
  48. print('Entities++++++++')
  49. for row in c.execute('SELECT * FROM entities'):
  50.     print(row)
  51. print('Components++++++')
  52. for row in c.execute('SELECT * FROM components'):
  53.     print(row)
  54. print('Positions+++++++')
  55. for row in c.execute('SELECT * FROM positions'):
  56.     print(row)
  57. print('Physics+++++++++')
  58. for row in c.execute('SELECT * FROM physics'):
  59.     print(row)
  60. print('Data++++++++++++')
  61. for row in c.execute('SELECT * FROM (((entityComponents left join entities on entities.entityID=entityComponents.entityID) left join components on entityComponents.componentID=components.componentID) left join positions on entityComponents.rowID=positions.rowID)'):
  62.     print(row)
Add Comment
Please, Sign In to add comment