-
Notifications
You must be signed in to change notification settings - Fork 3
Add ship experience #192
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add ship experience #192
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import type { RequireComponent } from "@core/tsHelpers"; | ||
| import type { BaseComponent } from "./component"; | ||
|
|
||
| export interface Experience extends BaseComponent<"experience"> { | ||
| amount: number; | ||
| rank: number; | ||
| } | ||
|
|
||
| export const ranks = [200, 600, 1400, 3000, 6200, Infinity]; | ||
|
|
||
| export function getRank(exp: number): number { | ||
| return ranks.findIndex((threshold) => exp < threshold); | ||
| } | ||
|
|
||
| export function addExperience( | ||
| entity: RequireComponent<"experience">, | ||
| value: number | ||
| ): void { | ||
| entity.cp.experience.amount += value; | ||
| const newRank = getRank(entity.cp.experience.amount); | ||
| if (newRank > entity.cp.experience.rank) { | ||
| entity.cp.experience.rank = newRank; | ||
|
|
||
| if (entity.hasComponents(["damage"])) { | ||
| entity.cp.damage.modifiers.rank = 0.1 * entity.cp.experience.rank; | ||
| } | ||
| if (entity.hasComponents(["hitpoints"])) { | ||
| entity.cp.hitpoints.hp.modifiers.rank = 0.1 * entity.cp.experience.rank; | ||
| } | ||
| entity.addTag("recalculate:modifiers"); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -3,19 +3,21 @@ import type { BaseComponent } from "./component"; | |||||
|
|
||||||
| export interface HitPoints extends BaseComponent<"hitpoints"> { | ||||||
| hp: { | ||||||
| base: number; | ||||||
| max: number; | ||||||
| value: number; | ||||||
| regen: number; | ||||||
| modifiers: Record<string, number>; | ||||||
| }; | ||||||
| shield?: { | ||||||
| max: number; | ||||||
| value: number; | ||||||
| regen: number; | ||||||
| }; | ||||||
| hit?: boolean; | ||||||
| hitBy: Record<number, number>; // entityId: timestamp | ||||||
| } | ||||||
|
|
||||||
| export function changeHp( | ||||||
| export function subtractHp( | ||||||
| entity: RequireComponent<"hitpoints">, | ||||||
| value: number | ||||||
| ): void { | ||||||
|
|
@@ -25,9 +27,30 @@ export function changeHp( | |||||
| 0, | ||||||
| entity.cp.hitpoints.shield.value - delta | ||||||
| ); | ||||||
| delta -= Math.min(entity.cp.hitpoints.shield.value, value); | ||||||
| delta += Math.min(entity.cp.hitpoints.shield.value, value); | ||||||
|
||||||
| delta += Math.min(entity.cp.hitpoints.shield.value, value); | |
| delta -= Math.min(entity.cp.hitpoints.shield.value, value); |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2,9 +2,18 @@ import type { Sim } from "@core/sim"; | |||||||||||||||||||||||||||||
| import { dumpCargo } from "@core/components/storage"; | ||||||||||||||||||||||||||||||
| import type { Faction } from "@core/archetypes/faction"; | ||||||||||||||||||||||||||||||
| import { entityIndexer } from "@core/entityIndexer/entityIndexer"; | ||||||||||||||||||||||||||||||
| import type { DockSize } from "@core/components/dockable"; | ||||||||||||||||||||||||||||||
| import { addExperience } from "@core/components/experience"; | ||||||||||||||||||||||||||||||
| import { System } from "./system"; | ||||||||||||||||||||||||||||||
| import { transport3D } from "./transport3d"; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| const expValues: Record<DockSize, number> = { | ||||||||||||||||||||||||||||||
| large: 200, | ||||||||||||||||||||||||||||||
| medium: 50, | ||||||||||||||||||||||||||||||
| small: 20, | ||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||
| const timestampThreshold = 120; // 2 minutes | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| export class DeadUnregisteringSystem extends System { | ||||||||||||||||||||||||||||||
| apply = (sim: Sim) => { | ||||||||||||||||||||||||||||||
| super.apply(sim); | ||||||||||||||||||||||||||||||
|
|
@@ -27,8 +36,29 @@ export class DeadUnregisteringSystem extends System { | |||||||||||||||||||||||||||||
| time: this.sim.getTime(), | ||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| if (entity.hasComponents(["position"])) | ||||||||||||||||||||||||||||||
| transport3D.hooks.explode.notify(entity); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| const attackers: number[] = []; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| for (const [attackerId, timestamp] of Object.entries( | ||||||||||||||||||||||||||||||
| entity.cp.hitpoints.hitBy | ||||||||||||||||||||||||||||||
| )) { | ||||||||||||||||||||||||||||||
| if (timestamp + timestampThreshold > this.sim.getTime()) { | ||||||||||||||||||||||||||||||
| attackers.push(Number(attackerId)); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| const exp = | ||||||||||||||||||||||||||||||
| expValues[entity.cp.dockable?.size || "small"] / attackers.length; | ||||||||||||||||||||||||||||||
| for (const attackerId of attackers) { | ||||||||||||||||||||||||||||||
| const attacker = this.sim.get(attackerId); | ||||||||||||||||||||||||||||||
| if (attacker?.hasComponents(["experience"])) { | ||||||||||||||||||||||||||||||
| addExperience(attacker, exp); | ||||||||||||||||||||||||||||||
|
Comment on lines
+53
to
+58
|
||||||||||||||||||||||||||||||
| const exp = | |
| expValues[entity.cp.dockable?.size || "small"] / attackers.length; | |
| for (const attackerId of attackers) { | |
| const attacker = this.sim.get(attackerId); | |
| if (attacker?.hasComponents(["experience"])) { | |
| addExperience(attacker, exp); | |
| if (attackers.length > 0) { | |
| const exp = | |
| expValues[entity.cp.dockable?.size || "small"] / attackers.length; | |
| for (const attackerId of attackers) { | |
| const attacker = this.sim.get(attackerId); | |
| if (attacker?.hasComponents(["experience"])) { | |
| addExperience(attacker, exp); | |
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import type { Sim } from "@core/sim"; | ||
| import { entityIndexer } from "@core/entityIndexer/entityIndexer"; | ||
| import { recalculate as recalculateDamage } from "@core/components/damage"; | ||
| import { recalculate as recalculateHitpoints } from "@core/components/hitpoints"; | ||
| import { System } from "./system"; | ||
|
|
||
| export class ModifierRecalculatingSystem extends System { | ||
| apply(sim: Sim) { | ||
| sim.hooks.phase.start.subscribe( | ||
| this.constructor.name, | ||
| this.recalculateModifiers.bind(this) | ||
| ); | ||
| } | ||
|
|
||
| // eslint-disable-next-line class-methods-use-this | ||
| recalculateModifiers(): void { | ||
| for (const entity of entityIndexer.search([], ["recalculate:modifiers"])) { | ||
| if (entity.hasComponents(["damage"])) { | ||
| recalculateDamage(entity.cp.damage); | ||
| } | ||
| if (entity.hasComponents(["hitpoints"])) { | ||
| recalculateHitpoints(entity.cp.hitpoints); | ||
| } | ||
| entity.removeTag("recalculate:modifiers"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| export const modifierRecalculatingSystem = new ModifierRecalculatingSystem(); |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,26 @@ | ||||||||||||||||||||||||
| import { getRank, ranks } from "@core/components/experience"; | ||||||||||||||||||||||||
| import React from "react"; | ||||||||||||||||||||||||
| import styles from "./styles.scss"; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| export interface ExperienceBarProps { | ||||||||||||||||||||||||
| amount: number; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| export const ExperienceBar: React.FC<ExperienceBarProps> = ({ amount }) => { | ||||||||||||||||||||||||
| const rank = getRank(amount); | ||||||||||||||||||||||||
| const progress = rank === 5 ? 0 : amount / ranks[rank]; | ||||||||||||||||||||||||
| const expLabel = rank === 5 ? "Max Rank" : `${amount}/${ranks[rank]}`; | ||||||||||||||||||||||||
|
Comment on lines
+10
to
+12
|
||||||||||||||||||||||||
| const rank = getRank(amount); | |
| const progress = rank === 5 ? 0 : amount / ranks[rank]; | |
| const expLabel = rank === 5 ? "Max Rank" : `${amount}/${ranks[rank]}`; | |
| const MAX_RANK = ranks.length - 1; | |
| const rank = getRank(amount); | |
| const progress = rank === MAX_RANK ? 0 : amount / ranks[rank]; | |
| const expLabel = rank === MAX_RANK ? "Max Rank" : `${amount}/${ranks[rank]}`; |
Copilot
AI
Jul 20, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another instance of magic number 5. This should use the same constant as suggested for line 11 to ensure consistency.
| const progress = rank === 5 ? 0 : amount / ranks[rank]; | |
| const expLabel = rank === 5 ? "Max Rank" : `${amount}/${ranks[rank]}`; | |
| const progress = rank === MAX_RANK ? 0 : amount / ranks[rank]; | |
| const expLabel = rank === MAX_RANK ? "Max Rank" : `${amount}/${ranks[rank]}`; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The function updates modifiers and adds the recalculate tag even when rank doesn't change. This could cause unnecessary recalculations. Move the modifier updates and tag addition inside the
if (newRank > entity.cp.experience.rank)block.