core2d 2.10.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.
- package/LICENSE +21 -0
- package/README.md +62 -0
- package/package.json +50 -0
- package/src/ACL.mjs +72 -0
- package/src/Animation.mjs +56 -0
- package/src/Axis.mjs +10 -0
- package/src/ButtonLayout.mjs +43 -0
- package/src/ButtonLayoutMap.mjs +5 -0
- package/src/Color.mjs +154 -0
- package/src/Command.mjs +16 -0
- package/src/CompositeOperations.mjs +15 -0
- package/src/Controller.mjs +67 -0
- package/src/Core2D.mjs +135 -0
- package/src/Direction.mjs +30 -0
- package/src/Engine.mjs +467 -0
- package/src/FontFamily.mjs +9 -0
- package/src/Frame.mjs +26 -0
- package/src/GamePad.mjs +50 -0
- package/src/Input.mjs +123 -0
- package/src/Key.mjs +22 -0
- package/src/KeyMap.mjs +25 -0
- package/src/Keyboard.mjs +34 -0
- package/src/Mouse.mjs +34 -0
- package/src/Point.mjs +38 -0
- package/src/Pointer.mjs +44 -0
- package/src/Rect.mjs +127 -0
- package/src/RenderableList.mjs +19 -0
- package/src/Scene.mjs +108 -0
- package/src/Sound.mjs +138 -0
- package/src/Sprite.mjs +368 -0
- package/src/Static.mjs +77 -0
- package/src/TextSprite.mjs +144 -0
- package/src/Touch.mjs +40 -0
- package/src/Transition.mjs +21 -0
- package/src/plugin/BaseTile.mjs +10 -0
- package/src/plugin/ClickableSprite.mjs +24 -0
- package/src/plugin/ControllableSprite.mjs +38 -0
- package/src/plugin/CursorSprite.mjs +45 -0
- package/src/plugin/Fog.mjs +31 -0
- package/src/plugin/FontSprite.mjs +74 -0
- package/src/plugin/JumperSprite.mjs +143 -0
- package/src/plugin/RandomRectTransition.mjs +34 -0
- package/src/plugin/Starfield.mjs +60 -0
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import { Core2D } from "../Core2D.mjs";
|
|
4
|
+
import { Rect } from "../Rect.mjs";
|
|
5
|
+
import { Sprite } from "../Sprite.mjs";
|
|
6
|
+
|
|
7
|
+
export class CursorSprite extends Sprite {
|
|
8
|
+
constructor() {
|
|
9
|
+
super();
|
|
10
|
+
this.hovering = null;
|
|
11
|
+
this.pointer = Core2D.getPointer();
|
|
12
|
+
this.setSolid();
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
check(sprite) {
|
|
16
|
+
const FOCUS = new Rect(this.getLeft(), this.getTop(), 1, 1);
|
|
17
|
+
return sprite.hasCollision(FOCUS);
|
|
18
|
+
}
|
|
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
|
+
}
|
|
26
|
+
|
|
27
|
+
this.hovering = sprite;
|
|
28
|
+
sprite.onHoverIn();
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
update() {
|
|
34
|
+
this.setPosition(this.pointer);
|
|
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
|
+
}
|
|
45
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import { Sprite } from "../Sprite.mjs";
|
|
4
|
+
|
|
5
|
+
export class Fog extends Sprite {
|
|
6
|
+
init() {
|
|
7
|
+
this.scene.add(new FogLayer()
|
|
8
|
+
.setImageId("fogSprite0")
|
|
9
|
+
.setSpeedX(-1));
|
|
10
|
+
|
|
11
|
+
this.scene.add(new FogLayer()
|
|
12
|
+
.setImageId("fogSprite1")
|
|
13
|
+
.setSpeedX(1)
|
|
14
|
+
.setRight(this.scene.right));
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
class FogLayer extends Sprite {
|
|
19
|
+
init() {
|
|
20
|
+
this.setLayerIndex(2)
|
|
21
|
+
.setHeight(this.scene.height)
|
|
22
|
+
.setWidth(this.scene.width * 2);
|
|
23
|
+
}
|
|
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
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import { Core2D } from "../Core2D.mjs";
|
|
4
|
+
import { Sprite } from "../Sprite.mjs";
|
|
5
|
+
|
|
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
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import { Command } from "../Command.mjs";
|
|
4
|
+
import { Core2D } from "../Core2D.mjs";
|
|
5
|
+
import { Sprite } from "../Sprite.mjs";
|
|
6
|
+
|
|
7
|
+
const PUSHABLE_TAG = "pushable";
|
|
8
|
+
|
|
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
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import { Color } from "../Color.mjs";
|
|
4
|
+
import { Core2D } from "../Core2D.mjs";
|
|
5
|
+
import { Sprite } from "../Sprite.mjs";
|
|
6
|
+
|
|
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 = [];
|
|
15
|
+
|
|
16
|
+
for (let i = 0; i < this.columns * this.rows; ++i) {
|
|
17
|
+
this.rects.push(i);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
sync() {
|
|
22
|
+
if (!this.rects.length) {
|
|
23
|
+
return true;
|
|
24
|
+
}
|
|
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
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import { Color } from "../Color.mjs";
|
|
4
|
+
import { Core2D } from "../Core2D.mjs";
|
|
5
|
+
import { Sprite } from "../Sprite.mjs";
|
|
6
|
+
|
|
7
|
+
const SPEED_SIZE_RATIO = 0.25;
|
|
8
|
+
const STAR_BLINK_CHANCE = 10;
|
|
9
|
+
const STAR_DENSITY = 100;
|
|
10
|
+
|
|
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
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
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(new Star()
|
|
46
|
+
.setColor(this.colors[Core2D.random(this.colors.length - 1)])
|
|
47
|
+
.setX(Core2D.random(this.scene.width))
|
|
48
|
+
.setY(Core2D.random(this.scene.height))
|
|
49
|
+
.setWidth(SIZE)
|
|
50
|
+
.setHeight(SIZE)
|
|
51
|
+
.setSpeedX(this.speedX * SIZE * SPEED_SIZE_RATIO)
|
|
52
|
+
.setSpeedY(this.speedY * SIZE * SPEED_SIZE_RATIO));
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
setColors(colors) {
|
|
57
|
+
this.colors = colors;
|
|
58
|
+
return this;
|
|
59
|
+
}
|
|
60
|
+
}
|