2017-09-01 16 views
0

私は2つのクラスを持っています、クリーチャークラスはプロパティとしてノード配列を持っています。しかし、生き物も配列で保持されます。私はいずれかのノードから生物のインデックスを取得することができるようにしたい。現在、私ができることは、クリーチャーのインデックスをクリーチャー自身から得ることができることです。私は、クリーチャー・クラス内の集合関数を使って、クリーチャーのクリーチャー・インデックス番号を設定し、すべてのノードのクリーチャー・インデックス番号を同じ数として設定しようとしています。配列のネストされたクラス、外側の配列のインデックスを取得したい

//Node class 
 
class Node { 
 
    constructor(x, y, r, color, highlight, highlightColor) { 
 
     this.x = x; 
 
     this.y = y; 
 
     this.r = r || 20; 
 
     this.color = color || "#ff0"; 
 
     this.highlight = highlight || false; 
 
     this.highlightColor = highlightColor || "#0000FF"; 
 
    } 
 
} 
 

 
// Creature class 
 
class Creature { 
 
    constructor(nodes, muscles, nodeColors) { 
 
     this.nodes = nodes; 
 
     this.muscles = muscles; 
 
     this.nodeColors = nodeColors || "#ff0"; 
 

 
     Object.defineProperties(this, { 
 

 
      nodesArray: { 
 
       "get": (i) => this.nodes[i], 
 
       "set": nodes => { 
 
        this.nodes[i] = newNode; 
 
       } 
 
      }, 
 

 
      musclesArray: { 
 
       "get": (i) => this.nodes[i], 
 
       "set": muscles => { 
 
        this.muscles[i] = newMuscle; 
 
       } 
 
      }, 
 
      creatureNumber: { 
 
       "get":() => creatures.indexOf(this), 
 
      } 
 
     }); 
 
    } 
 
} 
 

 
var nodes = [ 
 
    new Node(100, 100), 
 
    new Node(200, 200) 
 
]; 
 

 
var creatures = [ 
 
    new Creature(nodes, muscles) 
 
];

+0

私は数回あなたの質問を読んでいると私はあなたがTPが達成したいのかわからないです。あなたはおそらく小さなサンプル配列を追加し、それに基づいて何を得る必要があるのか​​を説明できますか? – Thijs

+0

あなたの問題を解決し、より良い性能を同時に得るためには、「array.indexOf」があまり効率的でないため、Creatureオブジェクトにidを明示的に格納することを検討する必要があります(O(N))。 – Joel

+0

@Thijs配列はスニペットにあります。基本的には、私はこれらの2つの配列を持っています。それぞれがそれぞれのクラスを拡張します。そして各クリーチャーは一連のノードを保持します。私がしたいことは、これらのノードのうちの1つしか取ることができず、他のクリーチャーの配列の中のクリーチャーのインデックスをつかむことができることです。これが理にかなってほしい。 – Chenny

答えて

1

私は少しあなたのクラスを変更(あなたは彼らが考えるほどではないにも必ずあなたが仕事)と生き物を管理するために、余分なクラスを追加しました。私は配列とのあなたのセットアップがうまくいくとは思わない。

//Node class 
 
class Node { 
 
    constructor(x, y, r, color, highlight, highlightColor) { 
 
     this.x = x; 
 
     this.y = y; 
 
     this.r = r || 20; 
 
     this.color = color || "#ff0"; 
 
     this.highlight = highlight || false; 
 
     this.highlightColor = highlightColor || "#0000FF"; 
 
    } 
 

 
    /* === PROPERTY: parentCreature === */ 
 
    get parentCreature() { 
 
    return this._parentCreature; 
 
    } 
 
    set parentCreature(creature) { 
 
    this._parentCreature = creature; 
 
    } 
 
    
 
    
 
    /* === METHODS: removeFromCreature === */ 
 
    removeFromCreature() { 
 
    this._parentCreature = null; 
 
    } 
 

 
} 
 

 
function setParentForNodes() { 
 
    this.nodesArray.forEach(node => { 
 
    node.parentCreature = this; 
 
    }); 
 
} 
 

 
// Creature class 
 
class Creature { 
 
    /* === CONSTRUCTOR === */ 
 
    constructor(nodes, muscles, nodeColors) { 
 
     this.nodes = nodes; 
 
     this.muscles = muscles; 
 
     this.nodeColors = nodeColors || "#ff0"; 
 
     
 
     setParentForNodes.call(this); 
 
    } 
 

 

 
    /* === PROPERTY: nodesArray === */ 
 
    get nodesArray() { 
 
     return this.nodes; 
 
    } 
 
    set nodesArray(value) { 
 
     this.nodes = value; 
 
     setParentForNodes.call(this); 
 
    } 
 
    
 
    /* === PROPERTY: musclesArray === */ 
 
    get musclesArray() { 
 
     return this.musclesArray; 
 
    } 
 
    set musclesArray(value) { 
 
     this.musclesArray = value; 
 
    } 
 
    
 
    
 
    /* === METHODS: removeNodes === */ 
 
    removeNodes() { 
 
     this.nodes.forEach(node => { 
 
     node.parentCreature = null; 
 
     }); 
 
     this.nodes = null; 
 
    } 
 
} 
 

 

 

 
class Creatures { 
 
    /* === CONSTRUCTOR === */ 
 
    constructor(creaturesArray = []) { 
 
    this.creatures = new Map(); 
 
    
 
    creaturesArray.forEach(creature => { 
 
     this.creatures.set(creature.id, creature.model); 
 
    }); 
 
    } 
 
    
 
    /* === METHOD: addCreature === */ 
 
    addCreature(id, model) { 
 
    if (this.creatures.has(id)) { 
 
     console.log('Creature ID already exists'); 
 
     return; 
 
    } 
 
    
 
    this.creatures.set(id, model); 
 
    } 
 

 
    /* === METHOD: getCreatureById === */ 
 
    getCreatureById(id) { 
 
    if (this.creatures.has(id)) { 
 
     return this.creatures.get(id); 
 
    } 
 
    
 
    return null; 
 
    } 
 
} 
 

 
// Create the nodes 
 
var nodes = [ 
 
    new Node(100, 100), 
 
    new Node(200, 200) 
 
]; 
 

 
// Create the Goblin with the nodes. 
 
var creatures = new Creatures([ 
 
    { 
 
     id: 'goblin', 
 
     model: new Creature(nodes, 'muscles') 
 
    } 
 
]); 
 

 

 
// Create the dwarf, it has no nodes 
 
creatures.addCreature('dwarf', new Creature([], 'muscles')); 
 

 

 
const 
 
    // Get the parent creature for the first node. 
 
    parentCreatureForNode = nodes[0].parentCreature, 
 
    // Get the creature instance for the dwarf. 
 
    dwarf = creatures.getCreatureById('dwarf'); 
 
    
 
// Remove the nodes from the parent of the first node. 
 
parentCreatureForNode.removeNodes(); 
 
// Assign the nodes to the dwarf. 
 
dwarf.nodesArray = nodes; 
 

 
// The goblin should report it has no nodes. 
 
console.log(creatures.getCreatureById('goblin')); 
 
// The dwarf should log it has 2 nodes. 
 
console.log(creatures.getCreatureById('dwarf')); 
 
// Make sure the node reports its parent is the dwarf. 
 
console.log(nodes[0].parentCreature === dwarf);

+0

助けてくれてありがとう、私は後でこのコードのいくつかを使用すると思います。しかし、見て、私はこれを使用する場合、私はまだ同じ問題に遭遇すると思う。私は基本的に2つのノードを結びつけようとしていますが、いったんそれらが付くと、それらは両方とも同じ生き物IDの下にあります。だから、私は、各ノードが、彼らがどのような生き物の一部の財産を保持することができなければならないと思っています。私がそれを呼び出すと、ただ1つのクリーチャーを削除し、そのクリーチャーから他のクリーチャーへのすべてのノードと筋肉を追加することができます。 – Chenny

+0

私は答えを変えました...私は今あなたが望むことをしていると思います。 – Thijs

+0

私は実際にはsetParentForNodes関数を使用するだけで済みましたが、コードをクリーンアップするために他の提案を引き受けることになりました。ありがとうございます! – Chenny

関連する問題