Added building component, refactored components to have setup, draw, update and tick methods, added fencing for building construction area parimeter, misc other changes.

This commit is contained in:
2019-12-17 15:40:26 -06:00
parent ba40645e23
commit e5579f48f1
19 changed files with 404 additions and 24 deletions

BIN
assets/fencing/fencing.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 218 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 217 B

View File

@@ -0,0 +1,135 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
width="32"
height="32"
viewBox="0 0 8.4666665 8.4666669"
version="1.1"
id="svg5422"
inkscape:version="0.92.4 5da689c313, 2019-01-14"
sodipodi:docname="fencing.svg"
inkscape:export-filename="/home/cyberarm/Code/i-mic-rts/assets/fencing/fencing.png"
inkscape:export-xdpi="96"
inkscape:export-ydpi="96">
<defs
id="defs5416">
<filter
style="color-interpolation-filters:sRGB;"
inkscape:label="Greyscale"
id="filter10184">
<feColorMatrix
values="0.21 0.72 0.072 0 0 0.21 0.72 0.072 0 0 0.21 0.72 0.072 0 0 0 0 0 1 0 "
id="feColorMatrix10182" />
</filter>
<filter
style="color-interpolation-filters:sRGB;"
inkscape:label="Greyscale"
id="filter10188">
<feColorMatrix
values="0.21 0.72 0.072 0 0 0.21 0.72 0.072 0 0 0.21 0.72 0.072 0 0 0 0 0 1 0 "
id="feColorMatrix10186" />
</filter>
</defs>
<sodipodi:namedview
id="base"
pagecolor="#ffffff"
bordercolor="#666666"
borderopacity="1.0"
inkscape:pageopacity="0.0"
inkscape:pageshadow="2"
inkscape:zoom="2.8"
inkscape:cx="-112.23234"
inkscape:cy="39.312696"
inkscape:document-units="mm"
inkscape:current-layer="layer1"
showgrid="true"
units="px"
showguides="true"
inkscape:guide-bbox="true"
inkscape:snap-bbox="true"
inkscape:bbox-paths="true"
inkscape:bbox-nodes="true"
inkscape:snap-bbox-edge-midpoints="true"
inkscape:snap-bbox-midpoints="true"
inkscape:object-paths="true"
inkscape:snap-intersection-paths="true"
inkscape:snap-smooth-nodes="true"
inkscape:snap-midpoints="true"
inkscape:snap-object-midpoints="true"
inkscape:snap-center="true"
inkscape:window-width="1920"
inkscape:window-height="1014"
inkscape:window-x="0"
inkscape:window-y="0"
inkscape:window-maximized="1">
<inkscape:grid
type="xygrid"
id="grid5191" />
</sodipodi:namedview>
<metadata
id="metadata5419">
<rdf:RDF>
<cc:Work
rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
<dc:title></dc:title>
</cc:Work>
</rdf:RDF>
</metadata>
<g
inkscape:label="Layer 1"
inkscape:groupmode="layer"
id="layer1"
transform="translate(0,-288.53332)">
<rect
style="opacity:1;fill:#555753;fill-opacity:1;stroke:none;stroke-width:0.37416676;stroke-linecap:round;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
id="rect5195"
width="8.4666672"
height="0.52915943"
x="0"
y="294.61874"
ry="0.25276139" />
<rect
style="opacity:1;fill:#555753;fill-opacity:1;stroke:none;stroke-width:0.37416676;stroke-linecap:round;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
id="rect5195-6"
width="8.4666681"
height="0.52915943"
x="-7.3115029e-07"
y="295.41251"
ry="0.25276139" />
<rect
style="opacity:1;fill:#555753;fill-opacity:1;stroke:none;stroke-width:0.37416676;stroke-linecap:round;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
id="rect5195-7"
width="8.4666681"
height="0.52915943"
x="-7.3115029e-07"
y="296.20624"
ry="0.25276139" />
<rect
style="opacity:1;fill:#888a85;fill-opacity:1;stroke:none;stroke-width:0.26458332;stroke-linecap:round;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
id="rect5193"
width="0.26458332"
height="2.6458333"
x="0"
y="294.35416"
ry="0.13229166" />
<rect
style="opacity:1;fill:#888a85;fill-opacity:1;stroke:none;stroke-width:0.26458332;stroke-linecap:round;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
id="rect5193-3"
width="0.26458332"
height="2.6458333"
x="8.2020836"
y="294.35416"
ry="0.13229166" />
</g>
</svg>

After

Width:  |  Height:  |  Size: 4.5 KiB

View File

@@ -0,0 +1,112 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
width="32"
height="32"
viewBox="0 0 8.4666665 8.4666669"
version="1.1"
id="svg5422"
inkscape:version="0.92.4 5da689c313, 2019-01-14"
sodipodi:docname="fencing_edge.svg"
inkscape:export-filename="/home/cyberarm/Code/i-mic-rts/assets/fencing/fencing_edge.png"
inkscape:export-xdpi="96"
inkscape:export-ydpi="96">
<defs
id="defs5416">
<filter
style="color-interpolation-filters:sRGB;"
inkscape:label="Greyscale"
id="filter10184">
<feColorMatrix
values="0.21 0.72 0.072 0 0 0.21 0.72 0.072 0 0 0.21 0.72 0.072 0 0 0 0 0 1 0 "
id="feColorMatrix10182" />
</filter>
<filter
style="color-interpolation-filters:sRGB;"
inkscape:label="Greyscale"
id="filter10188">
<feColorMatrix
values="0.21 0.72 0.072 0 0 0.21 0.72 0.072 0 0 0.21 0.72 0.072 0 0 0 0 0 1 0 "
id="feColorMatrix10186" />
</filter>
</defs>
<sodipodi:namedview
id="base"
pagecolor="#ffffff"
bordercolor="#666666"
borderopacity="1.0"
inkscape:pageopacity="0.0"
inkscape:pageshadow="2"
inkscape:zoom="11.2"
inkscape:cx="19.736534"
inkscape:cy="19.686409"
inkscape:document-units="mm"
inkscape:current-layer="layer1"
showgrid="true"
units="px"
showguides="true"
inkscape:guide-bbox="true"
inkscape:snap-bbox="true"
inkscape:bbox-paths="true"
inkscape:bbox-nodes="true"
inkscape:snap-bbox-edge-midpoints="true"
inkscape:snap-bbox-midpoints="true"
inkscape:object-paths="true"
inkscape:snap-intersection-paths="true"
inkscape:snap-smooth-nodes="true"
inkscape:snap-midpoints="true"
inkscape:snap-object-midpoints="true"
inkscape:snap-center="true"
inkscape:window-width="1920"
inkscape:window-height="1014"
inkscape:window-x="0"
inkscape:window-y="0"
inkscape:window-maximized="1">
<inkscape:grid
type="xygrid"
id="grid5191" />
</sodipodi:namedview>
<metadata
id="metadata5419">
<rdf:RDF>
<cc:Work
rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
<dc:title></dc:title>
</cc:Work>
</rdf:RDF>
</metadata>
<g
inkscape:label="Layer 1"
inkscape:groupmode="layer"
id="layer1"
transform="translate(0,-288.53332)">
<rect
style="opacity:1;fill:#555753;fill-opacity:1;stroke:none;stroke-width:0.37416676;stroke-linecap:round;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
id="rect5195"
width="8.4666672"
height="0.52915943"
x="288.53333"
y="-8.4666662"
ry="0.25276139"
transform="rotate(90)" />
<rect
style="opacity:1;fill:#888a85;fill-opacity:1;stroke:none;stroke-width:0.37417749;stroke-linecap:round;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
id="rect5193-3"
width="0.52916718"
height="2.6458333"
x="7.9374995"
y="294.35416"
ry="0.13229166" />
</g>
</svg>

After

Width:  |  Height:  |  Size: 3.6 KiB

View File

@@ -15,6 +15,33 @@ class IMICRTS
@@components[name] = klass
end
end
attr_reader :parent
def initialize(parent:)
@parent = parent
setup
end
def data
@parent.data
end
def data=(key, value)
@parent.data[key] = value
end
def setup
end
def draw
end
def update
end
def tick(tick_id)
end
end
end

View File

@@ -1,9 +1,7 @@
class IMICRTS
class BuildQueue < Component
Item = Struct.new(:entity, :progress)
def initialize(parent:)
@parent = parent
def setup
@queue = []
end

View File

@@ -0,0 +1,95 @@
class IMICRTS
class Building < Component
def setup
data.construction_progress ||= 0
data.construction_goal ||= 100
@text = CyberarmEngine::Text.new("", y: @parent.position.y, z: Float::INFINITY, size: 12)
data.state = :construct # deconstruct, building, idle
end
def draw
case data.state
when :construct
draw_construction unless @text.text.empty?
@text.text = "Building: #{(construction_progress * 100.0).round}%"
@text.x = @parent.position.x - @text.width / 2
@text.draw
when :deconstruct
when :building
when :idle
else
raise "Unknown state!"
end
end
def update
case data.state
when :construct
@parent.color.alpha = 255 * construction_progress
data.state = :idle if construction_complete?
end
end
def construction_complete?
data.construction_progress >= data.construction_goal
end
# WARNING: returns a floating point number, not network safe!
def construction_progress
data.construction_progress.to_f / data.construction_goal
end
def construction_work(work)
raise TypeError, "Got a non integer value!" unless work.is_a?(Integer)
data.construction_progress += work
data.construction_progress = data.construction_goal if construction_complete?
end
def draw_construction
@fencing ||= Gosu::Image.new(IMICRTS::ASSETS_PATH + "/fencing/fencing.png")
@fencing_edge ||= Gosu::Image.new(IMICRTS::ASSETS_PATH + "/fencing/fencing_edge.png")
tiles = []
each_tile(@parent.position / @parent.director.map.tile_size) do |tile, data, x, y|
tiles << tile
end
tiles.each do |tile|
# X
if tiles.find { |t| t.grid_position.x < tile.grid_position.x && t.grid_position.y == tile.grid_position.y} == nil
@fencing_edge.draw(tile.position.x - 30, tile.position.y, Float::INFINITY)
end
if tiles.find { |t| t.grid_position.x > tile.grid_position.x && t.grid_position.y == tile.grid_position.y} == nil
@fencing_edge.draw(tile.position.x, tile.position.y, Float::INFINITY)
end
# Y
if tiles.find { |t| t.grid_position.x == tile.grid_position.x && t.grid_position.y < tile.grid_position.y} == nil
@fencing.draw(tile.position.x, tile.position.y - 24, Float::INFINITY)
end
if tiles.find { |t| t.grid_position.x == tile.grid_position.x && t.grid_position.y > tile.grid_position.y} == nil
@fencing.draw(tile.position.x, tile.position.y, Float::INFINITY)
end
end
end
def each_tile(vector, &block)
if tile = @parent.director.map.tile_at(vector.x, vector.y)
ent = Entity.get(@parent.name)
origin = (tile.grid_position - 2)
ent.tiles.each_with_index do |array, y|
array.each_with_index do |space_required, x|
next unless space_required.is_a?(TrueClass)
other_tile = @parent.director.map.tile_at(origin.x + x, origin.y + y)
if other_tile
block.call(other_tile, space_required, origin.x + x, origin.y + y)
end
end
end
end
end
end
end

View File

@@ -1,8 +1,13 @@
class IMICRTS
class Movement < Component
attr_accessor :pathfinder
def initialize(parent:)
@parent = parent
def update
if pathfinder && pathfinder.path_current_node
rotate_towards(pathfinder.path_current_node.tile.position + @parent.director.map.tile_size / 2)
end
follow_path
end
def rotate_towards(vector)

View File

@@ -3,9 +3,7 @@ class IMICRTS
Action = Struct.new(:label, :image, :description, :block)
attr_reader :actions
def initialize(parent:)
@parent = parent
def setup
@actions = []
end

View File

@@ -1,9 +1,7 @@
class IMICRTS
class Turret < Component
attr_accessor :angle, :center
def initialize(parent:)
@parent = parent
def setup
@angle = 0
@center = CyberarmEngine::Vector.new(0.5, 0.5)
end

View File

@@ -7,6 +7,7 @@ tiles = [
]
IMICRTS::Entity.define_entity(:barracks, :building, 400, "Builds and soldiers", tiles) do |entity|
entity.has(:building)
entity.has(:build_queue)
entity.radius = 44

View File

@@ -7,6 +7,7 @@ tiles = [
]
IMICRTS::Entity.define_entity(:construction_yard, :building, 2_000, "Provides radar and builds construction workers", tiles) do |entity|
entity.has(:building)
entity.has(:build_queue)
entity.has(:sidebar_actions)
entity.component(:sidebar_actions).add(:add_to_build_queue, {entity: :construction_worker})

View File

@@ -7,6 +7,7 @@ tiles = [
]
IMICRTS::Entity.define_entity(:helipad, :building, 1_000, "Builds and rearms helicopters", tiles) do |entity|
entity.has(:building)
entity.has(:build_queue)
entity.has(:sidebar_actions)
entity.component(:sidebar_actions).add(:add_to_build_queue, {entity: :helicopter})

View File

@@ -7,6 +7,8 @@ tiles = [
]
IMICRTS::Entity.define_entity(:power_plant, :building, 800, "Generates power", tiles) do |entity|
entity.has(:building)
entity.radius = 24
entity.max_health = 100.0

View File

@@ -7,6 +7,8 @@ tiles = [
]
IMICRTS::Entity.define_entity(:refinery, :building, 1_400, "Generates credits", tiles) do |entity|
entity.has(:building)
entity.radius = 44
entity.max_health = 100.0

View File

@@ -7,6 +7,7 @@ tiles = [
]
IMICRTS::Entity.define_entity(:war_factory, :building, 2_000, "Builds units", tiles) do |entity|
entity.has(:building)
entity.has(:build_queue)
entity.has(:sidebar_actions)
entity.component(:sidebar_actions).add(:add_to_build_queue, {entity: :jeep})

View File

@@ -15,16 +15,20 @@ class IMICRTS
end
end
attr_reader :director, :player, :id, :name, :type, :speed
attr_reader :director, :player, :id, :name, :type, :speed, :data
attr_accessor :position, :angle, :radius, :target, :state,
:movement, :health, :max_health,
:turret, :center, :particle_emitters, :color
def initialize(name:, player:, id:, position:, angle:, director:)
def initialize(name:, player:, id:, position:, angle:, director:, proto_entity: false)
@player = player
@id = id
@position = position
@angle = angle
@director = director
@proto_entity = proto_entity
@data = FriendlyHash.new
@speed = 0.5
@color = Gosu::Color.rgba(255, 255, 255, 255)
@@ -125,18 +129,14 @@ class IMICRTS
render unless @render
@render.draw_rot(@position.x, @position.y, @position.z, @angle, @center.x, @center.y, 1, 1, @color)
component(:turret).draw if component(:turret)
@particle_emitters.each(&:draw)
unless @proto_entity
@components.values.each(&:draw)
@particle_emitters.each(&:draw)
end
end
def update
if component(:movement)
if component(:movement).pathfinder && component(:movement).pathfinder.path_current_node
component(:movement).rotate_towards(component(:movement).pathfinder.path_current_node.tile.position + @director.map.tile_size / 2)
end
component(:movement).follow_path
end
@components.values.each(&:update)
@particle_emitters.each do |emitter|
@particle_emitters.delete(emitter) if emitter.die?
@@ -146,6 +146,7 @@ class IMICRTS
def tick(tick_id)
@on_tick.call if @on_tick
component(:building).construction_work(1) if component(:building)
end
def on_tick(&block)

View File

@@ -12,10 +12,13 @@ class IMICRTS
@hash[key.to_sym] = value
end
def method_missing(method)
def method_missing(method, argument = nil)
if value = @hash.dig(method)
value
elsif argument != nil
@hash[method.to_s.sub("=", "").to_sym] = argument
else
return false unless argument # May result in bugginess!
raise "Unknown value for: #{method}"
end
end

View File

@@ -6,7 +6,7 @@ class IMICRTS
@entity = @options[:entity]
@construction_worker = @options[:construction_worker]
@preview = Entity.new(name: @entity, player: @player, id: 0, position: CyberarmEngine::Vector.new, angle: 0, director: @director)
@preview = Entity.new(name: @entity, player: @player, id: 0, position: CyberarmEngine::Vector.new, angle: 0, director: @director, proto_entity: true)
end
def draw