Advertisement
NikP98

Pixi Debug

Apr 13th, 2024 (edited)
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.03 KB | Source Code | 0 0
  1. =================
  2. === App Class ===
  3. =================
  4.  
  5. import { Application, Assets } from 'pixi.js';
  6. import { Viewport } from './Viewport';
  7.  
  8. export class App extends Application {
  9. static instance = null;
  10.  
  11. constructor() {
  12. if (App.instance) return App.instance;
  13.  
  14. super();
  15.  
  16. App.instance = this;
  17.  
  18. Assets.addBundle('characters', [
  19. { alias: 'man', src: '/man.json' },
  20. { alias: 'peep', src: '/peep.json' }
  21. ]);
  22.  
  23. this.mouseLocation;
  24.  
  25. this.viewport = null;
  26. }
  27.  
  28. async startApp(options) {
  29. await this.init(options);
  30. await Assets.init({ manifest });
  31.  
  32. const textures = await Assets.loadBundle('characters');
  33.  
  34. this.mouseLocation = this.renderer.events.pointer.global;
  35. this.viewport = new Viewport({ app: this });
  36.  
  37. this.stage.addChild(this.viewport);
  38.  
  39. return {
  40. textures
  41. };
  42. }
  43.  
  44. addChild(child) {
  45. if (!child.update) return;
  46. if (!this.ticker) return;
  47. if (!this.viewport) return;
  48. console.log(child);
  49. this.viewport.addChild(child);
  50.  
  51. this.ticker.add((e) => child.update(e));
  52. }
  53.  
  54. destroy() {
  55. this.destroy();
  56. App.instance = null;
  57. }
  58. }
  59.  
  60. const manifest = {
  61. bundles: [
  62. {
  63. name: 'characters',
  64. assets: [
  65. {
  66. alias: 'man',
  67. src: '/man.json'
  68. },
  69. { alias: 'peep', src: '/peep.json' }
  70. ]
  71. }
  72. ]
  73. };
  74.  
  75. ====================
  76. === Parent Class ===
  77. ====================
  78. import { AnimatedSprite, Assets, Container } from 'pixi.js';
  79. import { App } from './App';
  80. import { Vector2D } from './Vector2D';
  81.  
  82. export class Entity extends AnimatedSprite {
  83. static children = [];
  84.  
  85. constructor({ textures, variant = 'peep', index }) {
  86. super(textures[variant].animations['stand-down/tile']);
  87. this.app = new App();
  88.  
  89. this.skin = textures[variant]; // Get textures from bundle
  90.  
  91. this.index = index || Entity.children.length;
  92.  
  93. this.origin = new Vector2D();
  94. this.coordinates = new Vector2D();
  95. this.direction = Vector2D.random();
  96. this.velocity = new Vector2D();
  97. this.acceleration = new Vector2D();
  98.  
  99. this.anchor.set(0.5);
  100.  
  101. Entity.children.push(this);
  102. this.app.addChild(this);
  103. }
  104.  
  105. setPosition(x = 0, y = 0) {
  106. this.x = x;
  107. this.y = y;
  108. this.coordinates.set(x, y);
  109. }
  110.  
  111. setPosFromObject(v) {
  112. this.setPosition(v.x, v.y);
  113. }
  114.  
  115. moveTowards(target) {
  116. this.direction = target.sub(this.coordinates).normalize();
  117. this.velocity.setFromObject(direction);
  118. }
  119.  
  120. // Add methods for changing animation based on movement here, e.g.,
  121. setAnimation(animationName) {
  122. if (!this.skin.animations[animationName]) return;
  123. if (this.textures === this.skin.animations[animationName]) return;
  124.  
  125. this.textures = this.skin.animations[animationName];
  126.  
  127. this.play();
  128. }
  129. }
  130.  
  131. =======================
  132. === Inherited Class ===
  133. =======================
  134.  
  135. import { AnimatedSprite, Point, Graphics, Color, loadImageBitmap } from 'pixi.js';
  136. import { Entity } from './Entity';
  137. import { Vector2D } from './Vector2D';
  138.  
  139. export class Peep extends Entity {
  140. CURSOR_RADIUS = 100;
  141.  
  142. constructor({ textures }) {
  143. super({ textures });
  144.  
  145. // === State management ===
  146. this.origin.set(Math.random() * this.app.screen.width, Math.random() * this.app.screen.height);
  147. this.setPosition(this.origin.x, this.origin.y);
  148.  
  149. this.state = 'repel'; // Current state (idle, wander, pause)
  150.  
  151. this.animationMap = {
  152. motion: {
  153. stand: 'stand-',
  154. run: 'walk-'
  155. },
  156. direction: {
  157. right: 'right/tile',
  158. left: 'left/tile',
  159. down: 'down/tile',
  160. up: 'up/tile'
  161. }
  162. };
  163.  
  164. this.directionSet = ['left', 'down', 'right', 'up'];
  165.  
  166. this.revDirectionSet = ['right', 'up', 'left', 'down'];
  167. }
  168.  
  169. update(e) {
  170. // console.log(this.texture.label, this.currentFrame);
  171.  
  172. switch (this.state) {
  173. case 'wander':
  174. this.wanderUpdate(e.deltaTime);
  175. break;
  176. case 'follow':
  177. this.followUpdate(e.deltaTime);
  178. break;
  179. case 'repel':
  180. this.repelUpdate(e.deltaTime);
  181. break;
  182. case 'return':
  183. this.returnToOrigin(e.deltaTime);
  184. break;
  185. case 'pause':
  186. this.stand();
  187. break;
  188. default:
  189. break;
  190. }
  191.  
  192. this.zIndex = Math.floor(this.y);
  193. }
  194.  
  195. // TODO: Implement this and include animation updates
  196. switchState(newState) {
  197. this.state = newState;
  198. return;
  199. // this.origin = { x: this.x, y: this.y };
  200. }
  201.  
  202. // wanderUpdate(elapsed) {
  203. // // Update direction with some randomness
  204. // if (elapsed > 16.5) {
  205. // this.direction += (Math.random() - 0.5) * 0.5; // Change direction slightly
  206. // this.lastDirectionChange = elapsed;
  207. // }
  208.  
  209. // // Assuming speed is in pixels-per-second:
  210. // const dx = (Math.cos(this.direction) * this.speed * elapsed) / 1000;
  211. // const dy = (Math.sin(this.direction) * this.speed * elapsed) / 1000;
  212. // // Optimized animation selection
  213. // const dominantDirection =
  214. // Math.abs(dx) > Math.abs(dy) ? (dx > 0 ? 'right' : 'left') : dy > 0 ? 'down' : 'up';
  215. // this.x = Math.max(0, Math.min(this.app.screen.width, this.x + dx));
  216. // this.y = Math.max(0, Math.min(this.app.screen.height, this.y + dy));
  217. // this.setAnimation(this.animationMap.direction[dominantDirection]);
  218. // }
  219.  
  220. // followUpdate(mouseLocation, elapsed) {
  221. // const mouseCoords = new Vector2D.fromData(mouseLocation);
  222.  
  223. // const delta = mouseCoords.subtract(this);
  224. // const dx = mouseLocation.x - this.x;
  225. // const dy = mouseLocation.y - this.y;
  226. // const distance = Math.sqrt(dx * dx + dy * dy);
  227.  
  228. // if (distance > 100) {
  229. // } else {
  230. // const moveFactor = Math.min(distance / 100, (this.speed * elapsed) / 10000);
  231. // this.x += (dx + 1 / distance) * moveFactor;
  232. // this.y += (dy + 1 / distance) * moveFactor;
  233. // if (!this.animationSpeed) this.animationSpeed = 0.09;
  234. // }
  235.  
  236. // let dominantDirection = (Math.abs(dy) > Math.abs(dx) && (dy > 0 ? 'down' : 'up')) || '';
  237. // dominantDirection += (Math.abs(dx) > Math.abs(dy) && (dx > 0 ? 'right' : 'left')) || '';
  238. // this.setAnimation(this.animationMap.direction[dominantDirection]);
  239. // }
  240.  
  241. repelUpdate(elapsed) {
  242. const mouseCoords = Vector2D.fromObject(this.app.mouseLocation);
  243. if (!mouseCoords.multiply(1).magnitude()) mouseCoords.set(-200, -200);
  244.  
  245. const deltaFromMouse = Vector2D.subtract(mouseCoords, this.coordinates);
  246. const distanceFromMouse = deltaFromMouse.magnitude();
  247.  
  248. const distanceFromOrigin = Vector2D.subtract(this.origin, this.coordinates).magnitude();
  249.  
  250. const quadrantFromMouse = Vector2D.quadrant(deltaFromMouse);
  251. const directionQuadrant = Vector2D.quadrant(this.direction);
  252.  
  253. let motion = 'stand';
  254.  
  255. let dominantDirection = this.directionSet[directionQuadrant];
  256.  
  257. /*
  258. ================================
  259. ======== MOVEMENT LOGIC ========
  260. ================================
  261.  
  262. If entity is farther than CURSOR_RADIUS (100)
  263. from mouse, it stands.
  264.  
  265. If the mouse between CURSOR_RADIUS + 100
  266. and CURSOR RADIUS away from the entity,
  267. it looks at the cursor. (e.g. 200 > distance >= 100)
  268. */
  269.  
  270. if (distanceFromOrigin >= 10 && distanceFromMouse > this.CURSOR_RADIUS + 200) {
  271. this.switchState('return');
  272. this.velocity.set(0, 0);
  273. return;
  274. } else if (
  275. distanceFromMouse < this.CURSOR_RADIUS + 100 &&
  276. distanceFromMouse >= this.CURSOR_RADIUS
  277. ) {
  278. this.direction.setFromObject(deltaFromMouse.normalize());
  279. dominantDirection = this.directionSet[quadrantFromMouse];
  280. } else if (distanceFromMouse < this.CURSOR_RADIUS) {
  281. this.direction.setFromObject(deltaFromMouse.normalize());
  282. dominantDirection = this.revDirectionSet[quadrantFromMouse];
  283.  
  284. if (this.velocity.magnitude() > 0) motion = 'walk';
  285.  
  286. this.velocity.setFromObject(this.direction.multiply(elapsed));
  287.  
  288. const target = Vector2D.subtract(this.coordinates, this.velocity);
  289.  
  290. this.setPosition(target.x, target.y);
  291. }
  292. this.setAnimation(`${motion}-${dominantDirection}/tile`);
  293. }
  294.  
  295. returnToOrigin(elapsed) {
  296. const mouseCoords = Vector2D.fromObject(this.app.mouseLocation);
  297.  
  298. const deltaFromOrigin = Vector2D.subtract(this.origin, this.coordinates);
  299.  
  300. const distanceFromMouse = Vector2D.subtract(mouseCoords, this.coordinates).magnitude();
  301. const distanceFromOrigin = deltaFromOrigin.magnitude();
  302.  
  303. const directionQuadrant = Vector2D.quadrant(this.direction.angle());
  304. const originQuadrant = Vector2D.quadrant(deltaFromOrigin);
  305.  
  306. let motion = 'walk';
  307.  
  308. let dominantDirection = this.directionSet[directionQuadrant];
  309.  
  310. if (distanceFromOrigin <= 10 || distanceFromMouse < this.CURSOR_RADIUS) {
  311. this.switchState('repel');
  312. this.velocity.set(0, 0);
  313. return;
  314. }
  315. this.velocity.setFromObject(Vector2D.multiply(this.direction, elapsed));
  316. this.direction.setFromObject(deltaFromOrigin.normalize());
  317.  
  318. const target = Vector2D.add(this.coordinates, this.velocity);
  319. dominantDirection = this.directionSet[originQuadrant];
  320.  
  321. this.setPosition(target.x, target.y);
  322. this.setAnimation(`${motion}-${dominantDirection}/tile`);
  323. }
  324.  
  325. stand() {
  326. if (this.state === 'pause') return;
  327. this.animationSpeed = 0;
  328. this.state = 'pause';
  329. }
  330.  
  331. resume() {
  332. if (this.state !== 'pause') return;
  333. this.animationSpeed = 0.09;
  334. this.state = 'follow'; // Resume wandering by default
  335. }
  336. }
  337.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement