so://phie

game-engine

A lightweight JavaScript game engine
git clone git://soophie.de/game-engine

blob: f68e349bf181ab9c38c6893e69089fba4c5ddd62 (1464 bytes)[doc][blame][raw]

1export class Group {

2 constructor() {

3 this.items = [];

4 }

5 update() {

6 for(const item of this.items) {

7 item.update();

8 }

9 }

10 render(g) {

11 for(const item of this.items) {

12 item.render(g);

13 }

14 }

15 causeEvent(type, events) {

16 for(const item of this.items) {

17 item.causeEvent(type, events);

18 }

19 }

20 collide() {

21 for(const i of this.items) {

22 i.items = [];

23 i.collided = false;

24 }

25 const duplicate = [];

26 for(const a of this.items) {

27 for(const b of this.items) {

28 if(!duplicate.includes(b)) {

29 if(a !== b) {

30 let collided = false;

31 if(a.shape instanceof Shape.Rect && b.shape instanceof Shape.Rect) {

32 collided = Collision.collideRect(a, b);

33 }

34 else if(a.shape instanceof Shape.Circle && b.shape instanceof Shape.Circle) {

35 collided = Collision.collideCircle(a, b);

36 }

37 else {

38 collided = Collision.collideRectAndCircle(a, b);

39 }

40 if(!a.collided) {

41 a.collided = collided;

42 }

43 if(!b.collided) {

44 b.collided = collided;

45 }

46 if(collided) {

47 if(!a.items.includes(b)) {

48 a.items.push(b);

49 }

50 if(!b.items.includes(a)) {

51 b.items.push(a);

52 }

53 }

54 }

55 }

56 }

57 duplicate.push(a);

58 }

59 }

60 add(item) {

61 this.items.push(item);

62 }

63 remove(item) {

64 const i = this.items.findIndex(n => n === item);

65 if(i !== -1) {

66 this.items.splice(i, 1);

67 }

68 }

69 [Symbol.iterator]() {

70 return this.items.values();

71 }

72}