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

113 lines
No EOL
3.4 KiB
Text

import "luxe: world" for Entity, Transform, World
import "luxe: draw" for Draw, PathStyle
import "random" for Random
import "luxe: math" for Math
import "luxe: color" for Color
import "luxe: render" for Render
import "util" for Util
class CircleData{
construct new(entity: Entity, velocity: List, radius: Num){
_entity = entity
_radius = radius
_velocity = velocity
}
entity{_entity}
velocity{_velocity}
radius{_radius}
}
class CircleManager{
construct new(world: World){
_world = world
_circles = []
_rng = Random.new()
_draw = Draw.create(World.render_set(world))
//setup draw styles
_circle_style = PathStyle.new()
_circle_style.color = Color.white
_circle_style.thickness = 1
_intersection_style = PathStyle.new()
_intersection_style.color = Color.hex(0xFFFF99)
_intersection_style.thickness = 2
}
spawn_circles(count: Num){
for(i in 0...count){
//entity/transform setup
var circle_ent = Entity.create(_world)
Transform.create(circle_ent)
var width = Render.window_w()
var height = Render.window_h()
Transform.set_pos(circle_ent, _rng.float(0, width), _rng.float(0, height), 0)
//external wren class setup
var velocity = [_rng.float(-20, 20), _rng.float(-20, 20)]
var radius = _rng.float(20, 100)
var data = CircleData.new(circle_ent, velocity, radius)
_circles.add(data)
}
}
update(delta: Num){
var width = Render.window_w()
var height = Render.window_h()
//move and basic draw
_circles.each{|circle: CircleData|
//get pos
var pos = Transform.get_pos(circle.entity)
//update pos
pos.x = pos.x + circle.velocity.x * delta
pos.y = pos.y + circle.velocity.y * delta
if(pos.x < -circle.radius) pos.x = pos.x + width + circle.radius*2
if(pos.x > width + circle.radius) pos.x = pos.x - width - circle.radius*2
if(pos.y < -circle.radius) pos.y = pos.y + height + circle.radius*2
if(pos.y > height + circle.radius) pos.y = pos.y - height - circle.radius*2
//set pos
Transform.set_pos(circle.entity, pos.x, pos.y, pos.z)
//per-ring drawing
Draw.ring(_draw, pos.x, pos.y, pos.z, circle.radius, circle.radius, 0, 360, 8, _circle_style)
}
//intersections
//iterate all circle indices
for(i in 0..._circles.count){
//and then also all again, up to the current one (that way we have all combinations)
for(ii in 0...i){
//get needed data
var one = _circles[i]
var other = _circles[ii]
var one_pos = Transform.get_pos(one.entity)
var other_pos = Transform.get_pos(other.entity)
//get intersections
var intersections = Util.circle_intersect_circle(one_pos, one.radius, other_pos, other.radius)
if(intersections){ //and if we found any, draw them
Draw.line(_draw, intersections[0].x, intersections[0].y, intersections[1].x, intersections[1].y, 0, _intersection_style)
Draw.circle(_draw, intersections[0].x, intersections[0].y, 0, 8, 8, [1, 1, 1, 0.2])
Draw.circle(_draw, intersections[1].x, intersections[1].y, 0, 8, 8, [1, 1, 1, 0.2])
Draw.circle(_draw, intersections[0].x, intersections[0].y, 0, 3, 3, [1, 1, 1, 1])
Draw.circle(_draw, intersections[1].x, intersections[1].y, 0, 3, 3, [1, 1, 1, 1])
}
}
}
Draw.commit(_draw)
}
dispose(){
//clean up entities and draw context
Draw.destroy(_draw)
_circles.each{|circle|
Entity.destroy(circle.entity)
}
}
}