Hi,
It’s kind of lame to say this, but I just discovered compositing rather than using prototyping. Here’s a quick example :
import * as THREE from 'https://cdn.jsdelivr.net/npm/three@0.117.1/build/three.module.js'
const can_fight = (state) => ({
fight: () => {
console.log(`${state.name} slashes at the foe!`);
state.stamina--;
}
})
export const mage = (name) => {
let state = {
name,
health: 100,
mana: 100,
geo: new THREE.BoxGeometry(5, 1, 500),
mat: new THREE.MeshPhongMaterial({
color: "#000",
specular: "#444",
emissive: "#444",
shininess: 0,
}),
scene: scene,
}
state.mesh = new THREE.Mesh(state.geo, state.mat);
state.scene.add(state.mesh)
return Object.assign(
state,
can_fight(state),
);
}
let player = mage("mage")
player.fight()
What I would like to do is to have the possibility to directly invoke the ability “fight” in the creation of the object. Obviously if I do this, it doesn’t work. Is there a way to do this?
import * as THREE from 'https://cdn.jsdelivr.net/npm/three@0.117.1/build/three.module.js'
const can_fight = (state) => ({
fight: () => {
console.log(`${state.name} slashes at the foe!`);
state.stamina--;
}
})
export const mage = (name) => {
let state = {
name,
health: 100,
mana: 100,
geo: new THREE.BoxGeometry(5, 1, 500),
mat: new THREE.MeshPhongMaterial({
color: "#000",
specular: "#444",
emissive: "#444",
shininess: 0,
}),
scene: scene,
}
state.mesh = new THREE.Mesh(state.geo, state.mat);
state.scene.add(state.mesh)
return Object.assign(
state,
can_fight(state),
);
//////////////////////////////////////////////////////////////////////////////
// HERE WHAT I WOULD LIKE TO DO DIRECTLY IN MY OBJECT
state.fight()
//////////////////////////////////////////////////////////////////////////////
}
let player = mage("mage")
//////////////////////////////////////////////////////////////////////////////
TO AVOID THIS :
// player.fight()
//////////////////////////////////////////////////////////////////////////////