Skip to content

Commit

Permalink
Merge remote-tracking branch 'tobymao/master' into tiles-test-page
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeljb committed May 14, 2023
2 parents a7fe6e4 + 00f8cd5 commit 24d0267
Show file tree
Hide file tree
Showing 31 changed files with 570 additions and 318 deletions.
6 changes: 6 additions & 0 deletions TILES.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ game config/code:
- **upgrade**
- **cost** - *required* - integer
- **terrain** - `mountain`/`water` - multiple terrain types separated by `|`
- **loc** - (currently only supported for `:pointy` layouts) corner to
render the upgrade in, `5.5` to be where edges `5` and `0` meet, `0.5` for
edges `0` and `1`, and so on
- **border**
- **edge** - *required* - integer - which edge to modify
- **type** - `mountain`/`water`/`impassable` - Border type. If not'
Expand All @@ -91,6 +94,9 @@ game config/code:
upgrade is placed on this hex
- **blocks_lay** - indicates this tile cannot be laid normally
but can only be laid by special ability, such as a private company's ability.
- **loc** - (currently only supported for `:pointy` layouts) corner to
render the upgrade in, `5.5` to be where edges `5` and `0` meet, `0.5` for
edges `0` and `1`, and so on
- **frame**
- **color** - *required* - the color of the frame
- **color2** - A second color to display on the frame
Expand Down
11 changes: 8 additions & 3 deletions assets/app/view/game/part/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module Part
class Base < Snabberb::Component
needs :region_use
needs :tile, default: nil
needs :loc, default: nil

UPPER_LEFT = [0, 1, 2].freeze
UPPER_RIGHT = [2, 3, 4].freeze
Expand Down Expand Up @@ -152,9 +153,13 @@ def increment_weight_for_regions(regions, weight = 1)
end

def render_location
@render_location ||= preferred_render_locations.min_by.with_index do |t, i|
[combined_cost(t[:region_weights_in] || t[:region_weights]), i]
end
@render_location ||=
begin
locations = @loc ? preferred_render_locations_by_loc : preferred_render_locations
locations.min_by.with_index do |t, i|
[combined_cost(t[:region_weights_in] || t[:region_weights]), i]
end
end
end

# use this method to set instance vars that can be used in the other
Expand Down
6 changes: 5 additions & 1 deletion assets/app/view/game/part/icons.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ def preferred_render_locations
end
end

def parts_for_loc
@icons
end

def load_from_tile
@icons = @tile.icons.reject(&:large)
@icons = @tile.icons.select { |i| !i.large && (i.loc == @loc) }
@num_cities = @tile.cities.size
end

Expand Down
63 changes: 63 additions & 0 deletions assets/app/view/game/part/small_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,33 @@ module View
module Game
module Part
module SmallItem
def preferred_render_locations_by_loc
parts = parts_for_loc

if layout == :pointy
case @loc.to_s
when '0.5'
parts.one? ? [PP_BOTTOM_LEFT_CORNER] : [PP_WIDE_BOTTOM_LEFT_CORNER]
when '1.5'
parts.one? ? [PP_UPPER_LEFT_CORNER] : [PP_WIDE_UPPER_LEFT_CORNER]
when '2.5'
parts.one? ? [PP_TOP_CORNER] : [PP_WIDE_TOP_CORNER]
when '3.5'
parts.one? ? [PP_UPPER_RIGHT_CORNER] : [PP_WIDE_UPPER_RIGHT_CORNER]
when '4.5'
parts.one? ? [PP_BOTTOM_RIGHT_CORNER] : [PP_WIDE_BOTTOM_RIGHT_CORNER]
when '5.5'
parts.one? ? [PP_BOTTOM_CORNER] : [PP_WIDE_BOTTOM_CORNER]
else
@loc = nil
preferred_render_locations
end
else
@loc = nil
preferred_render_locations
end
end

P_RIGHT_CORNER = {
region_weights: Base::RIGHT_CORNER,
x: 75,
Expand Down Expand Up @@ -60,6 +87,24 @@ module SmallItem
y: -75,
}.freeze

PP_TOP_LEFT_CORNER = {
region_weights: Base::UPPER_LEFT_CORNER,
x: -65,
y: -37.5,
}.freeze

PP_TOP_RIGHT_CORNER = {
region_weights: Base::UPPER_RIGHT_CORNER,
x: 65,
y: -37.5,
}.freeze

PP_BOTTOM_RIGHT_CORNER = {
region_weights: Base::BOTTOM_LEFT_CORNER,
x: 65,
y: 37.5,
}.freeze

PP_BOTTOM_LEFT_CORNER = {
region_weights: Base::BOTTOM_LEFT_CORNER,
x: -65,
Expand Down Expand Up @@ -132,6 +177,24 @@ module SmallItem
y: 16,
}.freeze

PP_WIDE_UPPER_RIGHT_CORNER = {
region_weights: Base::RIGHT_CORNER,
x: 52,
y: -25,
}.freeze

PP_WIDE_UPPER_LEFT_CORNER = {
region_weights: Base::LEFT_CORNER,
x: -52,
y: -25,
}.freeze

PP_WIDE_BOTTOM_RIGHT_CORNER = {
region_weights: [9, 10, 11, 16],
x: 52,
y: 25,
}.freeze

PP_WIDE_BOTTOM_LEFT_CORNER = {
region_weights: [13, 14, 15, 19],
x: -52,
Expand Down
46 changes: 36 additions & 10 deletions assets/app/view/game/part/upgrade.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
# frozen_string_literal: true

require 'view/game/part/base'
require 'view/game/part/small_item'

module View
module Game
module Part
class Upgrade < Base
include SmallItem

needs :cost
needs :terrains, default: []
needs :size, default: nil
Expand Down Expand Up @@ -39,6 +42,11 @@ class Upgrade < Base
x: -50,
y: -45,
}.freeze
PP_EDGE2 = {
region_weights: [0, 5, 6],
x: -35,
y: -55,
}.freeze

P_RIGHT_CORNER = {
region_weights: [11, 18],
Expand All @@ -57,15 +65,31 @@ class Upgrade < Base
TRIANGLE_PATH = '0,20 10,0 20,20'

def preferred_render_locations
[
P_CENTER,
P_TOP_RIGHT_CORNER,
P_EDGE2,
P_BOTTOM_LEFT_CORNER,
P_RIGHT_CORNER,
P_LEFT_CORNER,
P_BOTTOM_RIGHT_CORNER,
]
if layout == :flat
[
P_CENTER,
P_TOP_RIGHT_CORNER,
P_EDGE2,
P_BOTTOM_LEFT_CORNER,
P_RIGHT_CORNER,
P_LEFT_CORNER,
P_BOTTOM_RIGHT_CORNER,
]
else
[
P_CENTER,
PP_TOP_RIGHT_CORNER,
PP_EDGE2,
PP_BOTTOM_LEFT_CORNER,
PP_RIGHT_CORNER,
PP_LEFT_CORNER,
PP_BOTTOM_RIGHT_CORNER,
]
end
end

def parts_for_loc
@terrains
end

def render_part
Expand All @@ -91,7 +115,9 @@ def render_part

children = [cost] + terrain

h(:g, { attrs: { transform: "#{translate} #{rotation_for_layout}" } }, children)
h(:g, { attrs: { transform: rotation_for_layout } }, [
h(:g, { attrs: { transform: translate } }, children),
])
end

def mountain(delta_x: 0, delta_y: 0)
Expand Down
2 changes: 2 additions & 0 deletions assets/app/view/game/part/upgrades.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module Part
class Upgrades < Snabberb::Component
needs :tile
needs :region_use
needs :loc, default: nil

def render
@tile.upgrades.map do |upgrade|
Expand All @@ -19,6 +20,7 @@ def render
terrains: upgrade.terrains,
tile: @tile,
size: upgrade.size,
loc: @loc,
)
end
end
Expand Down
36 changes: 30 additions & 6 deletions assets/app/view/game/tile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,19 @@ def render_tile_part(part_class, **kwargs)
h(part_class, region_use: @region_use, tile: @tile, **kwargs)
end

def render_tile_parts_by_loc(part_class, parts: nil, **kwargs)
return [] if !parts || parts.empty?

loc_to_parts = Hash.new { |h, k| h[k] = [] }
parts.each do |part|
loc_to_parts[part.loc] << part
end

loc_to_parts.map do |loc, _parts|
render_tile_part(part_class, loc: loc, **kwargs)
end
end

# if false, then the revenue is rendered by Part::Cities or Part::Towns
def should_render_revenue?
revenue = @tile.revenue_to_render
Expand Down Expand Up @@ -53,6 +66,11 @@ def render
# modified before being passed on to the next one
@region_use = Hash.new(0)

# array of parts to render
# - `render_tile_part` is called in the order they impact the region
# usage
# - the order of this array determines the order the parts are added to
# the DOM; parts at the end of the array render on top of ealier parts
children = []

render_revenue = should_render_revenue?
Expand All @@ -66,25 +84,31 @@ def render
if @tile.location_name && @tile.cities.size > 1 && !@tile.hex.hide_location_name
rendered_loc_name = render_tile_part(Part::LocationName)
end
children << render_tile_part(Part::Revenue) if render_revenue
@tile.labels.each { |x| children << render_tile_part(Part::Label, label: x) }
revenue = render_tile_part(Part::Revenue) if render_revenue
@tile.labels.each { |l| children << render_tile_part(Part::Label, label: l) }

children << render_tile_part(Part::Upgrades) unless @tile.upgrades.empty?
render_tile_parts_by_loc(Part::Upgrades, parts: @tile.upgrades).each { |p| children << p }
children << render_tile_part(Part::Blocker)

if @tile.location_name && (@tile.cities.size <= 1) && !@tile.hex.hide_location_name
rendered_loc_name = render_tile_part(Part::LocationName)
end
@tile.reservations.each { |x| children << render_tile_part(Part::Reservation, reservation: x) }
@tile.reservations.each { |r| children << render_tile_part(Part::Reservation, reservation: r) }

large, normal = @tile.icons.partition(&:large)
children << render_tile_part(Part::Icons) unless normal.empty?
render_tile_parts_by_loc(Part::Icons, parts: normal).each { |i| children << i }
children << render_tile_part(Part::LargeIcons) unless large.empty?
children << render_tile_part(Part::FutureLabel) if @tile.future_label

children << render_tile_part(Part::Assignments) unless @tile.hex&.assignments&.empty?
# borders should always be the top layer

# these parts should always be on the top layer
children << revenue if revenue
children << borders if borders
children << render_tile_part(Part::Partitions) unless @tile.partitions.empty?

# location name and coordinates on top of other "top" layer since they
# can be hidden
children << rendered_loc_name if rendered_loc_name && setting_for(:show_location_names, @game)
children << render_coords if @show_coords

Expand Down
5 changes: 5 additions & 0 deletions lib/engine/ability/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,11 @@ Lay a tile and place a station token without connectivity
teleport destination.
- `cost`: Cost to use the teleport ability.
- `free_tile_lay`: If true, the tile is laid with 0 cost. Default false.
- `from_owner`: If true, this ability uses a token from the owning corporation's
charter; if false, an additional token is created. Default true.
- `extra_action`: If true, this ability may be used in addition to the turn's
normal token placement step. Default false.


## tile_discount

Expand Down
6 changes: 4 additions & 2 deletions lib/engine/ability/teleport.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@
module Engine
module Ability
class Teleport < Base
attr_reader :tiles, :cost, :free_tile_lay
attr_reader :tiles, :cost, :free_tile_lay, :from_owner, :extra_action
attr_accessor :hexes

def setup(hexes:, tiles:, cost: nil, free_tile_lay: false)
def setup(hexes:, tiles:, cost: nil, free_tile_lay: false, from_owner: true, extra_action: nil)
@hexes = hexes
@tiles = tiles
@cost = cost
@free_tile_lay = free_tile_lay
@when = %w[track] if @when.empty?
@passive = false
@from_owner = from_owner
@extra_action = extra_action || false
end
end
end
Expand Down
Loading

0 comments on commit 24d0267

Please sign in to comment.