Compare commits

...

2 commits

Author SHA1 Message Date
8a9d5414f8 Entity interface split up
+ Better names
- Stuff is all broken (not like it worked before)
2022-07-05 14:55:51 -04:00
932fcaceb3 More random changes to entity manager (IDK)
~ Just committing so I can revert changes later if I screw something up
2022-06-21 10:59:31 -04:00
2 changed files with 165 additions and 117 deletions

View file

@ -1,18 +1,54 @@
// "EntityManager": Create entity objects and apply transformations to them. // "EntityManager": Create entity objects and apply transformations to them.
// + Functions are here, as to avoid storing unecessary data in the server store. // A bold attempt at functional programming in a videogame entity system
import { makePuppet, puppet, puppetEntry } from "./Puppetmaster"; import { makePuppet, puppet, puppetEntry } from "./Puppetmaster";
// import { ability } from "./AbilityManager"; // import { ability } from "./AbilityManager";
// I spent a lot of time thinking about the names for these types and they still suck const entityTags = ["minion"] as const
type modifiers = readonly [power: number, speed: number, defense: number, resistance: number]; // values used for calculation, only modified by buffs and debuffs (the first group is additive, the second multiplicative) type modifiers = readonly [power: number, speed: number, defense: number, resistance: number]; // values used for calculation, only modified by buffs and debuffs
type statuses = readonly [health: number, barrier: number]; // values used to store an entity's current status (more existential than stats) type statuses = readonly [health: number, barrier: number]; // values used to store an entity's current status (more existential than stats)
type statusEffect = readonly [string, modifiers, entityTransformTemplate, number]; type statusEffect = readonly [string, modifiers, entityTransformTemplate, number];
type entityTag = typeof entityTags[number]
export interface entityId {
readonly name: string;
readonly team: "players" | "enemies";
readonly tags: {
[tagName in entityTag]: boolean;
};
}
export interface entityProperties {
readonly id: entityId,
readonly baseModifiers: modifiers,
readonly maxStatuses: statuses,
readonly baseStatusEffects: statusEffect[],// Permanent immunities/status effects
}
export interface entityStatuses {
readonly statuses: statuses; // Health and stuff that change constantly
readonly statusEffects: statusEffect[]; // Burn, poison, etc.
}
type entityTransformType = "support" | "attack";
type healthTransformValue = [magnitude: number, affectsHealth: boolean, affectsBarrier: boolean]
interface entityTransformTemplate {
healthTransformValue: healthTransformValue,
statusEffectsGranted?: placeholder[]; // TBA (Stuff like burn, slow, stun, etc.)
}
type entityTransformDeterminer = (
entityPerformingId: entityId,
entityReceivingId: entityId,
entityPerformingPuppet: puppet,
entityReceivingPuppet: puppet,
) => [entityTransformType, entityTransformTemplate] | false;
interface ability {
placeholder: entityTransformDeterminer; // TBA (Most abilities will not be a single instant of damage/heal)
}
// Immunities are just status effects that take the place of a damaging status effect (so ones of that type can't be added) but don't do anything
// Status effects have a strength value that determines whether they can override another status effect of the same type (holy inferno can replace burn but burn can't replace holy inferno)
export interface entityController { export interface entityController {//Deprecated
setPosition: (location: Vector3) => void; setPosition: (location: Vector3) => void;
useAbility: (abilityName: string, activating: boolean) => void; useAbility: (abilityName: string, activating: boolean) => void;
} }
export interface entityModifier {} export interface entityModifier {}// Deprecated
/* Prototype /* Prototype
entityTransform(setOfAllEntities: entity[], actingEntity: entity, ability destructured to {hitBoxList, modificationList}) entityTransform(setOfAllEntities: entity[], actingEntity: entity, ability destructured to {hitBoxList, modificationList})
const entitiesEligible = getAffectedEntities(setOfAllEntities, actingEntity) const entitiesEligible = getAffectedEntities(setOfAllEntities, actingEntity)
@ -31,25 +67,6 @@ export interface entityModifier {}
const ableToUse = completeAbility ? canUseAbility(now, completeAbility) : completeAbility // canUseAbility should return the first abilityPart const ableToUse = completeAbility ? canUseAbility(now, completeAbility) : completeAbility // canUseAbility should return the first abilityPart
return resultOfFirstAbilityPart = ableToUse ? useAbilityPart(ability.firstPart, setOfAllEntities, thisEntity) : [ableToUse] return resultOfFirstAbilityPart = ableToUse ? useAbilityPart(ability.firstPart, setOfAllEntities, thisEntity) : [ableToUse]
*/ */
interface entityStats {
statuses: statuses; // Health and stuff that change constantly
/* immunities: {
[statusEffectName: string]: boolean | undefined;
};*/
statusEffects: statusEffect[]; // Burn, poison, etc.
}
interface entityId {
readonly name: string;
readonly team: "players" | "enemies";
readonly isMinion: boolean;
}
export interface entity {
readonly id: entityId;
stats: entityStats;
baseModifiers: modifiers; // Base modifiers that change only when the player switches weapons or something
maxStatuses: statuses;
puppet: puppet;
}
// type entityTransform = (entity: entity) => entity; // type entityTransform = (entity: entity) => entity;
// interface entityTransformTemplate { // interface entityTransformTemplate {
@ -65,23 +82,6 @@ export interface entity {
excludesSelf: boolean, excludesSelf: boolean,
affectsSameTeam: boolean, affectsSameTeam: boolean,
*/ //} */ //}
type entityTransformType = "heal" | "attack";
interface entityTransformTemplate {
thingus: entityTransformType;
magnitude: number;
affectsHealth: boolean;
affectsBarrier: boolean;
statusEffectsGranted?: placeholder[]; // TBA (Stuff like burn, slow, stun, etc.)
}
type entityTransformDeterminer = (
entityPerformingId: entityId,
entityReceivingId: entityId,
entityPerformingPuppet: puppet,
entityReceivingPuppet: puppet,
) => entityTransformTemplate | false;
interface ability {
placeholder: entityTransformDeterminer; // TBA (Most abilities will not be a single instant of damage/heal)
}
// type entityTransformTemplate = [entityTransformEligibilityTemplate, entityTransformApplicationTemplate] // type entityTransformTemplate = [entityTransformEligibilityTemplate, entityTransformApplicationTemplate]
/*function isEligibleForTransform(entityPerformingTransform: entity, entityReceivingTransform: entity, eligibilityTemplate: entityTransformEligibilityTemplate): false | [boolean] { // This function sucks /*function isEligibleForTransform(entityPerformingTransform: entity, entityReceivingTransform: entity, eligibilityTemplate: entityTransformEligibilityTemplate): false | [boolean] { // This function sucks
const entityReceivingId = entityReceivingTransform.id const entityReceivingId = entityReceivingTransform.id
@ -100,27 +100,57 @@ interface ability {
} }
return [onSameTeam] return [onSameTeam]
}*/ }*/
function applyEntityToAttack(
entityModifiers: modifiers, /*function applyAttackToEntityStats() {//else if (transformType === "heal") {
entityStatusEffects: statusEffect[], // Apply receiver's status effects to incoming heal
magnitude: number, const newEntityStatuses = applyHealToStatuses(
): number { entityStats.statuses, // DRY alert
const attack = applyModifiersToAttack(magnitude, entityModifiers); magnitude,
// + Apply status effects of performing entity to attack (e.g. weaken) entityTransformTemplate.affectsHealth,
return attack; entityTransformTemplate.affectsBarrier,
} maxStatuses,
);
// + Add or remove status effects (don't compare against immunities)
return {
statuses: newEntityStatuses,
statusEffects: entityStats.statusEffects, // Placeholder
};
}*/
function applyAttackToEntityStatuses( function applyAttackToEntityStatuses(
entityStatuses: statuses, entityStatuses: statuses,
entityTransformTemplate: entityTransformTemplate,
entityModifiers: modifiers, entityModifiers: modifiers,
entityStatusEffects: statusEffect[], entityStatusEffects: statusEffect[],
attack: number,
affectsHealth: boolean,
affectsBarrier: boolean,
): statuses { ): statuses {
// Not sure if this should return a whole entity
// + Apply status effects of receiving entity to damage (e.g. armor break) // + Apply status effects of receiving entity to damage (e.g. armor break)
const damage = applyModifiersToAttack(attack, entityModifiers); const modifiedDamage = applyModifiersToDamage(entityTransformTemplate.magnitude, entityModifiers);
const newStatuses = applyDamageToStatuses(entityStatuses, damage, affectsHealth, affectsBarrier); const newStatuses = applyDamageToStatuses(entityStatuses, modifiedDamage, entityTransformTemplate.affectsHealth, entityTransformTemplate.affectsBarrier);
return entityStatuses;
}
/*function applyHealToEntityStats() {//if (transformType === "attack") {
// Apply receiver's status effects to incoming damage
const incomingDamage = applyModifiersToDamage(magnitude, baseModifiers);
const newEntityStatuses = applyDamageToStatuses(
entityStats.statuses,
incomingDamage,
entityTransformTemplate.affectsHealth,
entityTransformTemplate.affectsBarrier,
);
// + Add or remove status effects (compare against immunities)
return {
statuses: newEntityStatuses,
statusEffects: entityStats.statusEffects, // Placeholder
};
} */
function applyHealToEntityStatuses(
entityStatuses: statuses,
entityTransformTemplate: entityTransformTemplate,
entityModifiers: modifiers,
entityMaxStatuses: statuses,
entityStatusEffects: statusEffect[],
): statuses {
// + Apply status effects of receiving entity to damage (e.g. heal up)
const newStatuses = applyHealToStatuses(entityStatuses, entityTransformTemplate.magnitude, entityTransformTemplate.affectsHealth, entityTransformTemplate.affectsBarrier, entityMaxStatuses);
return entityStatuses; return entityStatuses;
} }
// ? Are you meant to pipe determineEntityTransform into the resulting function? // ? Are you meant to pipe determineEntityTransform into the resulting function?
@ -131,53 +161,33 @@ function applyAttackToEntityStatuses(
return false; // Placeholder return false; // Placeholder
} }
*/ */
function transformEntityStats( function applyEntityToAttack(
entityPerformingTransform: entity, entityModifiers: modifiers,
entityStatusEffects: statusEffect[],
entityTransformTemplate: entityTransformTemplate,
): entityTransformTemplate {
const outgoingAttack = applyModifiersToAttack(entityTransformTemplate.magnitude, entityModifiers);
// + Apply user's status effects to outgoing attack
// (Old; same thing) + Apply status effects of performing entity to attack (e.g. weaken)
entityTransformTemplate.magnitude = outgoingAttack; // Mutating is cringe, but the other parts of the transform need to be defined first
return entityTransformTemplate;
}
function applyEntityToHeal(
entityModifiers: modifiers,
entityStatusEffects: statusEffect[],
entityTransformTemplate: entityTransformTemplate,
): entityTransformTemplate {
// + Apply user's status effects to outgoing heal
return entityTransformTemplate // There could be a heal modifier later
}
/*function transformEntityStats(
entityStats: entityStats, entityStats: entityStats,
entityTransformTemplate: entityTransformTemplate, entityTransformTemplate: entityTransformTemplate,
baseModifiers: modifiers, baseModifiers: modifiers,
maxStatuses: statuses, maxStatuses: statuses,
): entityStats { ): entityStats {
const transformType = entityTransformTemplate.thingus; const magnitude = entityTransformTemplate.magnitude*/
if (transformType === "attack") { //}
const outgoingAttack = applyModifiersToAttack(
entityTransformTemplate.magnitude,
entityPerformingTransform.baseModifiers,
);
// Apply user's status effects to outgoing attack
// Apply receiver's status effects to incoming damage
const incomingDamage = applyModifiersToDamage(outgoingAttack, baseModifiers);
const newEntityStatuses = applyDamageToStatuses(
entityStats.statuses,
incomingDamage,
entityTransformTemplate.affectsHealth,
entityTransformTemplate.affectsBarrier,
);
// Add or remove status effects
return {
statuses: newEntityStatuses,
statusEffects: entityStats.statusEffects, // Placeholder
};
} else if (transformType === "heal") {
const outgoingHeal = entityTransformTemplate.magnitude; // There could be a heal modifier later
// Apply user's status effects to outgoing heal
// Apply receiver's status effects to incoming heal
const newEntityStatuses = applyHealToStatuses(
entityStats.statuses, // DRY alert
outgoingHeal,
entityTransformTemplate.affectsHealth,
entityTransformTemplate.affectsBarrier,
maxStatuses,
);
// Add or remove status effects
return {
statuses: newEntityStatuses,
statusEffects: entityStats.statusEffects, // Placeholder
};
} else {
throw "Unknown entity transform type " + transformType; // Should be never
}
}
/*interface entityTransform extends entityTransformTemplate { /*interface entityTransform extends entityTransformTemplate {
specificEntity?: string, specificEntity?: string,
team: "players" | "enemies", team: "players" | "enemies",
@ -297,28 +307,61 @@ function getEntityByName(entityList: entity[], entityName: string): success<enti
return [false, "Entity not found"]; return [false, "Entity not found"];
} }
function getAbility() {} function getAbility() {}
function applyEntityTransformToEntityStatuses(entityTransform: entityTransformTemplate, statuses: statuses, maxStatuses: statuses) {
}
function applyEntityTransformToEntityList( function applyEntityTransformToEntityList(
entityList: entity[], entityList: entity[],
entityTransformDeterminer: entityTransformDeterminer, entityTransformDeterminer: entityTransformDeterminer,
entityPerformingTransform: entity, entityPerformingTransform: entity,
aim: Vector3, aim: Vector3,
): [entity[], placeholder[]?] { ): [entity[], placeholder[]?] {
const entityPerformingTransformModifiers = entityPerformingTransform.baseModifiers
const entityPerformingTransformStatusEffects = entityPerformingTransform.stats.statusEffects // Not good
const newEntityList = entityList.map(function (entityReceivingTransform: entity): entity { const newEntityList = entityList.map(function (entityReceivingTransform: entity): entity {
const entityTransformTemplate = entityTransformDeterminer( const entityTransform = entityTransformDeterminer(
entityPerformingTransform.id, entityPerformingTransform.id,
entityReceivingTransform.id, entityReceivingTransform.id,
entityPerformingTransform.puppet, entityPerformingTransform.puppet,
entityReceivingTransform.puppet, entityReceivingTransform.puppet,
); );
const newEntityStats = entityTransformTemplate if (entityTransform) {
? transformEntityStats( const entityStatuses = entityPerformingTransform.stats.statuses
entityPerformingTransform, const [entityTransformType, entityTransformTemplate] = entityTransform
entityReceivingTransform.stats, if (entityTransformType === "attack") {
entityTransformTemplate, const outgoingTransformTemplate = applyEntityToAttack(
entityReceivingTransform.baseModifiers, entityPerformingTransformModifiers,
entityReceivingTransform.maxStatuses, entityPerformingTransformStatusEffects,
) entityTransformTemplate
: entityReceivingTransform.stats; );
const newEntityStatuses = outgoingTransformTemplate
? applyAttackToEntityStatuses(
entityStatuses,
entityTransformTemplate,
entityReceivingTransform.baseModifiers,
entityReceivingTransform.stats.statusEffects, // This is also cringe
)
: entityStatuses; // Also cringe
} else {
assert(entityTransformType === "heal")
const outgoingTransformTemplate = applyEntityToHeal( // Lots of DRY violations here
entityPerformingTransformModifiers,
entityPerformingTransformStatusEffects,
entityTransformTemplate
);
const newEntityStatuses = outgoingTransformTemplate
? applyHealToEntityStatuses(
entityStatuses,
entityTransformTemplate,
entityReceivingTransform.baseModifiers,
entityReceivingTransform.maxStatuses,
entityReceivingTransform.stats.statusEffects, // So much cringe
)
: entityStatuses;
}
} else {
return entityReceivingTransform;
}
}); });
return [newEntityList]; return [newEntityList];
} }
@ -386,4 +429,4 @@ class entityManager extends actorClass<entityManagerRequest> {
export function initEntityManager() { export function initEntityManager() {
return new entityManager(); return new entityManager();
} }
*/ */

View file

@ -1,5 +1,5 @@
// "The": Handle events. // "The": Handle events.
import { entity, getEntityByName/*entityManagerRequest /*makeEntity, entityController*/ } from "./EntityManager"; import { entityId, entityProperties, entityStatuses/*entityManagerRequest /*makeEntity, entityController*/ } from "./EntityManager";
import { applyRequestsToPlayerStorage, playerManagerRequest } from "./PlayerManager" import { applyRequestsToPlayerStorage, playerManagerRequest } from "./PlayerManager"
export type sceneManagerRequest = [Player, "useAbility", Vector3] | [Player, "foo", "bar"] export type sceneManagerRequest = [Player, "useAbility", Vector3] | [Player, "foo", "bar"]
type endConditionFunction = (containedScenes: scene[], containedEntities: entity[], timeElapsed: number) => boolean type endConditionFunction = (containedScenes: scene[], containedEntities: entity[], timeElapsed: number) => boolean
@ -8,11 +8,16 @@ export interface sceneTemplate {
readonly onCompletion: readonly playerManagerRequest[] // Requests to get sent out when the scene ends readonly onCompletion: readonly playerManagerRequest[] // Requests to get sent out when the scene ends
} }
export interface scene extends sceneTemplate { export interface scene extends sceneTemplate {
containedScenes?: { /*containedScenes?: {
[sceneName: string]: scene | undefined [sceneName: string]: scene | undefined
}; // Scenes within this scene that are isolated from each other and can be run in parallel }; // Scenes within this scene that are isolated from each other and can be run in parallel
// Not necessarily "A list of events that need to return true (in sequence) to complete this event", but such events would go there // Not necessarily "A list of events that need to return true (in sequence) to complete this event", but such events would go there
containedEntities: entity[]; // A list of entities that need to die to complete the event *///Scene nesting is currently cancelled
containedEntities: string[]; // "A list of entities that need to die to complete the event" (not really but kinda)
containedEntityProperties: {
[entityName: string]: entityProperties
}
containedEntityStatuses
players: { players: {
[userId: number]: [inThisScene: true] | [inThisScene: false, subScene: string] | undefined [userId: number]: [inThisScene: true] | [inThisScene: false, subScene: string] | undefined
} }
@ -21,7 +26,7 @@ export interface scene extends sceneTemplate {
export function runScene(scene: scene, now: number): success<[scene, playerManagerRequest[]]> { export function runScene(scene: scene, now: number): success<[scene, playerManagerRequest[]]> {
return [true, [scene, []]]; return [true, [scene, []]];
} }
function getPlayerSceneName(scene: scene, userId: number): success<string | false> { /*function getPlayerSceneName(scene: scene, userId: number): success<string | false> {
let playerSceneLocation = scene.players[userId]; let playerSceneLocation = scene.players[userId];
if (!playerSceneLocation) { if (!playerSceneLocation) {
return [false, "Player does not exist"]; // Some kind of error needs to go here return [false, "Player does not exist"]; // Some kind of error needs to go here
@ -30,9 +35,9 @@ function getPlayerSceneName(scene: scene, userId: number): success<string | fals
} else { } else {
return [true, playerSceneLocation[1]] return [true, playerSceneLocation[1]]
} }
} }*/
function applyRequestToScene(scene: scene, now: number, request: sceneManagerRequest): [scene, playerManagerRequest[]] { function applyRequestToScene(scene: scene, now: number, request: sceneManagerRequest): [scene, playerManagerRequest[]] {
const playerSceneResult = getPlayerSceneName(scene, request[0].UserId) /*const playerSceneResult = getPlayerSceneName(scene, request[0].UserId)
if (!playerSceneResult[0]) { if (!playerSceneResult[0]) {
return [scene, []]; // Some kind of error needs to go here return [scene, []]; // Some kind of error needs to go here
} }
@ -53,7 +58,7 @@ function applyRequestToScene(scene: scene, now: number, request: sceneManagerReq
containedScenes[playerSceneName] = sceneRequestResult[0] // This is questionably object-oriented containedScenes[playerSceneName] = sceneRequestResult[0] // This is questionably object-oriented
scene.containedScenes = containedScenes scene.containedScenes = containedScenes
return [scene, sceneRequestResult[1]] return [scene, sceneRequestResult[1]]
} }*/
} }
export function applyRequestsToScene(scene: scene, now: number, requests: sceneManagerRequest[]): success<[scene, playerManagerRequest[]]> { export function applyRequestsToScene(scene: scene, now: number, requests: sceneManagerRequest[]): success<[scene, playerManagerRequest[]]> {
try { try {