Created
January 30, 2019 01:53
-
-
Save DoctorGester/9fe41b7a4556239484ca93d75eea9e45 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function highlight_outline(cell_index_to_highlight: boolean[], color: XYZ) { | |
const cell_index_to_edges: Array<{ edge: Edge, from: XY, to: XY, deleted: boolean }[]> = []; | |
const unique_edges: { edge: Edge, from: XY, to: XY, deleted: boolean }[] = []; | |
function merge_edges(at: XY, going_towards: Edge, right_relative: number | undefined, left_relative: number | undefined, index: number) { | |
const right_neighbor = right_relative && cell_index_to_edges[right_relative]; | |
const right_edge = right_neighbor && right_neighbor.find(old => old.edge == going_towards); | |
const left_neighbor = left_relative && cell_index_to_edges[left_relative]; | |
const left_edge = left_neighbor && left_neighbor.find(old => old.edge == going_towards); | |
if (right_edge && left_edge) { | |
right_edge.to = left_edge.to; | |
left_edge.deleted = true; | |
cell_index_to_edges[index].push(right_edge); | |
} else { | |
if (right_edge) { | |
right_edge.to = at; | |
cell_index_to_edges[index].push(right_edge); | |
} | |
if (left_edge) { | |
left_edge.from = at; | |
cell_index_to_edges[index].push(left_edge); | |
} | |
} | |
if (!right_edge && !left_edge) { | |
const new_edge = { edge: going_towards, from: at, to: at, deleted: false }; | |
cell_index_to_edges[index].push(new_edge); | |
unique_edges.push(new_edge); | |
} | |
} | |
for (let index = 0; index < cell_index_to_highlight.length; index++) { | |
const is_highlighted = cell_index_to_highlight[index]; | |
if (!is_highlighted) continue; | |
const cell = battle.cells[index]; | |
const at = cell.position; | |
const right = grid_cell_index_raw(battle, at.x + 1, at.y); | |
const left = grid_cell_index_raw(battle, at.x - 1, at.y); | |
const top = grid_cell_index_raw(battle, at.x, at.y + 1); | |
const bottom = grid_cell_index_raw(battle, at.x, at.y - 1); | |
const edge_side_right_left: [Edge, number | undefined, number | undefined, number | undefined][] = [ | |
[ Edge.top, top, right, left ], | |
[ Edge.bottom, bottom, left, right ], | |
[ Edge.right, right, bottom, top ], | |
[ Edge.left, left, top, bottom ] | |
]; | |
for (const [ edge, side, right, left ] of edge_side_right_left) { | |
if (side == undefined || !cell_index_to_highlight[side]) { | |
if (!cell_index_to_edges[index]) { | |
cell_index_to_edges[index] = []; | |
} | |
merge_edges(cell.position, edge, right, left, index); | |
} | |
} | |
} | |
const half = battle_cell_size / 2; | |
const height = 160; | |
for (const { edge, from, to, deleted } of unique_edges) { | |
if (deleted) continue; | |
const fx = Particles.CreateParticle("particles/ui/highlight_rope.vpcf", ParticleAttachment_t.PATTACH_CUSTOMORIGIN, 0); | |
const [fr_x, fr_y, fr_z] = battle_position_to_world_position_center(from); | |
const [to_x, to_y, to_z] = battle_position_to_world_position_center(to); | |
switch (edge) { | |
case Edge.bottom: { | |
Particles.SetParticleControl(fx, 0, [fr_x - half, fr_y - half, fr_z + height]); | |
Particles.SetParticleControl(fx, 1, [to_x + half, to_y - half, to_z + height]); | |
break; | |
} | |
case Edge.top: { | |
Particles.SetParticleControl(fx, 0, [fr_x + half, fr_y + half, fr_z + height]); | |
Particles.SetParticleControl(fx, 1, [to_x - half, to_y + half, to_z + height]); | |
break; | |
} | |
case Edge.left: { | |
Particles.SetParticleControl(fx, 0, [fr_x - half, fr_y + half, fr_z + height]); | |
Particles.SetParticleControl(fx, 1, [to_x - half, to_y - half, to_z + height]); | |
break; | |
} | |
case Edge.right: { | |
Particles.SetParticleControl(fx, 0, [fr_x + half, fr_y - half, fr_z + height]); | |
Particles.SetParticleControl(fx, 1, [to_x + half, to_y + half, to_z + height]); | |
break; | |
} | |
} | |
Particles.SetParticleControl(fx, 2, color); | |
register_particle_for_reload(fx); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment