summaryrefslogtreecommitdiff
path: root/src/engine/components
diff options
context:
space:
mode:
Diffstat (limited to 'src/engine/components')
-rw-r--r--src/engine/components/ComponentNames.ts2
-rw-r--r--src/engine/components/Highlight.ts9
-rw-r--r--src/engine/components/LambdaSpawn.ts29
-rw-r--r--src/engine/components/Sprite.ts13
-rw-r--r--src/engine/components/Text.ts22
-rw-r--r--src/engine/components/index.ts2
6 files changed, 72 insertions, 5 deletions
diff --git a/src/engine/components/ComponentNames.ts b/src/engine/components/ComponentNames.ts
index 5e9b589..1f41d18 100644
--- a/src/engine/components/ComponentNames.ts
+++ b/src/engine/components/ComponentNames.ts
@@ -8,4 +8,6 @@ export namespace ComponentNames {
export const Interactable = "Interactable";
export const Pushable = "Pushable";
export const Colliding = "Colliding";
+ export const LambdaSpawn = "LambdaSpawn";
+ export const Text = "Text";
}
diff --git a/src/engine/components/Highlight.ts b/src/engine/components/Highlight.ts
index 5875057..66ec74b 100644
--- a/src/engine/components/Highlight.ts
+++ b/src/engine/components/Highlight.ts
@@ -1,4 +1,5 @@
import { Component, ComponentNames } from ".";
+import { Direction } from "../interfaces";
export class Highlight extends Component {
public isHighlighted: boolean;
@@ -6,8 +7,8 @@ export class Highlight extends Component {
private onUnhighlight: Function;
constructor(
- onHighlight: Function,
- onUnhighlight: Function,
+ onHighlight: (direction: Direction) => void,
+ onUnhighlight: () => void,
isHighlighted: boolean = false,
) {
super(ComponentNames.Highlight);
@@ -17,10 +18,10 @@ export class Highlight extends Component {
this.onUnhighlight = onUnhighlight;
}
- public highlight() {
+ public highlight(direction: Direction) {
if (!this.isHighlighted) {
this.isHighlighted = true;
- this.onHighlight();
+ this.onHighlight(direction);
}
}
diff --git a/src/engine/components/LambdaSpawn.ts b/src/engine/components/LambdaSpawn.ts
new file mode 100644
index 0000000..c45092a
--- /dev/null
+++ b/src/engine/components/LambdaSpawn.ts
@@ -0,0 +1,29 @@
+import { Component, ComponentNames } from ".";
+import { Direction } from "../interfaces";
+
+export class LambdaSpawn extends Component {
+ public direction: Direction | null;
+ public spawnsLeft: number;
+ public code: string = "";
+
+ constructor(
+ spawnsLeft: number,
+ code: string,
+ direction: Direction | null = null,
+ ) {
+ super(ComponentNames.LambdaSpawn);
+
+ this.spawnsLeft = spawnsLeft;
+ this.direction = direction;
+ this.code = code;
+ }
+
+ public spawn(direction: Direction) {
+ if (this.spawnsLeft <= 0) {
+ return;
+ }
+
+ this.direction = direction;
+ this.spawnsLeft -= 1;
+ }
+}
diff --git a/src/engine/components/Sprite.ts b/src/engine/components/Sprite.ts
index 82d7011..c623bac 100644
--- a/src/engine/components/Sprite.ts
+++ b/src/engine/components/Sprite.ts
@@ -46,7 +46,7 @@ export class Sprite extends Component {
}
public draw(ctx: CanvasRenderingContext2D, drawArgs: DrawArgs) {
- const { center, rotation, tint, opacity } = drawArgs;
+ const { center, rotation, tint, opacity, backgroundText } = drawArgs;
ctx.save();
ctx.translate(center.x, center.y);
@@ -59,6 +59,17 @@ export class Sprite extends Component {
ctx.globalAlpha = opacity;
}
+ if (backgroundText) {
+ // draw text
+ const { fillStyle, font, textAlign, text } = backgroundText;
+ ctx.fillStyle = fillStyle;
+ ctx.font = font;
+ ctx.textAlign = textAlign;
+
+ const height = ctx.measureText("M").width;
+ ctx.fillText(text, center.x, center.y + height / 2);
+ }
+
ctx.drawImage(
this.sheet,
...this.getSpriteArgs(),
diff --git a/src/engine/components/Text.ts b/src/engine/components/Text.ts
new file mode 100644
index 0000000..94dc7a7
--- /dev/null
+++ b/src/engine/components/Text.ts
@@ -0,0 +1,22 @@
+import { Component, ComponentNames } from ".";
+
+export class Text extends Component {
+ public text: string = "";
+ public fillStyle: string;
+ public font: string;
+ public textAlign: CanvasTextAlign;
+
+ constructor(
+ text: string,
+ fillStyle = "white",
+ font = "25px scientifica",
+ textAlign: CanvasTextAlign = "center",
+ ) {
+ super(ComponentNames.Text);
+
+ this.text = text;
+ this.fillStyle = fillStyle;
+ this.font = font;
+ this.textAlign = textAlign;
+ }
+}
diff --git a/src/engine/components/index.ts b/src/engine/components/index.ts
index 4ae886a..104ba2d 100644
--- a/src/engine/components/index.ts
+++ b/src/engine/components/index.ts
@@ -9,3 +9,5 @@ export * from "./Highlight";
export * from "./Interactable";
export * from "./Pushable";
export * from "./Colliding";
+export * from "./LambdaSpawn";
+export * from "./Text";