Skip to content

Instantly share code, notes, and snippets.

@seobyeongky
Created September 23, 2014 02:30
Show Gist options
  • Save seobyeongky/43cbe66fa7d653fa0bb9 to your computer and use it in GitHub Desktop.
Save seobyeongky/43cbe66fa7d653fa0bb9 to your computer and use it in GitHub Desktop.
외곽선 따라 돌아가는 효과?
{advance} = require './utils'
RIGHT = 0
UP = 1
LEFT = 2
DOWN = 3
CCW_DIRECTION = [RIGHT,UP,LEFT,DOWN]
CCW_OFFSET = [[1,0],[0,1],[-1,0],[0,-1]] #right,up,left,down
THE_RIHGTSIDE_POINT_OFFSET = [[1,0],[1,1],[0,1],[0,0]]
HORIZONTAL = 0
VERTICAL = 1
BEGIN_X = 0
BEGIN_Y = 1
END_X = 2
END_Y = 3
get_delta = (direction) -> CCW_OFFSET[direction]
is_overlap = (a,b) ->
a[0] == b[0] && a[1] == b[1]
calc_good_pos = (A,B,C,ratio) ->
r = OUTLINE_LIGHT_RADIUS_RATIO
a_sign_x = A[0] - B[0]
a_sign_y = A[1] - B[1]
c_sign_x = C[0] - B[0]
c_sign_y = C[1] - B[1]
theta = ratio * (Math.PI / 4)
if a_sign_x
x = r * (1 - Math.cos(theta))
y = r * (1 - Math.sin(theta))
else
x = r * (1 - Math.sin(theta))
y = r * (1 - Math.cos(theta))
if a_sign_x
x *= a_sign_x
if a_sign_y
y *= a_sign_y
if c_sign_x
x *= c_sign_x
if c_sign_y
y *= c_sign_y
[B[0] + x, B[1] + y]
get_outline_paths = ->
dmap = {} # Direction map
pop_info = (index) ->
return null if not dmap[index]?
# X Y DIRECTION
info = [index % N, Math.floor(index / N), dmap[index]]
delete dmap[index]
info
# Calcuate Direction map
for x in [0...N]
for y in [0...N]
continue if is_out_of_board(x, y)
continue if is_masked(x, y)
# (x,y) is a point between blocks
# that right-up side block is not masked
for direction in CCW_DIRECTION
delta = CCW_OFFSET[direction]
cx = x + delta[0]
cy = y + delta[1]
continue if not is_masked(cx, cy)
# Calcuate the outline's rightside point
offset = THE_RIHGTSIDE_POINT_OFFSET[direction]
rightside_x = x + offset[0]
rightside_y = y + offset[1]
direction_plus_90 = direction + 1
direction_plus_90 = 0 if direction_plus_90 >= CCW_DIRECTION.length
dmap[rightside_x + rightside_y * N] = direction_plus_90
# Get paths
paths = []
loop
keys = Object.keys(dmap)
break if keys.length == 0
current_path = []
begin = current = pop_info keys[0]
loop
current_path.push [current[0], current[1]]
delta = CCW_OFFSET[current[2]]
next_x = current[0] + delta[0]
next_y = current[1] + delta[1]
# skip out_of_board test because it never happens
# return null if is_out_of_board next_x, next_y
next_index = next_x + next_y * N
next = pop_info next_index
unless next?
current_path.push [next_x, next_y]
break
current = next
paths.push current_path
paths
paths.forEach (path) ->
duration_fade = OUTLINE_LIGHT_DURATION_FADE_RATIO * path.length
duration_exists = OUTLINE_LIGHT_DURATION_EXISTS_RATIO * path.length
light = h.sprite texture:'effects/outline/head.png', scale:OUTLINE_LIGHT_HEAD_SCALE, visible : false
streak = cc.MotionStreak2.create(duration_fade, duration_exists, 0.05, OUTLINE_LIGHT_STROKE, cc.color(255,255,255), "effects/outline/streak.png")
tick = 0
do move = ->
return if stop_outline_effect
index = Math.floor(tick / OUTLINE_LIGHT_TICK_PER_BLOCK)
ratio = (tick % OUTLINE_LIGHT_TICK_PER_BLOCK) / OUTLINE_LIGHT_TICK_PER_BLOCK
if index >= path.length
index = tick = 0
current = path[index]
next = advance path, index + 1
straight = false
if ratio < OUTLINE_LIGHT_RADIUS_RATIO
prev = advance path, index - 1
if same_line prev, current, next
straight = true
else
xy = calc_good_pos prev, current, next, 1 - ratio / OUTLINE_LIGHT_RADIUS_RATIO
x = BLOCK_SIZE * xy[0]
y = BLOCK_SIZE * xy[1]
else if ratio > 1 - OUTLINE_LIGHT_RADIUS_RATIO
next_next = advance path, index + 2
if same_line current, next, next_next
straight = true
else
xy = calc_good_pos current, next, next_next, 1 + (1 - ratio) / OUTLINE_LIGHT_RADIUS_RATIO
x = BLOCK_SIZE * xy[0]
y = BLOCK_SIZE * xy[1]
else
straight = true
if straight
x = BLOCK_SIZE * (current[0] + ratio * (next[0] - current[0]))
y = BLOCK_SIZE * (current[1] + ratio * (next[1] - current[1]))
light.x = streak.x = x
light.y = streak.y = y
tick++
env.scheduler.next_tick move
return
lights.push light
streaks.push streak
env.board_root.addChild streak
env.board_root.addChild light
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment