From 3f0fbb7f143a314490f81751f3c03a2ce9bf350c Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 29 May 2023 21:50:29 +0200 Subject: [PATCH] Add Map2d component, add lightMap inside level.json --- src/components/Map2d.svelte | 11 ++++++ src/components/Scene.svelte | 42 ++++++++++++++++------- src/components/Torch.svelte | 68 ++++++++++++++++++++----------------- src/lib/map.ts | 13 +++++++ src/lib/maps/level-0.json | 14 ++++++++ 5 files changed, 104 insertions(+), 44 deletions(-) create mode 100644 src/components/Map2d.svelte diff --git a/src/components/Map2d.svelte b/src/components/Map2d.svelte new file mode 100644 index 0000000..ed14226 --- /dev/null +++ b/src/components/Map2d.svelte @@ -0,0 +1,11 @@ + + +{#each map2d as mapX, x} + {#each mapX as item, y} + + {/each} +{/each} diff --git a/src/components/Scene.svelte b/src/components/Scene.svelte index 35c713e..6f46601 100644 --- a/src/components/Scene.svelte +++ b/src/components/Scene.svelte @@ -3,12 +3,28 @@ import * as SC from 'svelte-cubed'; import { position } from '$lib/player'; - import { collisions, tiles } from '$lib/map'; import { textures } from '$lib/textures'; + import { collisions, tiles, lights } from '$lib/map'; import Wall from './Wall.svelte'; import Floor from './Floor.svelte'; import Torch from './Torch.svelte'; + import Map2d from './Map2d.svelte'; + + const getLightDirection = function (x: number, y: number) { + if ($collisions[x + 1][y]) { + return 0; + } + if ($collisions[x][y + 1]) { + return Math.PI / 2; + } + if ($collisions[x - 1][y]) { + return Math.PI; + } + if ($collisions[x][y - 1]) { + return Math.PI / 2 + Math.PI; + } + }; - {#each $collisions as collisionX, x} - {#each collisionX as collision, y} - {#if collision == 1} - - {/if} - {#if collision == 0} - - {/if} - {/each} - {/each} + + {#if item == 1} + + {/if} + {#if item == 0} + + {/if} + - + + {#if item} + + {/if} + - + - + - + diff --git a/src/lib/map.ts b/src/lib/map.ts index b3d5e16..0218801 100644 --- a/src/lib/map.ts +++ b/src/lib/map.ts @@ -44,3 +44,16 @@ const createCollisions = () => { }; export const collisions = createCollisions(); + +const createLights = () => { + const { subscribe, set } = writable( + swapXY(level0.width, level0.height, level0.lightMap) + ); + + return { + subscribe, + set + }; +}; + +export const lights = createLights(); diff --git a/src/lib/maps/level-0.json b/src/lib/maps/level-0.json index 26bdaa1..f1e21f1 100644 --- a/src/lib/maps/level-0.json +++ b/src/lib/maps/level-0.json @@ -28,5 +28,19 @@ [0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] + ], + "lightMap": [ + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] ] }