update: talent, state props

This commit is contained in:
Vick Scarlet
2026-08-16 20:06:16 +08:00
committed by 神戸小鳥
parent 85c871cec1
commit 32f703be0f
12 changed files with 534 additions and 192 deletions
+73 -80
View File
@@ -1,11 +1,9 @@
import type { Talent } from '@remake/data/talent'
import type { Event } from '@remake/data/event'
import type { Achievement } from '@remake/data/achievement'
import { produce, enableMapSet } from 'immer'
import { produce } from 'immer'
import { sum } from '@remake/vitex'
enableMapSet()
/** 基础的属性 */
export interface Properties {
age: number
@@ -16,22 +14,35 @@ export interface Properties {
spirit: number
}
export interface HLProperties {
current: Properties // 当前属性
highest: Properties // 历史最高属性
lowest: Properties // 历史最低属性
}
export type Allocation = Omit<Properties, 'age' | 'spirit'>
export function createProperties(allocation: Allocation) {
return { ...allocation, age: -1, spirit: 0 }
}
export function createHLProperties(allocation: Allocation) {
const current = createProperties(allocation)
return { current, highest: { ...current }, lowest: { ...current } }
}
export interface GameState {
properties: Properties // 本局属性
highest: Properties // 本局最高属性
lowest: Properties // 本局最低属性
props: HLProperties // 本局属性
life: number // 本局生命值
talents: Set<Talent['id']> // 本局拥有的天赋
events: Set<Event['id']> // 本局触发过的事件
achievements: Set<Achievement['id']> // 本局达成的成就
talentTriggers: Map<Talent['id'], number> // 本局天赋触发次数
}
/** 持久化存储的数据 */
export interface ProfileState {
times: number // 游戏次数
external?: Talent['id'] // 继承的天赋
lockedTalent?: Talent['id'] // 继承的天赋
talents: Set<Talent['id']> // 拥有过的天赋
events: Set<Event['id']> // 触发过的事件
achievements: Set<Achievement['id']> // 达成的成就
@@ -43,39 +54,37 @@ export function createState(
allocation: Allocation,
talents?: Iterable<Talent['id']>,
): GameState {
const properties = { ...allocation, age: -1, spirit: 0 }
return {
properties,
highest: { ...properties },
lowest: { ...properties },
props: createHLProperties(allocation),
life: 1,
talents: new Set(talents),
events: new Set(),
achievements: new Set(),
talentTriggers: new Map(),
}
}
export interface FlatState {
AGE: GameState['properties']['age']
CHR: GameState['properties']['charm']
INT: GameState['properties']['intelligence']
STR: GameState['properties']['strength']
MNY: GameState['properties']['money']
SPR: GameState['properties']['spirit']
AGE: GameState['props']['current']['age']
CHR: GameState['props']['current']['charm']
INT: GameState['props']['current']['intelligence']
STR: GameState['props']['current']['strength']
MNY: GameState['props']['current']['money']
SPR: GameState['props']['current']['spirit']
HAGE: GameState['props']['highest']['age']
HCHR: GameState['props']['highest']['charm']
HINT: GameState['props']['highest']['intelligence']
HSTR: GameState['props']['highest']['strength']
HMNY: GameState['props']['highest']['money']
HSPR: GameState['props']['highest']['spirit']
LAGE: GameState['props']['lowest']['age']
LCHR: GameState['props']['lowest']['charm']
LINT: GameState['props']['lowest']['intelligence']
LSTR: GameState['props']['lowest']['strength']
LMNY: GameState['props']['lowest']['money']
LSPR: GameState['props']['lowest']['spirit']
LIF: GameState['life']
TLT: GameState['talents']
EVT: GameState['events']
LAGE: GameState['lowest']['age']
HAGE: GameState['highest']['age']
LCHR: GameState['lowest']['charm']
HCHR: GameState['highest']['charm']
LINT: GameState['lowest']['intelligence']
HINT: GameState['highest']['intelligence']
LSTR: GameState['lowest']['strength']
HSTR: GameState['highest']['strength']
LMNY: GameState['lowest']['money']
HMNY: GameState['highest']['money']
LSPR: GameState['lowest']['spirit']
HSPR: GameState['highest']['spirit']
TMS: ProfileState['times']
AEVT: ProfileState['events']
@@ -97,38 +106,40 @@ type FlatMapper<Key extends FlatStateKey> = (
) => FlatState[Key]
const FlatMappers = {
AGE: state => state.game.properties.age,
CHR: state => state.game.properties.charm,
INT: state => state.game.properties.intelligence,
STR: state => state.game.properties.strength,
MNY: state => state.game.properties.money,
SPR: state => state.game.properties.spirit,
AGE: state => state.game.props.current.age,
CHR: state => state.game.props.current.charm,
INT: state => state.game.props.current.intelligence,
STR: state => state.game.props.current.strength,
MNY: state => state.game.props.current.money,
SPR: state => state.game.props.current.spirit,
HAGE: state => state.game.props.highest.age,
HCHR: state => state.game.props.highest.charm,
HINT: state => state.game.props.highest.intelligence,
HSTR: state => state.game.props.highest.strength,
HMNY: state => state.game.props.highest.money,
HSPR: state => state.game.props.highest.spirit,
LAGE: state => state.game.props.lowest.age,
LCHR: state => state.game.props.lowest.charm,
LINT: state => state.game.props.lowest.intelligence,
LSTR: state => state.game.props.lowest.strength,
LMNY: state => state.game.props.lowest.money,
LSPR: state => state.game.props.lowest.spirit,
LIF: state => state.game.life,
TLT: state => state.game.talents,
EVT: state => state.game.events,
LAGE: state => state.game.lowest.age,
HAGE: state => state.game.highest.age,
LCHR: state => state.game.lowest.charm,
HCHR: state => state.game.highest.charm,
LINT: state => state.game.lowest.intelligence,
HINT: state => state.game.highest.intelligence,
LSTR: state => state.game.lowest.strength,
HSTR: state => state.game.highest.strength,
LMNY: state => state.game.lowest.money,
HMNY: state => state.game.highest.money,
LSPR: state => state.game.lowest.spirit,
HSPR: state => state.game.highest.spirit,
TMS: state => state.profile.times,
AEVT: state => state.profile.events,
ATLT: state => state.profile.talents,
AACH: state => state.profile.achievements,
SUM: state => {
const { age, ...others } = state.game.highest
SUM: ({ game: { props } }) => {
const { age, ...others } = props.highest
const s = sum(Object.values(others))
return Math.floor(s * 2 + age / 2)
},
} as { [Key in FlatStateKey]: FlatMapper<Key> }
export const SupportedFlatStateKeys = new Set(Object.keys(FlatMappers))
const flatStateHandle = {
get<Key extends FlatStateKey>(target: FlatTarget, prop: Key) {
return FlatMappers[prop]?.(target)
@@ -140,43 +151,25 @@ export function createFlatState(game: GameState, profile: ProfileState) {
return new Proxy({ game, profile }, flatStateHandle) as unknown as FlatState
}
export interface Effect {
properties?: Partial<Properties>
life?: number
talents?: Iterable<Talent['id']>
events?: Iterable<Event['id']>
achievements?: Iterable<Achievement['id']>
}
export function stateEffect(state: GameState, effect: Effect) {
return produce(state, draft => {
for (const key in effect.properties) {
export function propsEffect(hlp: HLProperties, effect: Partial<Properties>) {
return produce(hlp, draft => {
for (const key in effect) {
const prop = key as keyof Properties
const value = effect.properties[prop]!
draft.properties[prop] += value
const value = effect[prop]!
draft.current[prop] += value
draft.highest[prop] = Math.max(
draft.highest[prop],
draft.properties[prop],
draft.current[prop],
)
draft.lowest[prop] = Math.min(
draft.lowest[prop],
draft.properties[prop],
draft.current[prop],
)
}
if (effect.life) draft.life += effect.life
if (effect.talents)
draft.talents = new Set([...draft.talents, ...effect.talents])
if (effect.events)
draft.events = new Set([...draft.events, ...effect.events])
if (effect.achievements)
draft.achievements = new Set([
...draft.achievements,
...effect.achievements,
])
})
}
function highestProperties(a: Properties, b?: Properties): Properties {
export function highestProperties(a: Properties, b?: Properties): Properties {
if (!b) return { ...a }
const result = {} as Properties
for (const key of Object.keys(a) as (keyof Properties)[]) {
@@ -185,7 +178,7 @@ function highestProperties(a: Properties, b?: Properties): Properties {
return result
}
function lowestProperties(a: Properties, b?: Properties): Properties {
export function lowestProperties(a: Properties, b?: Properties): Properties {
if (!b) return { ...a }
const result = {} as Properties
for (const key of Object.keys(a) as (keyof Properties)[]) {
@@ -197,15 +190,15 @@ function lowestProperties(a: Properties, b?: Properties): Properties {
export function nextProfile(
profile: ProfileState,
state: GameState,
external?: Talent['id'],
lockedTalent?: Talent['id'],
) {
return {
times: profile.times + 1,
talents: profile.talents.union(state.talents),
events: profile.events.union(state.events),
achievements: profile.achievements.union(state.achievements),
highest: highestProperties(state.highest, profile.highest),
lowest: lowestProperties(state.lowest, profile.lowest),
external: external ?? profile.external,
highest: highestProperties(state.props.highest, profile.highest),
lowest: lowestProperties(state.props.lowest, profile.lowest),
lockedTalent: lockedTalent ?? profile.lockedTalent,
}
}