Intersecting_Circles/intersecting-circles-modifiers/modifiers/move/move.wren
2021-12-14 12:52:41 +01:00

72 lines
1.9 KiB
Text

import "luxe: io" for IO
import "luxe: world" for World, Entity, Modifiers, ModifierSystem, Transform
import "luxe: render" for Render
import "modifiers/circle/circle" for Circle
//User facing API
//This is what the user of your modifier will interact with
class Move {
static create(entity) { Modifiers.create(This, entity) }
static destroy(entity) { Modifiers.destroy(This, entity) }
static has(entity) { Modifiers.has(This, entity) }
static set_velocity(entity, velocity) {
var data = Modifiers.get(This, entity)
data.velocity = velocity
}
} //Move
//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 MoveSystem is ModifierSystem {
construct new() {
//called when your system is first created.
}
init(world) {
_world = world
//called when your modifier is created in a world
}
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) {
//called usually once every frame.
//called when the world that this modifier lives in, is ticked
var width = Render.window_w()
var height = Render.window_h()
each {|entity, data|
var pos = Transform.get_pos(entity)
var radius = Circle.get_radius(entity)
//update pos
pos.x = pos.x + data.velocity.x * delta
pos.y = pos.y + data.velocity.y * delta
if(pos.x < -radius) pos.x = pos.x + width + radius*2
if(pos.x > width + radius) pos.x = pos.x - width - radius*2
if(pos.y < -radius) pos.y = pos.y + height + radius*2
if(pos.y > height + radius) pos.y = pos.y - height - radius*2
//set pos
Transform.set_pos(entity, pos.x, pos.y, pos.z)
}
} //tick
} //MoveSystem
var Modifier = MoveSystem //required