core2d 2.10.4 → 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.
@@ -4,35 +4,41 @@ import { Command } from "../Command.mjs";
4
4
  import { Core2D } from "../Core2D.mjs";
5
5
  import { Sprite } from "../Sprite.mjs";
6
6
 
7
- export class ControllableSprite extends Sprite {
8
- constructor() {
9
- super();
10
- this.controller = Core2D.getController();
11
- this.step = 2;
12
- }
13
-
14
- setStep(step) {
15
- this.step = step;
16
- return this;
17
- }
18
-
19
- sync() {
20
- if (this.controller.keyDown(Command.LEFT) && this.left > 0) {
21
- this.x -= this.step;
22
- }
23
-
24
- if (this.controller.keyDown(Command.RIGHT) && this.right < this.scene.width) {
25
- this.x += this.step;
26
- }
27
-
28
- if (this.controller.keyDown(Command.UP) && this.top > 0) {
29
- this.y -= this.step;
30
- }
31
-
32
- if (this.controller.keyDown(Command.DOWN) && this.bottom < this.scene.height) {
33
- this.y += this.step;
34
- }
35
-
36
- return Sprite.prototype.sync.call(this);
37
- }
7
+ export class ControllableSprite extends Sprite {
8
+ constructor() {
9
+ super();
10
+ this.controller = Core2D.getController();
11
+ this.step = 2;
12
+ }
13
+
14
+ setStep(step) {
15
+ this.step = step;
16
+ return this;
17
+ }
18
+
19
+ sync() {
20
+ if (this.controller.keyDown(Command.LEFT) && this.left > 0) {
21
+ this.x -= this.step;
22
+ }
23
+
24
+ if (
25
+ this.controller.keyDown(Command.RIGHT) &&
26
+ this.right < this.scene.width
27
+ ) {
28
+ this.x += this.step;
29
+ }
30
+
31
+ if (this.controller.keyDown(Command.UP) && this.top > 0) {
32
+ this.y -= this.step;
33
+ }
34
+
35
+ if (
36
+ this.controller.keyDown(Command.DOWN) &&
37
+ this.bottom < this.scene.height
38
+ ) {
39
+ this.y += this.step;
40
+ }
41
+
42
+ return Sprite.prototype.sync.call(this);
43
+ }
38
44
  }
@@ -4,42 +4,42 @@ import { Core2D } from "../Core2D.mjs";
4
4
  import { Rect } from "../Rect.mjs";
5
5
  import { Sprite } from "../Sprite.mjs";
6
6
 
7
- export class CursorSprite extends Sprite {
8
- constructor() {
9
- super();
10
- this.hovering = null;
11
- this.pointer = Core2D.getPointer();
12
- this.setSolid();
13
- }
7
+ export class CursorSprite extends Sprite {
8
+ constructor() {
9
+ super();
10
+ this.hovering = null;
11
+ this.pointer = Core2D.getPointer();
12
+ this.setSolid();
13
+ }
14
14
 
15
- check(sprite) {
16
- const FOCUS = new Rect(this.getLeft(), this.getTop(), 1, 1);
17
- return sprite.hasCollision(FOCUS);
18
- }
15
+ check(sprite) {
16
+ const FOCUS = new Rect(this.getLeft(), this.getTop(), 1, 1);
17
+ return sprite.hasCollision(FOCUS);
18
+ }
19
19
 
20
- onCollision(sprite) {
21
- if (sprite.hasTag("clickable") && this.check(sprite)) {
22
- if (this.hovering != sprite) {
23
- if (this.hovering) {
24
- this.hovering.onHoverOut();
25
- }
20
+ onCollision(sprite) {
21
+ if (sprite.hasTag("clickable") && this.check(sprite)) {
22
+ if (this.hovering != sprite) {
23
+ if (this.hovering) {
24
+ this.hovering.onHoverOut();
25
+ }
26
26
 
27
- this.hovering = sprite;
28
- sprite.onHoverIn();
29
- }
30
- }
31
- }
27
+ this.hovering = sprite;
28
+ sprite.onHoverIn();
29
+ }
30
+ }
31
+ }
32
32
 
33
- update() {
34
- this.setPosition(this.pointer);
33
+ update() {
34
+ this.setPosition(this.pointer);
35
35
 
36
- if (this.hovering) {
37
- if (!this.hasCollision(this.hovering)) {
38
- this.hovering.onHoverOut();
39
- this.hovering = null;
40
- } else if (this.pointer.getPush()) {
41
- this.hovering.onClick();
42
- }
43
- }
44
- }
36
+ if (this.hovering) {
37
+ if (!this.hasCollision(this.hovering)) {
38
+ this.hovering.onHoverOut();
39
+ this.hovering = null;
40
+ } else if (this.pointer.getPush()) {
41
+ this.hovering.onClick();
42
+ }
43
+ }
44
+ }
45
45
  }
@@ -3,29 +3,32 @@
3
3
  import { Sprite } from "../Sprite.mjs";
4
4
 
5
5
  export class Fog extends Sprite {
6
- init() {
7
- this.scene.add(new FogLayer()
8
- .setImageId("fogSprite0")
9
- .setSpeedX(-1));
6
+ init() {
7
+ this.scene.add(new FogLayer().setImageId("fogSprite0").setSpeedX(-1));
10
8
 
11
- this.scene.add(new FogLayer()
12
- .setImageId("fogSprite1")
13
- .setSpeedX(1)
14
- .setRight(this.scene.right));
15
- }
9
+ this.scene.add(
10
+ new FogLayer()
11
+ .setImageId("fogSprite1")
12
+ .setSpeedX(1)
13
+ .setRight(this.scene.right)
14
+ );
15
+ }
16
16
  }
17
17
 
18
18
  class FogLayer extends Sprite {
19
- init() {
20
- this.setLayerIndex(2)
21
- .setHeight(this.scene.height)
22
- .setWidth(this.scene.width * 2);
23
- }
19
+ init() {
20
+ this.setLayerIndex(2)
21
+ .setHeight(this.scene.height)
22
+ .setWidth(this.scene.width * 2);
23
+ }
24
24
 
25
- update() {
26
- if (this.speedX < 0 && this.right == this.scene.right || this.speedX > 0 && this.left == this.scene.left) {
27
- const SIGNAL = this.speedX / Math.abs(this.speedX);
28
- this.x -= this.scene.width * SIGNAL;
29
- }
30
- }
25
+ update() {
26
+ if (
27
+ (this.speedX < 0 && this.right == this.scene.right) ||
28
+ (this.speedX > 0 && this.left == this.scene.left)
29
+ ) {
30
+ const SIGNAL = this.speedX / Math.abs(this.speedX);
31
+ this.x -= this.scene.width * SIGNAL;
32
+ }
33
+ }
31
34
  }
@@ -4,71 +4,76 @@ import { Core2D } from "../Core2D.mjs";
4
4
  import { Sprite } from "../Sprite.mjs";
5
5
 
6
6
  export class FontSprite extends Sprite {
7
- constructor(text = "") {
8
- super();
9
- this.font = "";
10
- this.text = text;
11
- }
12
-
13
- render(context) {
14
- Sprite.prototype.render.call(this, context) && this._parse(context);
15
- }
16
-
17
- setFont(font) {
18
- this.font = font;
19
- }
20
-
21
- setText(text) {
22
- this._text = text;
23
- this._parse();
24
- return this;
25
- }
26
-
27
- get text() {
28
- return this._text;
29
- }
30
-
31
- set text(text) {
32
- this.setText(text);
33
- }
34
-
35
- _parse(context) {
36
- const SPACE = 4;
37
- const SPACING = 0;
38
- let height = 0;
39
- let width = 0;
40
- let x = 0;
41
- let y = 0;
42
-
43
- for (let i = 0; i < this._text.length; ++i) {
44
- let character = this._text[i];
45
-
46
- if (character == " ") {
47
- x += SPACE + SPACING;
48
- } else if (character == "\n") {
49
- x = 0;
50
- y += height + SPACING;
51
- } else {
52
- const IMAGE = Core2D.image(character + this.font + "Font");
53
-
54
- if (context) {
55
- context.drawImage(IMAGE, this.x + this.scene.x + x, this.y + this.scene.y + y, IMAGE.width, IMAGE.height);
56
- }
57
-
58
- x += IMAGE.width + SPACING;
59
-
60
- if (x > width) {
61
- width = x;
62
- }
63
-
64
- if (IMAGE.height > height) {
65
- height = IMAGE.height;
66
- }
67
- }
68
- }
69
-
70
- this.setWidth(width);
71
- this.setHeight(y + height);
72
- }
7
+ constructor(text = "") {
8
+ super();
9
+ this.font = "";
10
+ this.text = text;
11
+ }
12
+
13
+ render(context) {
14
+ Sprite.prototype.render.call(this, context) && this._parse(context);
15
+ }
16
+
17
+ setFont(font) {
18
+ this.font = font;
19
+ }
20
+
21
+ setText(text) {
22
+ this._text = text;
23
+ this._parse();
24
+ return this;
25
+ }
26
+
27
+ get text() {
28
+ return this._text;
29
+ }
30
+
31
+ set text(text) {
32
+ this.setText(text);
33
+ }
34
+
35
+ _parse(context) {
36
+ const SPACE = 4;
37
+ const SPACING = 0;
38
+ let height = 0;
39
+ let width = 0;
40
+ let x = 0;
41
+ let y = 0;
42
+
43
+ for (let i = 0; i < this._text.length; ++i) {
44
+ let character = this._text[i];
45
+
46
+ if (character == " ") {
47
+ x += SPACE + SPACING;
48
+ } else if (character == "\n") {
49
+ x = 0;
50
+ y += height + SPACING;
51
+ } else {
52
+ const IMAGE = Core2D.image(character + this.font + "Font");
53
+
54
+ if (context) {
55
+ context.drawImage(
56
+ IMAGE,
57
+ this.x + this.scene.x + x,
58
+ this.y + this.scene.y + y,
59
+ IMAGE.width,
60
+ IMAGE.height
61
+ );
62
+ }
63
+
64
+ x += IMAGE.width + SPACING;
65
+
66
+ if (x > width) {
67
+ width = x;
68
+ }
69
+
70
+ if (IMAGE.height > height) {
71
+ height = IMAGE.height;
72
+ }
73
+ }
74
+ }
75
+
76
+ this.setWidth(width);
77
+ this.setHeight(y + height);
78
+ }
73
79
  }
74
-
@@ -7,137 +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
  }
143
-
@@ -5,30 +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
  }
34
-