ecs.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. import {
  2. web3,
  3. AddEntity,
  4. ApplySystem,
  5. InitializeComponent,
  6. } from "../../clients/bolt-sdk/lib";
  7. import { Direction, Framework } from "../framework";
  8. import { expect } from "chai";
  9. export function ecs(framework: Framework) {
  10. describe("ECS", () => {
  11. it("Add entity 1", async () => {
  12. const addEntity = await AddEntity({
  13. payer: framework.provider.wallet.publicKey,
  14. world: framework.worldPda,
  15. connection: framework.provider.connection,
  16. });
  17. await framework.provider.sendAndConfirm(addEntity.transaction);
  18. framework.entity1Pda = addEntity.entityPda; // Saved for later
  19. });
  20. it("Add entity 2", async () => {
  21. const addEntity = await AddEntity({
  22. payer: framework.provider.wallet.publicKey,
  23. world: framework.worldPda,
  24. connection: framework.provider.connection,
  25. });
  26. await framework.provider.sendAndConfirm(addEntity.transaction);
  27. framework.entity2Pda = addEntity.entityPda; // Saved for later
  28. });
  29. it("Add entity 3", async () => {
  30. const addEntity = await AddEntity({
  31. payer: framework.provider.wallet.publicKey,
  32. world: framework.worldPda,
  33. connection: framework.provider.connection,
  34. });
  35. await framework.provider.sendAndConfirm(addEntity.transaction);
  36. });
  37. it("Add entity 4 (with seed)", async () => {
  38. const addEntity = await AddEntity({
  39. payer: framework.provider.wallet.publicKey,
  40. world: framework.worldPda,
  41. seed: Buffer.from("custom-seed"),
  42. connection: framework.provider.connection,
  43. });
  44. await framework.provider.sendAndConfirm(addEntity.transaction);
  45. framework.entity4Pda = addEntity.entityPda;
  46. });
  47. it("Initialize Velocity Component on Entity 1 (with seed)", async () => {
  48. const initializeComponent = await InitializeComponent({
  49. payer: framework.provider.wallet.publicKey,
  50. entity: framework.entity1Pda,
  51. componentId: framework.exampleComponentVelocity.programId,
  52. seed: "component-velocity",
  53. });
  54. await framework.provider.sendAndConfirm(initializeComponent.transaction);
  55. framework.componentVelocityEntity1Pda = initializeComponent.componentPda; // Saved for later
  56. });
  57. it("Initialize Position Component on Entity 1", async () => {
  58. const initializeComponent = await InitializeComponent({
  59. payer: framework.provider.wallet.publicKey,
  60. entity: framework.entity1Pda,
  61. componentId: framework.exampleComponentPosition.programId,
  62. });
  63. await framework.provider.sendAndConfirm(initializeComponent.transaction);
  64. framework.componentPositionEntity1Pda = initializeComponent.componentPda; // Saved for later
  65. });
  66. it("Initialize Position Component on Entity 2", async () => {
  67. const initializeComponent = await InitializeComponent({
  68. payer: framework.provider.wallet.publicKey,
  69. entity: framework.entity2Pda,
  70. componentId: framework.exampleComponentPosition.programId,
  71. });
  72. await framework.provider.sendAndConfirm(initializeComponent.transaction);
  73. });
  74. it("Initialize Position Component on Entity 4", async () => {
  75. const initializeComponent = await InitializeComponent({
  76. payer: framework.provider.wallet.publicKey,
  77. entity: framework.entity4Pda,
  78. componentId: framework.exampleComponentPosition.programId,
  79. });
  80. await framework.provider.sendAndConfirm(initializeComponent.transaction);
  81. framework.componentPositionEntity4Pda = initializeComponent.componentPda; // Saved for later
  82. });
  83. it("Check Position on Entity 1 is default", async () => {
  84. const position =
  85. await framework.exampleComponentPosition.account.position.fetch(
  86. framework.componentPositionEntity1Pda,
  87. );
  88. expect(position.x.toNumber()).to.equal(0);
  89. expect(position.y.toNumber()).to.equal(0);
  90. expect(position.z.toNumber()).to.equal(0);
  91. });
  92. it("Apply Simple Movement System (Up) on Entity 1", async () => {
  93. const applySystem = await ApplySystem({
  94. authority: framework.provider.wallet.publicKey,
  95. systemId: framework.systemSimpleMovement.programId,
  96. world: framework.worldPda,
  97. entities: [
  98. {
  99. entity: framework.entity1Pda,
  100. components: [
  101. { componentId: framework.exampleComponentPosition.programId },
  102. ],
  103. },
  104. ],
  105. args: {
  106. direction: Direction.Up,
  107. },
  108. });
  109. await framework.provider.sendAndConfirm(applySystem.transaction, [], {
  110. skipPreflight: true,
  111. });
  112. const position =
  113. await framework.exampleComponentPosition.account.position.fetch(
  114. framework.componentPositionEntity1Pda,
  115. );
  116. expect(position.x.toNumber()).to.equal(0);
  117. expect(position.y.toNumber()).to.equal(1);
  118. expect(position.z.toNumber()).to.equal(0);
  119. });
  120. it("Apply Simple Movement System (Right) on Entity 1", async () => {
  121. const applySystem = await ApplySystem({
  122. authority: framework.provider.wallet.publicKey,
  123. systemId: framework.systemSimpleMovement.programId,
  124. world: framework.worldPda,
  125. entities: [
  126. {
  127. entity: framework.entity1Pda,
  128. components: [
  129. { componentId: framework.exampleComponentPosition.programId },
  130. ],
  131. },
  132. ],
  133. args: {
  134. direction: Direction.Right,
  135. },
  136. });
  137. await framework.provider.sendAndConfirm(applySystem.transaction);
  138. const position =
  139. await framework.exampleComponentPosition.account.position.fetch(
  140. framework.componentPositionEntity1Pda,
  141. );
  142. expect(position.x.toNumber()).to.equal(1);
  143. expect(position.y.toNumber()).to.equal(1);
  144. expect(position.z.toNumber()).to.equal(0);
  145. });
  146. it("Apply Fly System on Entity 1", async () => {
  147. const applySystem = await ApplySystem({
  148. authority: framework.provider.wallet.publicKey,
  149. systemId: framework.systemFly.programId,
  150. world: framework.worldPda,
  151. entities: [
  152. {
  153. entity: framework.entity1Pda,
  154. components: [
  155. { componentId: framework.exampleComponentPosition.programId },
  156. ],
  157. },
  158. ],
  159. });
  160. await framework.provider.sendAndConfirm(applySystem.transaction);
  161. const position =
  162. await framework.exampleComponentPosition.account.position.fetch(
  163. framework.componentPositionEntity1Pda,
  164. );
  165. expect(position.x.toNumber()).to.equal(1);
  166. expect(position.y.toNumber()).to.equal(1);
  167. expect(position.z.toNumber()).to.equal(1);
  168. });
  169. it("Apply System Velocity on Entity 1", async () => {
  170. const applySystem = await ApplySystem({
  171. authority: framework.provider.wallet.publicKey,
  172. systemId: framework.systemApplyVelocity.programId,
  173. world: framework.worldPda,
  174. entities: [
  175. {
  176. entity: framework.entity1Pda,
  177. components: [
  178. {
  179. componentId: framework.exampleComponentVelocity.programId,
  180. seed: "component-velocity",
  181. },
  182. { componentId: framework.exampleComponentPosition.programId },
  183. ],
  184. },
  185. ],
  186. });
  187. await framework.provider.sendAndConfirm(applySystem.transaction);
  188. const velocity =
  189. await framework.exampleComponentVelocity.account.velocity.fetch(
  190. framework.componentVelocityEntity1Pda,
  191. );
  192. expect(velocity.x.toNumber()).to.equal(10);
  193. expect(velocity.y.toNumber()).to.equal(0);
  194. expect(velocity.z.toNumber()).to.equal(0);
  195. expect(velocity.lastApplied.toNumber()).to.not.equal(0);
  196. const position =
  197. await framework.exampleComponentPosition.account.position.fetch(
  198. framework.componentPositionEntity1Pda,
  199. );
  200. expect(position.x.toNumber()).to.greaterThan(1);
  201. expect(position.y.toNumber()).to.equal(1);
  202. expect(position.z.toNumber()).to.equal(1);
  203. });
  204. it("Apply System Velocity on Entity 1, with Clock external account", async () => {
  205. const applySystem = await ApplySystem({
  206. authority: framework.provider.wallet.publicKey,
  207. systemId: framework.systemApplyVelocity.programId,
  208. world: framework.worldPda,
  209. entities: [
  210. {
  211. entity: framework.entity1Pda,
  212. components: [
  213. {
  214. componentId: framework.exampleComponentVelocity.programId,
  215. seed: "component-velocity",
  216. },
  217. { componentId: framework.exampleComponentPosition.programId },
  218. ],
  219. },
  220. ],
  221. extraAccounts: [
  222. {
  223. pubkey: new web3.PublicKey(
  224. "SysvarC1ock11111111111111111111111111111111",
  225. ),
  226. isWritable: false,
  227. isSigner: false,
  228. },
  229. ],
  230. });
  231. await framework.provider.sendAndConfirm(applySystem.transaction);
  232. const position =
  233. await framework.exampleComponentPosition.account.position.fetch(
  234. framework.componentPositionEntity1Pda,
  235. );
  236. expect(position.x.toNumber()).to.greaterThan(1);
  237. expect(position.y.toNumber()).to.equal(1);
  238. expect(position.z.toNumber()).to.equal(300);
  239. });
  240. it("Apply Fly System on Entity 4", async () => {
  241. const applySystem = await ApplySystem({
  242. authority: framework.provider.wallet.publicKey,
  243. systemId: framework.systemFly.programId,
  244. world: framework.worldPda,
  245. entities: [
  246. {
  247. entity: framework.entity4Pda,
  248. components: [
  249. { componentId: framework.exampleComponentPosition.programId },
  250. ],
  251. },
  252. ],
  253. });
  254. await framework.provider.sendAndConfirm(applySystem.transaction);
  255. const position =
  256. await framework.exampleComponentPosition.account.position.fetch(
  257. framework.componentPositionEntity4Pda,
  258. );
  259. expect(position.x.toNumber()).to.equal(0);
  260. expect(position.y.toNumber()).to.equal(0);
  261. expect(position.z.toNumber()).to.equal(1);
  262. });
  263. });
  264. }