Skip to content

Commit

Permalink
Split AI <=> NPC
Browse files Browse the repository at this point in the history
  • Loading branch information
shezard committed Jun 29, 2023
1 parent 3bc6654 commit 58534b3
Show file tree
Hide file tree
Showing 12 changed files with 199 additions and 131 deletions.
18 changes: 17 additions & 1 deletion src/components/Editor/Editor.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
isToolActivated,
currentAI,
currentTexture,
currentDoodad
currentDoodad,
currentNpc
} from '$stores/editor'
import type { Script, Tile } from '../..';
import EditorTile from './EditorTile.svelte';
Expand Down Expand Up @@ -106,6 +109,19 @@
});
}
if($activatedTool === 'npc' && e.type === 'mousedown') {
store.update((store) => {
const ai = store.levels[$currentLevelNumber].getScriptAt(x, y);
if(ai) {
store.levels[$currentLevelNumber].removeScriptAt(x, y);
} else {
store.levels[$currentLevelNumber].addNpcAt(x, y , $currentNpc)
}
return store;
});
}
if($activatedTool === 'doodad' && e.type === 'mousedown') {
store.update((store) => {
const doodad = store.levels[$currentLevelNumber].getScriptAt(x, y);
Expand Down
15 changes: 13 additions & 2 deletions src/components/Editor/EditorInfoPane.svelte
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<script lang="ts">
import { store } from '$stores/store';
import { currentLevelNumber, currentLevel, currentTexture, currentAI, currentDoodad } from '$stores/editor';
import { currentLevelNumber, currentLevel, currentTexture, currentAI, currentNpc, currentDoodad } from '$stores/editor';
import { textures } from '$stores/textures';
import EditorTexture from './EditorTexture.svelte';
import EditorTool from "./EditorTool.svelte";
import type { AIName, DoodadName } from '../..';
import type { AIName, DoodadName, NpcName } from '../..';
import { Level } from '$lib/Level';
const changeLevel = (e: Event) => {
Expand Down Expand Up @@ -90,6 +90,8 @@
const aiOptions : AIName[] = ['orc', 'gobelin'];
const npcOptions : NpcName[] = ['man'];
const doodadOptions : DoodadName[] = ['door'];
</script>
Expand Down Expand Up @@ -148,6 +150,15 @@
</select>
</EditorTool>

<EditorTool tool="npc">
NPC
<select on:click|stopPropagation bind:value={$currentNpc}>
{#each npcOptions as npc}
<option value="{npc}">{npc}</option>
{/each}
</select>
</EditorTool>

<EditorTool tool="doodad">
Doodad
<select on:click|stopPropagation bind:value={$currentDoodad}>
Expand Down
19 changes: 19 additions & 0 deletions src/components/Npc.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<script lang="ts">
import type { Npc } from '..';
import Box from './Box.svelte';
import { player } from '$stores/player';
import type * as THREE from 'three';
export let npc: Npc;
export let texture: THREE.Texture[];
</script>

<Box
x={npc.x}
y={npc.y}
wx={Math.abs(Math.cos($player.position.t))}
wy={Math.abs(Math.sin($player.position.t))}
{texture}
color={npc.color}
transparent
/>
5 changes: 5 additions & 0 deletions src/components/Script.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import { container } from '$stores/container';
import { player } from '$stores/player';
import { get } from 'svelte/store';
import Npc from './Npc.svelte';
export let script: Script;
Expand Down Expand Up @@ -109,6 +110,10 @@
<AI ai={script} {texture}/>
{/if}

{#if script.type == 'npc'}
<Npc npc={script} {texture}/>
{/if}

{#if script.type == 'loot'}
<Box
x={script.x}
Expand Down
15 changes: 14 additions & 1 deletion src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,25 @@ export type Doodad = {
z?: number;
} & OrientedPosition;

export type NpcName = 'man' | 'not-found';

export type Npc = {
id: number;
type: 'npc';
name: string;
collision?: boolean;
texture: string[];
color?: number;
} & OrientedPosition;

export type AIName = 'orc' | 'gobelin' | 'not-found';
export type AIMode = 'idle' | 'attack-on-sight';

export type AI = {
id: number;
type: 'ai';
name: string;
mode: AIMode;
collision?: boolean;
texture: string[];
color?: number;
Expand Down Expand Up @@ -90,7 +103,7 @@ export type Container = {
content: ItemName[];
} & OrientedPosition;

export type Script = Doodad | AI | Loot | Panel | Container;
export type Script = Doodad | AI | Npc | Loot | Panel | Container;

export interface OrientedPosition {
x: number;
Expand Down
8 changes: 8 additions & 0 deletions src/lib/AI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const makeAI = function (
id: id,
type: 'ai',
name: aiName,
mode: 'attack-on-sight',
collision: true,
texture: ['orc-1'],
x: x,
Expand All @@ -41,6 +42,7 @@ export const makeAI = function (
id: id,
type: 'ai',
name: aiName,
mode: 'attack-on-sight',
collision: true,
texture: [`${aiName}-idle`],
x: x,
Expand All @@ -63,6 +65,7 @@ export const makeAI = function (
id: 0,
type: 'ai',
name: 'not-found',
mode: 'idle',
collision: true,
texture: ['not-found'],
x: x,
Expand All @@ -81,6 +84,11 @@ export const makeAI = function (
};

export const advanceAi = (store: Store, level: Level) => (ai: AI) => {
if (ai.mode === 'idle') {
return;
}

// ai.mode === 'aggro'
const position = get(player).position;
const stats = get(player).stats;

Expand Down
9 changes: 8 additions & 1 deletion src/lib/Level.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import type {
OrientedPosition,
Light,
AIName,
DoodadName
DoodadName,
NpcName
} from '..';
import { advanceAi, makeAI } from './AI';
import { makeDoodad } from './Doodad';
import { makeNpc } from './Npc';

export class Level {
width: number;
Expand Down Expand Up @@ -149,6 +151,11 @@ export class Level {
});
}

addNpcAt(x: number, y: number, npcName: NpcName) {
const id = Math.max(...this.scripts.map((script) => script.id), 0) + 1;
this.scripts = [...this.scripts, makeNpc(npcName, id, x, y)];
}

addDoodadAt(x: number, y: number, doodadName: DoodadName): void {
const id = Math.max(...this.scripts.map((script) => script.id), 0) + 1;
this.scripts = [...this.scripts, makeDoodad(doodadName, id, x, y)];
Expand Down
27 changes: 27 additions & 0 deletions src/lib/Npc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import type { Npc, NpcName } from '..';

export const makeNpc = function (npcName: NpcName, id: number, x: number, y: number): Npc {
if (npcName === 'man') {
return {
id: id,
type: 'npc',
name: npcName,
collision: true,
texture: ['npc-1'],
x: x,
y: y,
t: 0
};
}

return {
id: id,
type: 'npc',
name: 'not-found',
collision: true,
texture: ['npc-1'],
x: x,
y: y,
t: 0
};
};
Loading

0 comments on commit 58534b3

Please sign in to comment.