Items should inherit characteristics from older ancestors too
All checks were successful
E2E Testing / test (push) Successful in 1m30s

This commit is contained in:
Sebastian Böckelmann 2024-05-07 15:15:24 +02:00
parent 6eff107282
commit 0b1a3806f8
3 changed files with 48 additions and 3 deletions

View File

@ -15,6 +15,7 @@ import {ItemGroup} from "./inventory/ItemGroup";
import {AbstractItemGroup} from "./inventory/AbstractItemGroup";
import {ConcreteItemGroup} from "./inventory/ConcreteItemGroup";
import {Item} from "./inventory/Item";
import {ItemgroupUtilities} from "./utils/ItemgroupUtilities";
export class GameModel {
gameModelName: string
@ -65,10 +66,15 @@ export class GameModel {
addItem(itemName: string, conceteItemgroupName: string) {
const itemgroup = GameModel.findItemgroupByName(conceteItemgroupName, this.itemgroups);
if(itemgroup instanceof ConcreteItemGroup) {
itemgroup.addItem(new Item(itemName, "", ModelComponentType.ITEM ))
const itemgroups: ItemGroup[] = ItemgroupUtilities.findItemgroupPathToItemgroup(conceteItemgroupName, this.itemgroups);
if(itemgroups.length > 0) {
itemgroup.addItem(new Item(itemName, "", ModelComponentType.ITEM ), itemgroups)
}
}
}
public static findItemgroupByName(name: string, itemgroups: ItemGroup[]) {
const itemgroupQueue: ItemGroup[] = itemgroups.concat();
while(itemgroupQueue.length > 0 ) {

View File

@ -5,9 +5,11 @@ export class ConcreteItemGroup extends ItemGroup {
items: Item[] = [];
addItem(item: Item) {
addItem(item: Item, parentItemgroups: ItemGroup[]) {
if(this.findItemByName(item.componentName) == undefined) {
item.addInheritedGroup(this);
parentItemgroups.forEach(itemgroup => {
item.inheritedGroups.push(itemgroup);
})
this.items.push(item);
}
}

View File

@ -0,0 +1,37 @@
import {GameModel} from "../GameModel";
import {ItemGroup} from "../inventory/ItemGroup";
import {AbstractItemGroup} from "../inventory/AbstractItemGroup";
export class ItemgroupUtilities {
public static findItemgroupPathToItemgroup(searchedItemgroupName: string, itemgroups: ItemGroup[]): ItemGroup[] {
for(let i=0; i<itemgroups.length; i++) {
const found_path = this.findItemgroupPathToItemgroupRecursive(itemgroups[i], searchedItemgroupName, []);
if(found_path.length > 0) {
return found_path;
}
}
return [];
}
private static findItemgroupPathToItemgroupRecursive(root: ItemGroup, searchedItemgroupName: string, path: ItemGroup[]): ItemGroup[] {
path.push(root);
if(root.componentName === searchedItemgroupName) {
return path;
}
if(root instanceof AbstractItemGroup) {
for(let i=0; i<root.children.length; i++) {
const found_path = this.findItemgroupPathToItemgroupRecursive(root.children[i], searchedItemgroupName, path.concat())
if(found_path != undefined && found_path.length > 0) {
return found_path;
}
}
return [];
} else {
return [];
}
}
}