main #48
@ -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 ) {
 | 
			
		||||
 | 
			
		||||
@ -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);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										37
									
								
								src/app/project/game-model/utils/ItemgroupUtilities.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								src/app/project/game-model/utils/ItemgroupUtilities.ts
									
									
									
									
									
										Normal 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 [];
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user