core2d 2.11.1 → 2.11.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7,136 +7,136 @@ import { Sprite } from "../Sprite.mjs";
7
7
  const PUSHABLE_TAG = "pushable";
8
8
 
9
9
  export class JumperSprite extends Sprite {
10
- constructor(scene) {
11
- super(scene);
12
- this.accelerationY = 0.5;
13
- this.canGoLeft = true;
14
- this.canGoRight = true;
15
- this.canJump = false;
16
- this.controller = Core2D.getController();
17
- this.jumpCommand = Command.A;
18
- this.jumpSpeed = -8;
19
- this.solid = true;
20
- this.step = 2;
21
- }
22
-
23
- setJumpCommand(jumpCommand) {
24
- this.jumpCommand = jumpCommand;
25
- return this;
26
- }
27
-
28
- setJumpSpeed(jumpSpeed) {
29
- this.jumpSpeed = jumpSpeed;
30
- return this;
31
- }
32
-
33
- setStep(step) {
34
- this.step = step;
35
- return this;
36
- }
37
-
38
- jump() {
39
- this.canJump = false;
40
- this.setSpeedY(this.jumpSpeed);
41
- this.onJump();
42
- }
43
-
44
- onCollision(sprite) {
45
- const collision = this.getCollision(sprite);
46
-
47
- if (collision.bottom) {
48
- if (this.speedY > 0) {
49
- this.setSpeedY(0);
50
- this.setBottom(sprite.top - 1);
51
- this.canJump = true;
52
- }
53
-
54
- this.onBottomCollision(sprite);
55
- } else if (collision.top) {
56
- if (this.speedY < 0) {
57
- this.setSpeedY(0);
58
- this.setTop(sprite.bottom + 1);
59
- }
60
-
61
- this.onTopCollision(sprite);
62
- } else if (collision.left) {
63
- if (!sprite.hasTag(PUSHABLE_TAG)) {
64
- this.canGoLeft = false;
65
- }
66
-
67
- this.setLeft(sprite.right + 1);
68
- this.onLeftCollision(sprite);
69
- } else if (collision.right) {
70
- if (!sprite.hasTag(PUSHABLE_TAG)) {
71
- this.canGoRight = false;
72
- }
73
-
74
- this.setRight(sprite.left - 1);
75
- this.onRightCollision(sprite);
76
- }
77
- }
78
-
79
- sync() {
80
- if (this.solid) {
81
- if (this.canJump && this.controller.keyPush(this.jumpCommand)) {
82
- this.jump();
83
- }
84
-
85
- if (this.canGoLeft && this.controller.keyDown(Command.LEFT)) {
86
- this.x -= this.step;
87
- this.canGoRight = true;
88
- this.onLeft();
89
- } else if (this.canGoRight && this.controller.keyDown(Command.RIGHT)) {
90
- this.x += this.step;
91
- this.canGoLeft = true;
92
- this.onRight();
93
- } else {
94
- this.onIdle();
95
- }
96
-
97
- if (!this.collided) {
98
- this.canGoLeft = true;
99
- this.canGoRight = true;
100
- this.canJump = false;
101
- }
102
- }
103
-
104
- return Sprite.prototype.sync.call(this);
105
- }
106
-
107
- onJump() {
108
- // no default behavior
109
- }
110
-
111
- onIdle() {
112
- // no default behavior
113
- }
114
-
115
- onLeft() {
116
- // no default behavior
117
- }
118
-
119
- onRight() {
120
- // no default behavior
121
- }
122
-
123
- onLeftCollision(sprite) {
124
- // no default behavior
125
- return sprite;
126
- }
127
-
128
- onRightCollision(sprite) {
129
- // no default behavior
130
- return sprite;
131
- }
132
-
133
- onTopCollision(sprite) {
134
- // no default behavior
135
- return sprite;
136
- }
137
-
138
- onBottomCollision(sprite) {
139
- // no default behavior
140
- return sprite;
141
- }
10
+ constructor(scene) {
11
+ super(scene);
12
+ this.accelerationY = 0.5;
13
+ this.canGoLeft = true;
14
+ this.canGoRight = true;
15
+ this.canJump = false;
16
+ this.controller = Core2D.getController();
17
+ this.jumpCommand = Command.A;
18
+ this.jumpSpeed = -8;
19
+ this.solid = true;
20
+ this.step = 2;
21
+ }
22
+
23
+ setJumpCommand(jumpCommand) {
24
+ this.jumpCommand = jumpCommand;
25
+ return this;
26
+ }
27
+
28
+ setJumpSpeed(jumpSpeed) {
29
+ this.jumpSpeed = jumpSpeed;
30
+ return this;
31
+ }
32
+
33
+ setStep(step) {
34
+ this.step = step;
35
+ return this;
36
+ }
37
+
38
+ jump() {
39
+ this.canJump = false;
40
+ this.setSpeedY(this.jumpSpeed);
41
+ this.onJump();
42
+ }
43
+
44
+ onCollision(sprite) {
45
+ const collision = this.getCollision(sprite);
46
+
47
+ if (collision.bottom) {
48
+ if (this.speedY > 0) {
49
+ this.setSpeedY(0);
50
+ this.setBottom(sprite.top - 1);
51
+ this.canJump = true;
52
+ }
53
+
54
+ this.onBottomCollision(sprite);
55
+ } else if (collision.top) {
56
+ if (this.speedY < 0) {
57
+ this.setSpeedY(0);
58
+ this.setTop(sprite.bottom + 1);
59
+ }
60
+
61
+ this.onTopCollision(sprite);
62
+ } else if (collision.left) {
63
+ if (!sprite.hasTag(PUSHABLE_TAG)) {
64
+ this.canGoLeft = false;
65
+ }
66
+
67
+ this.setLeft(sprite.right + 1);
68
+ this.onLeftCollision(sprite);
69
+ } else if (collision.right) {
70
+ if (!sprite.hasTag(PUSHABLE_TAG)) {
71
+ this.canGoRight = false;
72
+ }
73
+
74
+ this.setRight(sprite.left - 1);
75
+ this.onRightCollision(sprite);
76
+ }
77
+ }
78
+
79
+ sync() {
80
+ if (this.solid) {
81
+ if (this.canJump && this.controller.keyPush(this.jumpCommand)) {
82
+ this.jump();
83
+ }
84
+
85
+ if (this.canGoLeft && this.controller.keyDown(Command.LEFT)) {
86
+ this.x -= this.step;
87
+ this.canGoRight = true;
88
+ this.onLeft();
89
+ } else if (this.canGoRight && this.controller.keyDown(Command.RIGHT)) {
90
+ this.x += this.step;
91
+ this.canGoLeft = true;
92
+ this.onRight();
93
+ } else {
94
+ this.onIdle();
95
+ }
96
+
97
+ if (!this.collided) {
98
+ this.canGoLeft = true;
99
+ this.canGoRight = true;
100
+ this.canJump = false;
101
+ }
102
+ }
103
+
104
+ return Sprite.prototype.sync.call(this);
105
+ }
106
+
107
+ onJump() {
108
+ // no default behavior
109
+ }
110
+
111
+ onIdle() {
112
+ // no default behavior
113
+ }
114
+
115
+ onLeft() {
116
+ // no default behavior
117
+ }
118
+
119
+ onRight() {
120
+ // no default behavior
121
+ }
122
+
123
+ onLeftCollision(sprite) {
124
+ // no default behavior
125
+ return sprite;
126
+ }
127
+
128
+ onRightCollision(sprite) {
129
+ // no default behavior
130
+ return sprite;
131
+ }
132
+
133
+ onTopCollision(sprite) {
134
+ // no default behavior
135
+ return sprite;
136
+ }
137
+
138
+ onBottomCollision(sprite) {
139
+ // no default behavior
140
+ return sprite;
141
+ }
142
142
  }
@@ -5,29 +5,29 @@ import { Core2D } from "../Core2D.mjs";
5
5
  import { Sprite } from "../Sprite.mjs";
6
6
 
7
7
  export class RandomRectTransition extends Sprite {
8
- init() {
9
- this.columns = 16;
10
- this.rows = 8;
11
- this.setColor(Color.Black);
12
- this.setWidth(this.scene.width / this.columns);
13
- this.setHeight(this.scene.height / this.rows);
14
- this.rects = [];
8
+ init() {
9
+ this.columns = 16;
10
+ this.rows = 8;
11
+ this.setColor(Color.Black);
12
+ this.setWidth(this.scene.width / this.columns);
13
+ this.setHeight(this.scene.height / this.rows);
14
+ this.rects = [];
15
15
 
16
- for (let i = 0; i < this.columns * this.rows; ++i) {
17
- this.rects.push(i);
18
- }
19
- }
16
+ for (let i = 0; i < this.columns * this.rows; ++i) {
17
+ this.rects.push(i);
18
+ }
19
+ }
20
20
 
21
- sync() {
22
- if (!this.rects.length) {
23
- return true;
24
- }
21
+ sync() {
22
+ if (!this.rects.length) {
23
+ return true;
24
+ }
25
25
 
26
- const number = this.rects.splice(Core2D.random(this.rects.length - 1), 1);
27
- const column = number % this.columns;
28
- const row = Math.floor(number / this.columns);
29
- this.x = column * this.width;
30
- this.y = row * this.height;
31
- return Sprite.prototype.sync.call(this);
32
- }
26
+ const number = this.rects.splice(Core2D.random(this.rects.length - 1), 1);
27
+ const column = number % this.columns;
28
+ const row = Math.floor(number / this.columns);
29
+ this.x = column * this.width;
30
+ this.y = row * this.height;
31
+ return Sprite.prototype.sync.call(this);
32
+ }
33
33
  }
@@ -9,54 +9,54 @@ const STAR_BLINK_CHANCE = 10;
9
9
  const STAR_DENSITY = 100;
10
10
 
11
11
  class Star extends Sprite {
12
- init() {
13
- this.addTag("star").setBoundary();
14
- }
15
-
16
- offBoundary() {
17
- if (this.right < this.scene.left) {
18
- this.setLeft(this.scene.right);
19
- } else if (this.left > this.scene.right) {
20
- this.setRight(this.scene.left);
21
- }
22
-
23
- if (this.bottom < this.scene.top) {
24
- this.setTop(this.scene.bottom);
25
- } else if (this.top > this.scene.bottom) {
26
- this.setBottom(this.scene.top);
27
- }
28
- }
29
-
30
- update() {
31
- this.setVisible(Core2D.random(STAR_BLINK_CHANCE * this.width) > 0);
32
- }
12
+ init() {
13
+ this.addTag("star").setBoundary();
14
+ }
15
+
16
+ offBoundary() {
17
+ if (this.right < this.scene.left) {
18
+ this.setLeft(this.scene.right);
19
+ } else if (this.left > this.scene.right) {
20
+ this.setRight(this.scene.left);
21
+ }
22
+
23
+ if (this.bottom < this.scene.top) {
24
+ this.setTop(this.scene.bottom);
25
+ } else if (this.top > this.scene.bottom) {
26
+ this.setBottom(this.scene.top);
27
+ }
28
+ }
29
+
30
+ update() {
31
+ this.setVisible(Core2D.random(STAR_BLINK_CHANCE * this.width) > 0);
32
+ }
33
33
  }
34
34
 
35
35
  export class Starfield extends Sprite {
36
- constructor() {
37
- super();
38
- this.colors = [Color.White];
39
- }
40
-
41
- init() {
42
- for (let i = 0; i < STAR_DENSITY; ++i) {
43
- const SIZE = 1 + Core2D.random(1);
44
-
45
- this.scene.add(
46
- new Star()
47
- .setColor(this.colors[Core2D.random(this.colors.length - 1)])
48
- .setX(Core2D.random(this.scene.width))
49
- .setY(Core2D.random(this.scene.height))
50
- .setWidth(SIZE)
51
- .setHeight(SIZE)
52
- .setSpeedX(this.speedX * SIZE * SPEED_SIZE_RATIO)
53
- .setSpeedY(this.speedY * SIZE * SPEED_SIZE_RATIO)
54
- );
55
- }
56
- }
57
-
58
- setColors(colors) {
59
- this.colors = colors;
60
- return this;
61
- }
36
+ constructor() {
37
+ super();
38
+ this.colors = [Color.White];
39
+ }
40
+
41
+ init() {
42
+ for (let i = 0; i < STAR_DENSITY; ++i) {
43
+ const SIZE = 1 + Core2D.random(1);
44
+
45
+ this.scene.add(
46
+ new Star()
47
+ .setColor(this.colors[Core2D.random(this.colors.length - 1)])
48
+ .setX(Core2D.random(this.scene.width))
49
+ .setY(Core2D.random(this.scene.height))
50
+ .setWidth(SIZE)
51
+ .setHeight(SIZE)
52
+ .setSpeedX(this.speedX * SIZE * SPEED_SIZE_RATIO)
53
+ .setSpeedY(this.speedY * SIZE * SPEED_SIZE_RATIO)
54
+ );
55
+ }
56
+ }
57
+
58
+ setColors(colors) {
59
+ this.colors = colors;
60
+ return this;
61
+ }
62
62
  }