Move messages to Log class, use binding

This commit is contained in:
Tobias Berger 2021-09-27 13:25:44 +02:00
parent 2dbca10f6a
commit 79edd639c7
4 changed files with 34 additions and 20 deletions

View file

@ -2,42 +2,40 @@
import { Message, MessageType } from "../shark/Message"; import { Message, MessageType } from "../shark/Message";
import { SharkGame } from "../shark/SharkGame"; import { SharkGame } from "../shark/SharkGame";
export function addMessage( export let messages: Message[];
function addMessage(
message: string, message: string,
type: MessageType = MessageType.message messageType: MessageType = MessageType.message
): void { ) {
SharkGame.messages = [ messages = [...messages, new Message(message, messageType)];
...SharkGame.messages.slice(
-Math.max(...SharkGame.Settings.logLength.options)
),
new Message(message, type),
];
} }
$: logLength = SharkGame.Settings.logLength.current; $: logLength = SharkGame.Settings.logLength.current;
$: displaymessages = [...SharkGame.messages].slice(-logLength).reverse(); $: displaymessages = messages.slice(-logLength).reverse();
</script> </script>
<div id="log"> <div id="log">
<button <button
on:click={() => { on:click={() => {
addMessage("message" + SharkGame.messages.length); addMessage("message" + messages.length);
}}>Add Test Message</button }}>Add Message</button
> >
<button <button
on:click={() => { on:click={() => {
addMessage("warn" + SharkGame.messages.length, MessageType.warning); addMessage("warn" + messages.length, MessageType.warning);
}}>Add Test Warning</button }}>Add Warning</button
> >
<button <button
on:click={() => { on:click={() => {
addMessage("error" + SharkGame.messages.length, MessageType.error); addMessage("error" + messages.length, MessageType.error);
}}>Add Test Error</button }}>Add Error</button
> >
<button <button
on:click={() => { on:click={() => {
Message.even = true; Message.even = true;
SharkGame.messages = [new Message("Log cleared.")]; messages = [];
addMessage("Log cleared");
}}>Clear log</button }}>Clear log</button
> >
<ol> <ol>

View file

@ -1,4 +1,5 @@
<script lang="ts"> <script lang="ts">
import { SharkGame } from "../shark/SharkGame";
import Log from "./Log.svelte"; import Log from "./Log.svelte";
import ResourceTable from "./ResourceTable.svelte"; import ResourceTable from "./ResourceTable.svelte";
</script> </script>
@ -6,7 +7,7 @@
<main> <main>
<div id="left-column"> <div id="left-column">
<ResourceTable /> <ResourceTable />
<Log /> <Log bind:messages={SharkGame.Log.messages} />
</div> </div>
</main> </main>

15
src/shark/Log.ts Normal file
View file

@ -0,0 +1,15 @@
import type { Message } from "./Message";
import { SharkGame } from "./SharkGame";
import { StaticClass } from "./StaticClass";
export class Log extends StaticClass {
static #messages: Message[] = [];
static get messages(): Message[] {
return this.#messages;
}
static set messages(messages: Message[]) {
this.#messages = messages.slice(
-Math.max(...SharkGame.Settings.logLength.options)
);
}
}

View file

@ -1,4 +1,4 @@
import { Message } from "./Message"; import { Log } from "./Log";
import { Settings } from "./Settings"; import { Settings } from "./Settings";
import { StaticClass } from "./StaticClass"; import { StaticClass } from "./StaticClass";
@ -54,7 +54,7 @@ export class SharkGame extends StaticClass {
static title: string; static title: string;
static readonly Settings = Settings; static readonly Settings = Settings;
static messages: Message[] = [new Message("Welcome to Sharg!")]; static readonly Log = Log;
static init(): void { static init(): void {
for (const setting of Object.values(Settings)) { for (const setting of Object.values(Settings)) {