mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-10-21 18:57:03 +02:00
haha characteristics render!
This commit is contained in:
1 parent
bd42cb44e4
commit
af85fb74cb
10 files changed
+114
-38
No files matched your search
@@ -23,7 +23,7 @@ export function TankopediaCharacteristics({
|
||||
<div className={styles.characteristics}>
|
||||
{characteristicsGroups.map((group) => {
|
||||
const items = group.order.filter(
|
||||
(item) => "name" in item && item.name in characteristics,
|
||||
(item) => "name" in item && characteristics.has(item.name),
|
||||
).length;
|
||||
const shouldRender = items > 0;
|
||||
|
||||
@@ -51,7 +51,7 @@ function Group({ group, characteristics }: GroupProps) {
|
||||
const strings = useStrings();
|
||||
const toys = group.order.filter((item) => "toy" in item).length;
|
||||
const items = group.order.filter(
|
||||
(item) => "name" in item && characteristics[item.name] !== null,
|
||||
(item) => "name" in item && characteristics.has(item.name),
|
||||
).length;
|
||||
const groupSpan = `calc(${toys} * var(--group-span) + ${items} + 2)`;
|
||||
const contentSpan = `calc(${toys} * var(--group-span) + ${items})`;
|
||||
@@ -88,8 +88,8 @@ function Group({ group, characteristics }: GroupProps) {
|
||||
{group.order.map((item) => (
|
||||
<>
|
||||
{"toy" in item && <Toy {...item} />}
|
||||
{"name" in item && characteristics[item.name] !== null && (
|
||||
<Item config={item} value={characteristics[item.name]!} />
|
||||
{"name" in item && characteristics.get(item.name) && (
|
||||
<Item config={item} value={characteristics.get(item.name)!} />
|
||||
)}
|
||||
</>
|
||||
))}
|
||||
|
||||
@@ -26,12 +26,12 @@ export const characteristics = {
|
||||
|
||||
damage: {
|
||||
type: CharacteristicType.Number,
|
||||
compute({ characteristic, gun, shell, equalizer, assault_distance }) {
|
||||
compute({ characteristic, gun, shell, environment }) {
|
||||
let coefficient = 1;
|
||||
|
||||
if (gun.assault_ranges && gun.assault_ranges.types.includes(shell.type)) {
|
||||
const match = gun.assault_ranges.ranges.find(
|
||||
({ distance }) => distance >= assault_distance,
|
||||
({ distance }) => distance >= environment.assault_distance,
|
||||
);
|
||||
|
||||
if (match !== undefined) {
|
||||
@@ -39,7 +39,7 @@ export const characteristics = {
|
||||
}
|
||||
}
|
||||
|
||||
coefficient *= equalizer.damage;
|
||||
// coefficient *= equalizer.damage;
|
||||
|
||||
return coefficient * shell.armor_damage;
|
||||
},
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
import { api } from "../api/dynamic";
|
||||
import { characteristics } from "../config/characteristics";
|
||||
import { Tankopedia, type TankEnvironment } from "../stores/tankopedia";
|
||||
import type { TankState } from "../tankopedia/tankState";
|
||||
import type {
|
||||
CharacteristicContext,
|
||||
CharacteristicName,
|
||||
ComputedCharacteristics,
|
||||
} from "../types/characteristics";
|
||||
import type { DuelSide } from "./useEquipment";
|
||||
|
||||
const tanks = await api.tanks();
|
||||
|
||||
export function useCharacteristics(side: DuelSide) {
|
||||
const state = Tankopedia.use((state) => state[side]);
|
||||
const environment = Tankopedia.use((state) => state.environment);
|
||||
|
||||
return computeCharacteristics(state, environment);
|
||||
}
|
||||
|
||||
export function computeCharacteristics(
|
||||
state: TankState,
|
||||
environment: TankEnvironment,
|
||||
) {
|
||||
const computed: ComputedCharacteristics = new Map();
|
||||
|
||||
const tank = tanks.tanks[state.tank];
|
||||
const engine = tank.engines.find((engine) => engine.id === state.engine)!;
|
||||
const track = tank.tracks.find((track) => track.id === state.track)!;
|
||||
const turret = tank.turrets.find((turret) => turret.id === state.turret)!;
|
||||
const gun = turret.guns.find((gun) => gun.id === state.gun)!;
|
||||
const shell = gun.shells.find((shell) => shell.id === state.shell)!;
|
||||
|
||||
function characteristic(name: CharacteristicName) {
|
||||
if (!computed.has(name)) {
|
||||
throw new Error(`Characteristic ${name} not computed yet`);
|
||||
}
|
||||
|
||||
return computed.get(name)!;
|
||||
}
|
||||
|
||||
const context = {
|
||||
state,
|
||||
environment,
|
||||
|
||||
tank,
|
||||
engine,
|
||||
track,
|
||||
turret,
|
||||
gun,
|
||||
shell,
|
||||
|
||||
characteristic,
|
||||
} satisfies CharacteristicContext;
|
||||
|
||||
for (const name in characteristics) {
|
||||
const characteristic = characteristics[name as CharacteristicName];
|
||||
let shouldRender = true;
|
||||
|
||||
if ("should_render" in characteristic) {
|
||||
shouldRender = characteristic.should_render(context);
|
||||
}
|
||||
|
||||
if (!shouldRender) continue;
|
||||
|
||||
const value = characteristic.compute(context);
|
||||
|
||||
computed.set(name as CharacteristicName, value);
|
||||
}
|
||||
|
||||
return computed;
|
||||
}
|
||||
@@ -43,14 +43,14 @@ const { accent, locale } = Astro.props;
|
||||
color: var(--gray-12);
|
||||
font-family: var(--font-family-regular);
|
||||
|
||||
.app {
|
||||
main {
|
||||
display: flex;
|
||||
flex: 1;
|
||||
flex-direction: column;
|
||||
|
||||
background-color: var(--gray-1);
|
||||
|
||||
padding: var(--space-4);
|
||||
/* padding: var(--space-4); */
|
||||
gap: var(--space-6);
|
||||
}
|
||||
}
|
||||
@@ -75,9 +75,9 @@ const { accent, locale } = Astro.props;
|
||||
<body>
|
||||
<slot name="pre-app" />
|
||||
|
||||
<div class="app">
|
||||
<main>
|
||||
<slot />
|
||||
</div>
|
||||
</main>
|
||||
|
||||
<!-- <Footer {locale} /> -->
|
||||
</body>
|
||||
|
||||
@@ -8,8 +8,8 @@ import { defaultEqualizer } from "../../../../../config/equalizer";
|
||||
import { withErrorWrapper } from "../../../../../hocs/withErrorWrapper";
|
||||
import { withLocale } from "../../../../../hocs/withLocale";
|
||||
import { useAwait } from "../../../../../hooks/useAwait";
|
||||
import { useCharacteristics } from "../../../../../hooks/useCharacteristics";
|
||||
import { Tankopedia } from "../../../../../stores/tankopedia";
|
||||
import { computeCharacteristics } from "../../../../../tankopedia/computeCharacteristics";
|
||||
import type { MaybeSkeletonComponentProps } from "../../../../../types/maybeSkeletonComponentProps";
|
||||
import styles from "./_index.module.css";
|
||||
|
||||
@@ -26,14 +26,10 @@ export const Page = withErrorWrapper(
|
||||
Tankopedia.useInitialization(protagonistTank);
|
||||
|
||||
const protagonist = Tankopedia.use((state) => state.protagonist);
|
||||
// const protagonistEquipment = useEquipment("protagonist", protagonistTank.tank!);
|
||||
const equalize = Tankopedia.use((state) => state.environment.equalize);
|
||||
|
||||
const characteristics = useMemo(
|
||||
() => computeCharacteristics(),
|
||||
[protagonist],
|
||||
);
|
||||
const characteristics = useCharacteristics("protagonist");
|
||||
|
||||
const equalize = Tankopedia.use((state) => state.equalize);
|
||||
const thicknessRange = useMemo(() => {
|
||||
const entries = Object.values(tanks.tanks);
|
||||
const filtered = entries.filter(
|
||||
|
||||
@@ -75,14 +75,18 @@ export enum TankopediaRelativeAgainst {
|
||||
All,
|
||||
}
|
||||
|
||||
export interface TankEnvironment {
|
||||
equalize: boolean;
|
||||
assault_distance: number;
|
||||
}
|
||||
|
||||
interface Tankopedia {
|
||||
disturbed: boolean;
|
||||
revealed: boolean;
|
||||
|
||||
protagonist: TankState;
|
||||
antagonist: TankState;
|
||||
|
||||
equalize: boolean;
|
||||
environment: TankEnvironment;
|
||||
|
||||
shot?: Shot;
|
||||
skills: Record<string, number>;
|
||||
@@ -121,7 +125,7 @@ export const Tankopedia = new Soapstone<Tankopedia, [TankDefinition]>(
|
||||
antagonist: createTankState(tank),
|
||||
model: models.models[tank.id],
|
||||
|
||||
equalize: false,
|
||||
environment: { equalize: false, assault_distance: 0 },
|
||||
|
||||
relativeAgainst: TankopediaRelativeAgainst.Class,
|
||||
editStatic: false,
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
import type { ComputedCharacteristics } from "../types/characteristics";
|
||||
|
||||
export function computeCharacteristics() {
|
||||
const characteristics: Partial<ComputedCharacteristics> = {};
|
||||
|
||||
return characteristics as ComputedCharacteristics;
|
||||
}
|
||||
@@ -27,7 +27,10 @@ export function createTankState(tank: TankDefinition) {
|
||||
shell: gun.shells[0].id,
|
||||
track: tank.tracks.at(-1)!.id,
|
||||
|
||||
assault_distance: (gun.assault_ranges?.ranges[0].distance ?? 0) / 2,
|
||||
environment: {
|
||||
assault_distance: (gun.assault_ranges?.ranges[0].distance ?? 0) / 2,
|
||||
},
|
||||
|
||||
speed: 0,
|
||||
|
||||
equipment: {},
|
||||
|
||||
@@ -9,7 +9,6 @@ export interface TankState {
|
||||
shell: number;
|
||||
track: number;
|
||||
|
||||
assault_distance: number;
|
||||
speed: number;
|
||||
|
||||
equipment: Record<number, number>;
|
||||
|
||||
@@ -1,11 +1,16 @@
|
||||
import type { Strings } from "@blitzkit/i18n";
|
||||
import type {
|
||||
Equalizer,
|
||||
EngineDefinition,
|
||||
GunDefinition,
|
||||
ShellDefinition,
|
||||
TankDefinition,
|
||||
TrackDefinition,
|
||||
TurretDefinition,
|
||||
} from "@blitzkit/protos";
|
||||
import type { ReactNode } from "react";
|
||||
import { characteristics } from "../config/characteristics";
|
||||
import type { TankEnvironment } from "../stores/tankopedia";
|
||||
import type { TankState } from "../tankopedia/tankState";
|
||||
|
||||
export type Characteristic = {
|
||||
should_render?(context: CharacteristicContext): boolean;
|
||||
@@ -20,14 +25,17 @@ export type Characteristic = {
|
||||
}
|
||||
);
|
||||
|
||||
interface CharacteristicContext {
|
||||
export interface CharacteristicContext {
|
||||
state: TankState;
|
||||
environment: TankEnvironment;
|
||||
|
||||
tank: TankDefinition;
|
||||
engine: EngineDefinition;
|
||||
track: TrackDefinition;
|
||||
turret: TurretDefinition;
|
||||
gun: GunDefinition;
|
||||
shell: ShellDefinition;
|
||||
|
||||
equalizer: Equalizer;
|
||||
|
||||
assault_distance: number;
|
||||
|
||||
characteristic(
|
||||
name: CharacteristicName,
|
||||
): ReturnType<Characteristic["compute"]>;
|
||||
@@ -41,8 +49,9 @@ export enum CharacteristicType {
|
||||
export type CharacteristicName = keyof typeof characteristics;
|
||||
export type CharacteristicReturnType = ReturnType<Characteristic["compute"]>;
|
||||
|
||||
export type ComputedCharacteristics = Partial<
|
||||
Record<CharacteristicName, CharacteristicReturnType>
|
||||
export type ComputedCharacteristics = Map<
|
||||
CharacteristicName,
|
||||
CharacteristicReturnType
|
||||
>;
|
||||
|
||||
export interface CharacteristicRenderConfig {
|
||||
|
||||
Reference in new issue
Block a user