HexCardsClient/modifiers/slot/slot.wren
2021-05-24 09:40:34 +02:00

104 lines
2.8 KiB
Text

import "luxe: io" for IO
import "luxe: world" for World, Entity, Modifiers, ModifierSystem, Transform
import "utils/hex" for Hex
import "utils/vec" for Vec
import "modifiers/field/field" for Field
import "luxe: assets" for Strings
//User facing API
//This is what the user of your modifier will interact with
class Slot {
static create(entity) { Modifiers.create(This, entity) }
static destroy(entity) { Modifiers.destroy(This, entity) }
static has(entity) { Modifiers.has(This, entity) }
static set_coord(entity, coord){
var data = Modifiers.get(This, entity)
data.coord = coord
}
static get_coord(entity, coord){
var data = Modifiers.get(This, entity)
return data.coord
}
} //Slot
//Your modifier system implementation.
//This speaks to the engine and your user facing API
//to do the actual work. You'll get notified when things change
//in the world and respond to them here.
class SlotSystem is ModifierSystem {
construct new() {
//called when your system is first created.
}
init(world) {
_world = world
//called when your modifier is created in a world
if(World.tag_has(_world, "edit")) _positions = {}
}
destroy() {
//called when your modifier is removed from a world
}
attach(entity, data) {
//called when attached to an entity
}
detach(entity, data) {
//called when detached from an entity, like on destroy
}
tick(delta) {
if(!World.tag_has(_world, "edit")) return //only in editor pls
//called usually once every frame.
//called when the world that this modifier lives in, is ticked
each {|entity, data|
var distance = 50
var scale = 1
find_entity(entity, Strings.get(data.field))
.then{|field|
distance = Field.get_distance(field)
scale = Field.get_scale(field)
}
var pos = Transform.get_pos(entity)
if(!Vec.approximately(_positions[entity], pos)){
var coord = Hex.pos_to_coord(pos, distance)
coord = Hex.round(coord)
data.coord = coord
pos = Hex.coord_to_pos(coord, distance)
Transform.set_pos(entity, pos.x, pos.y, pos.z)
_positions[entity] = pos
if(data.auto_naming){
var name = "Slot_(%(coord.x),%(coord.y),%(coord.z))"
if(Entity.get_name(entity) != name){
var num = 1
while(Entity.valid(Entity.get_named(_world, name)) && Entity.get_named(_world, name) != entity){
num = num + 1
name = "Slot_(%(coord.x),%(coord.y),%(coord.z))_%(num)"
}
Entity.set_name(entity, name)
}
}
} else {
//we could optimize this by not doing it every frame... ...in theory
pos = Hex.coord_to_pos(data.coord, distance)
Transform.set_pos(entity, pos.x, pos.y, pos.z)
_positions[entity] = pos
}
Transform.set_scale(entity, scale, scale, 1)
}
} //tick
} //SlotSystem
var Modifier = SlotSystem //required