Skip to content

Commit

Permalink
Simplify map templating
Browse files Browse the repository at this point in the history
  • Loading branch information
ottosichert committed Nov 28, 2023
1 parent 88d4186 commit 54293c2
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 35 deletions.
12 changes: 10 additions & 2 deletions src/engine/generate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import { MapCell } from 'worldmap-generator';
import { creatureSpawns, creatureStats, getRandomDistribution } from './balancing';
import { World, world } from './biomes';

import { Player, Cell, SingleCategories, MultipleCategories, Entity, Rock, Flower, Tree, Bush, grounds, containers, Spell, Armor, Terrain, Sand, Water, Skin, CharacterSelect, Interaction, Lily, Apple, Blossom } from "./entities";
import { Player, Cell, SingleCategories, MultipleCategories, Entity, Rock, Flower, Tree, Bush, grounds, containers, Spell, Armor, Terrain, Sand, Water, Skin, CharacterSelect, Interaction, Lily, Apple, Blossom, Equipment } from "./entities";
import { generateFog } from './fog';
import { createMatrix, generateWhiteNoise, valueNoise } from './noise';
import { corners, createCreature, createEquipment, createInteraction, createParticle, getDeterministicRandomInt, orientations, Processor, sum, TerminalState, updateCell, wrapCoordinates } from "./utils";
import { rural } from './worlds';
import { collectEquipment } from './equipments';

const getSingleElements = (world: World, cells: MapCell[], category: SingleCategories) => cells.map(cell => world.tileCells[cell.name][category]);
const getMultipleElements = (world: World, cells: MapCell[], category: MultipleCategories) => cells.reduce<ReactComponentElement<Entity>[]>((cellTypes, cell) => [
Expand Down Expand Up @@ -250,7 +251,8 @@ function generateLevel(state: TerminalState): TerminalState {
});
});

// load world
/*
// load character select
let characterSelect: Processor<Interaction>;
[state, characterSelect] = createInteraction(state, { x: 0, y: 0 }, CharacterSelect, { quest: 'characterSelect', equipments: [] });
Expand All @@ -259,6 +261,12 @@ function generateLevel(state: TerminalState): TerminalState {
const [targetX] = wrapCoordinates(state, (index - 10) * 3, 0);
state = createEquipment(state, { x: targetX, y: 0, parent: { container: 'interactions', id: characterSelect.id } }, Skin, { amount: 0, maximum: 0, level: skin.charCodeAt(0), particles: [], material: 'gold' })[0];
})
*/

// apply default skin
let skin: Processor<Equipment>;
[state, skin] = createEquipment(state, { x: 0, y: 0 }, Skin, { amount: 0, maximum: 0, level: '\u010b'.charCodeAt(0), particles: [], material: 'gold' });
state = collectEquipment(state, skin.id);


// spawn equipment for development purposes
Expand Down
4 changes: 4 additions & 0 deletions src/engine/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,13 @@ export type Orientation = typeof orientations[number];

export const keyToOrientation: Record<KeyboardEvent["key"], Orientation> = {
ArrowUp: 'up',
w: 'up',
ArrowRight: 'right',
d: 'right',
ArrowDown: 'down',
s: 'down',
ArrowLeft: 'left',
a: 'left',
};

// x, y
Expand Down
71 changes: 38 additions & 33 deletions src/engine/worlds.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,30 +51,33 @@ Boss
*/

const air: LayoutCell = {};
const tree: LayoutCell = { terrain: <Tree /> };
const tre2: LayoutCell = { terrain: <Tree direction="up" /> };
const bush: LayoutCell = { sprite: <Bush /> };
const seed: LayoutCell = { sprite: <Bush />, item: <Seed amount={3} /> };
const flwr: LayoutCell = { sprite: <Flower /> };
const herb: LayoutCell = { sprite: <Flower />, item: <Herb amount={1} /> };
const chst: LayoutCell = { creature: [Chest, { amount: 8, maximum: 8, orientation: 'up', equipments: [], particles: [] }] };
const cmps: LayoutCell = { equipment: [Compass, { material: 'iron', amount: 0, maximum: 0, level: 0 }] };

const getBlockedProps = (direction: Direction): EquipmentProps => (
{ amount: 0, maximum: 0, level: 0, material: 'wood', mode: 'equipped', particle: [
Wave, { direction, material: 'plant' }
] }
);

const pluu: LayoutCell = { equipment: [Blocked, getBlockedProps('up') ] };
const plur: LayoutCell = { equipment: [Blocked, getBlockedProps('upRight') ], sprite: <Flower /> };
const plrr: LayoutCell = { equipment: [Blocked, getBlockedProps('right') ] };
const plrd: LayoutCell = { equipment: [Blocked, getBlockedProps('rightDown') ] };
const pldd: LayoutCell = { equipment: [Blocked, getBlockedProps('down') ] };
const pldl: LayoutCell = { equipment: [Blocked, getBlockedProps('downLeft') ] };
const plll: LayoutCell = { equipment: [Blocked, getBlockedProps('left') ] };
const pllu: LayoutCell = { equipment: [Blocked, getBlockedProps('leftUp') ], sprite: <Flower /> };
const cellMap: Record<string, LayoutCell> = {
'0': null,
' ': {},
'#': { terrain: <Tree /> },
'Θ': { terrain: <Tree direction="up" /> },
'τ': { sprite: <Bush /> },
'T': { sprite: <Bush />, item: <Seed amount={3} /> },
',': { sprite: <Flower /> },
';': { sprite: <Flower />, item: <Herb amount={1} /> },
'+': { creature: [Chest, { amount: 8, maximum: 8, orientation: 'up', equipments: [], particles: [] }] },
'^': { equipment: [Compass, { material: 'iron', amount: 0, maximum: 0, level: 0 }] },
'─': { equipment: [Blocked, getBlockedProps('up') ] },
'│': { equipment: [Blocked, getBlockedProps('right') ] },
'┌': { equipment: [Blocked, getBlockedProps('leftUp') ], sprite: <Flower /> },
'┐': { equipment: [Blocked, getBlockedProps('upRight') ], sprite: <Flower /> },
'┘': { equipment: [Blocked, getBlockedProps('rightDown') ] },
'└': { equipment: [Blocked, getBlockedProps('downLeft') ] },
};

const mapToCells = (map: string) => map.split('\n').map(row => row.split('').map(cell => cellMap[cell]));


export type EquipmentProps = Omit<React.ComponentProps<Equipment>, 'id' | 'particles'> & {
particle?: [Particle, Omit<React.ComponentProps<Particle>, 'id'>],
Expand All @@ -95,25 +98,27 @@ export type World = {
rooms: Record<string, Room>,
};

const ruralMap = `\
00000 00000
000 Θ#Θ#Θ#Θ 000
00 Θ#ττ┌─┐ττ#Θ 00
0 #ττ,,│ │,,ττ# 0
Θτ,, └─┘ ,,τΘ
#τ, , ; ,τ#
Θτ, ,+, ,T; ,τΘ
#τ, , ; ,τ#
Θτ,, ^ ,,τΘ
0 #ττ,, ,,ττ# 0
00 Θ#ττ, ,ττ#Θ 00
000 Θ#Θ Θ#Θ 000
00000 00000\
`;

export const rural: World = {
rooms: {
spawn: {
// 19x13
layout: [
[null,null,null,null,null,air, air, air, air, air, air, air, air, air, null,null,null,null,null],
[null,null,null,air, air, air, tre2,tree,tre2,tree,tre2,tree,tre2,air, air, air, null,null,null],
[null,null,air, air, tre2,tree,bush,bush,pllu,pluu,plur,bush,bush,tree,tre2,air, air, null,null],
[null,air, air, tree,bush,bush,flwr,flwr,plll,null,plrr,flwr,flwr,bush,bush,tree,air, air, null],
[air, air, tre2,bush,flwr,flwr,null,null,pldl,pldd,plrd,null,null,flwr,flwr,bush,tre2,air, air ],
[air, tree,bush,flwr,null,null,flwr,null,null,null,null,null,herb,null,null,flwr,bush,tree,air ],
[air, tre2,bush,flwr,null,flwr,chst,flwr,null,null,null,flwr,seed,herb,null,flwr,bush,tre2,air ],
[air, tree,bush,flwr,null,null,flwr,null,null,null,null,null,herb,null,null,flwr,bush,tree,air ],
[air, air, tre2,bush,flwr,flwr,null,null,null,cmps,null,null,null,flwr,flwr,bush,tre2,air, air ],
[null,air, air, tree,bush,bush,flwr,flwr,null,null,null,flwr,flwr,bush,bush,tree,air, air, null],
[null,null,air, air, tre2,tree,bush,bush,flwr,air, flwr,bush,bush,tree,tre2,air, air, null,null],
[null,null,null,air, air, air, tre2,tree,tre2,air ,tre2,tree,tre2,air, air, air, null,null,null],
[null,null,null,null,null,air, air, air, air, air, air, air, air, air, null,null,null,null,null],
]
layout: mapToCells(ruralMap),
}
}
}

0 comments on commit 54293c2

Please sign in to comment.