Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add crossover rendering #1739

Merged
merged 2 commits into from
Sep 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion assets/app/view/game/part/track_node_path.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

require 'lib/settings'
require 'view/game/part/base'
require 'view/game/part/town_location'

Expand All @@ -8,13 +9,18 @@ module Game
module Part
class TrackNodePath < Base
include TownLocation
include Lib::Settings

needs :tile
needs :path
needs :color, default: 'black'
needs :width, default: 8
needs :dash, default: '0'

CROSSOVER_GAP = 3
STRAIGHT_CROSSOVER = '1 55 63 56'
GENTLE_CROSSOVER = '1 55 47 56'

PARALLEL_SPACING = [8, 6, 4].freeze

EDGE_PERP_ANGLES = [90, 30, -30, -90, -150, 150].freeze
Expand Down Expand Up @@ -256,6 +262,13 @@ def load_from_tile
@end_x = edge_x_pos(@end_edge, 87)
@end_y = edge_y_pos(@end_edge, 87)
lanes = @path.lanes

if @tile.crossover? && @path.straight?
@crossover_dash = STRAIGHT_CROSSOVER
elsif @tile.crossover? && @path.gentle_curve?
@crossover_dash = GENTLE_CROSSOVER
end

elsif @num_exits == 1
@begin_edge = @path.exits[0]
@begin_x = edge_x_pos(@begin_edge, 87)
Expand All @@ -281,6 +294,13 @@ def load_from_tile
end
lanes = @path.lanes
lanes.reverse! if @path.b.edge?

if @tile.crossover? && @path.straight?
@crossover_dash = STRAIGHT_CROSSOVER
elsif @tile.crossover? && @path.gentle_curve?
@crossover_dash = GENTLE_CROSSOVER
end

else
# city/town - city/town
@ct_edge0 = @tile.preferred_city_town_edges[@stop0] if @stop0
Expand Down Expand Up @@ -411,7 +431,17 @@ def render_part
'stroke-dasharray': @dash,
) if @terminal

h(:path, props)
children = [h(:path, props)]
if @crossover_dash
props[:attrs].merge!(
stroke: setting_for(@tile.color),
'stroke-width': @width + CROSSOVER_GAP * 2,
'stroke-dasharray': @crossover_dash,
'stroke-dashoffset': 1,
)
children.prepend(h(:path, props))
end
children
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/engine/config/tile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ module Tile
'988' => 'city=revenue:50,slots:3;path=a:0,b:_0,track:narrow;path=a:1,b:_0,track:narrow;path=a:2,b:_0,track:narrow;path=a:3,b:_0,track:narrow;path=a:4,b:_0,track:narrow',
'989' => 'city=revenue:70,slots:3;path=a:0,b:_0,track:narrow;path=a:1,b:_0,track:narrow;path=a:2,b:_0,track:narrow;path=a:3,b:_0,track:narrow;path=a:4,b:_0,track:narrow',
'990' => 'city=revenue:100,slots:3;path=a:0,b:_0,track:narrow;path=a:1,b:_0,track:narrow;path=a:2,b:_0,track:narrow;path=a:3,b:_0,track:narrow;path=a:4,b:_0,track:narrow;path=a:5,b:_0,track:narrow;label=B',
'1167' => 'city=revenue:70;city=revenue:70;path=a:0,b:_0;path=a:_0,b:1;path=a:2,b:_1;path=a:_1,b:3;path=a:4,b:_0;path=a:5,b:_1',
'1167' => 'city=revenue:70,loc:0;city=revenue:70,loc:3;path=a:0,b:_0;path=a:_0,b:1;path=a:2,b:_1;path=a:_1,b:3;path=a:4,b:_0;path=a:5,b:_1',
'1168' => 'city=revenue:60,slots:3;path=a:0,b:_0;path=a:1,b:_0;path=a:2,b:_0;path=a:3,b:_0',
}.freeze

Expand Down
29 changes: 26 additions & 3 deletions lib/engine/part/path.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,21 +98,44 @@ def path?
end

def node?
@_node ||= @nodes.any?
return @_node if defined?(@_node)

@_node = @nodes.any?
end

def terminal?
@_terminal ||= !!@terminal
!!@terminal
end

def single?
@_single ||= @lanes.first[0] == 1 && @lanes.last[0] == 1
return @_single if defined?(@_single)

@_single = @lanes.first[0] == 1 && @lanes.last[0] == 1
end

def exits
@exits ||= @edges.map(&:num)
end

def straight?
return @_straight if defined?(@_straight)

ct_edge = @tile.preferred_city_town_edges[@nodes.first] if @nodes.one?
a_num = @a.edge? ? @a.num : ct_edge
b_num = @b.edge? ? @b.num : ct_edge

@_straight = a_num && b_num && (a_num - b_num).abs == 3
end

def gentle_curve?
return @_gentle_curve if defined?(@_gentle_curve)

ct_edge = @tile.preferred_city_town_edges[@nodes.first] if @nodes.one?
a_num = @a.edge? ? @a.num : ct_edge
b_num = @b.edge? ? @b.num : ct_edge
@_gentle_curve = a_num && b_num && (((d = (a_num - b_num).abs) == 2) || d == 4 || d == 2.5 || d == 3.5)
end

def rotate(ticks)
path = Path.new(@a.rotate(ticks), @b.rotate(ticks), @terminal, @lanes)
path.index = index
Expand Down
33 changes: 33 additions & 0 deletions lib/engine/tile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,39 @@ def compute_city_town_edges
ct_edges
end

# this is invariant over rotations
def crossover?
return @_crossover if defined?(@_crossover)

@_crossover = compute_crossover
end

def compute_crossover
edge_paths = Hash.new { |h, k| h[k] = [] }
paths.each do |p|
edge_paths[p.a.num] << p if p.a.edge?
edge_paths[p.b.num] << p if p.b.edge?
end

paths.each do |p|
next if p.nodes.size > 1

ct_edge = preferred_city_town_edges[p.nodes.first] if p.nodes.one?
a_num = p.a.edge? ? p.a.num : ct_edge
b_num = p.b.edge? ? p.b.num : ct_edge

if p.straight?
return true if edge_paths[(a_num + 1) % 6].any?(&:straight?)
return true if edge_paths[(a_num - 1) % 6].any?(&:straight?)
elsif p.gentle_curve?
low = [a_num, b_num].min
middle = (a_num - b_num).abs == 2 ? (low + 1) % 6 : (low - 1) % 6
return true if edge_paths[middle].any? { |ep| ep.straight? || ep.gentle_curve? }
end
end
false
end

def revenue_to_render
@revenue_to_render ||= @stops.map(&:revenue_to_render)
end
Expand Down