Skip to content

Instantly share code, notes, and snippets.

@ironchefpython
Created April 23, 2012 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ironchefpython/2471570 to your computer and use it in GitHub Desktop.
Save ironchefpython/2471570 to your computer and use it in GitHub Desktop.
Sample tool definition
game.modManager.registerEvent({
"type": "custom",
"properties": {"foo": game.modManager.StringType }
});
game.addEventListener("custom", function(event) { console.log(event.get("foo")); });
game.eventManager.propogateEvent("custom", {"foo": "bar"});
var mm = game.modManager;
var ItemComponent = mm.registerPrototype({
"id": "item",
"properties": {
"name": game.modManager.stringType,
"renderWithIcon": game.modManager.booleanType,
"stack_size": game.modManager.numberType,
"texture": game.modManager.textureType
}
});
var Material = mm.registerPrototype({
"id": "material",
"properties": {
"color": game.modManager.colorType,
"hardness": game.modManager.numberType
}
});
var Metal = mm.registerPrototype(Material, {
"id": "metal"
});
console.log(Metal.type);
console.log(game.modManager.getPrototype("metal").type);
var MetalIngot = mm.registerPrototype(ItemComponent, {
"id": "metal_ingot",
"name": "${metal.name} Ingot",
"renderWithIcon": true,
"stack_size": 64,
"properties": {
"material": Metal.type
},
"events": {
"init": function(event) {
var $this = event.target;
$this.texture = game.colorizeTexture("ingot.png", $this.metal.color);
}
}
});
Iron = mm.registerPrototype(Metal, {
"id": "iron",
"name": "Iron",
"color": "#E6E7E8",
"hardness": 4,
"durability": 5
});
Copper = mm.registerPrototype(Metal, {
"id": "copper",
"name": "Copper",
"color": "#B87333",
"hardness": 3,
"durability": 5
});
var Tool = mm.registerPrototype(ItemComponent, {
"id": "tool",
"properties": {
"material": Material.type,
"durability": game.modManager.numberType,
"bonus_target": game.modManager.functionType,
"bonus": function() {
return this.hardness;
},
},
"events": {
"init": function(event) {
var $this= event.target;
$this.durability = Math.pow($this.material.durability, 3);
},
"use": function(event) {
var $this = event.equipped;
if ($this.bonus_target(event.target)) {
event.action.time /= $this.bonus;
}
},
"harvest": function(event) {
var $this = event.equipped;
$this.durability--;
if ($this.durability == 0) {
$this.dispatchEvent("break");
}
},
"break": function(event) {
event.target.destroy();
},
}
});
var Axe = game.modManager.registerPrototype(Tool, {
"id" : "axe",
"components": {
"item": {
"name" : "${material.name} Axe",
"renderWithIcon": true,
"stack_size": 1,
},
"craftable": {
"recipe": "$material under $material over $material over blah",
},
"tool": {
"bonus_target": function(obj) {
return obj.hasComponent(game.modManager.registerPrototype("vanilla.timber"))
}
}
},
"events": {
"init": function(event) {
var $this = event.target;
$this.texture = game.colorizeTexture("axe.png", $this.material.color);
}
}
});
CopperIngot = MetalIngot.registerPrototype({"id": "copper_ingot", "material": Copper});
IronIngot = MetalIngot.registerPrototype({"id": "iron_ingot", "material": Iron});
CopperAxe = Axe.registerPrototype({"id": "copper_axe", "material": Copper});
IronAxe = Axe.registerPrototype({"id": "iron_axe", "material": Iron});
var MetalOre = game.modManager.registerPrototype({
"id": "metal_ore",
"listeners": {
"harvest": function(event) {
game.addItem(this.getProperty("drop"), event.location);
event.block.location.world.dropItem(this, event.block.location);
},
},
"properties": {
"smelt_to": "metal_ingot"
},
"components": { "item": {} }
});
console.log("The registered components are: " + [c.id for each (c in game.modManager.components)]);
console.log("The color of Copper is : " + game.modManager.getComponent("copper").getPropertyValue("color"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment