ecs.ts 11 KB

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