mirror of
https://github.com/cyberarm/i-mic-fps.git
synced 2025-12-15 15:42:35 +00:00
Extracted Crosshair and Demo from Game
This commit is contained in:
@@ -89,6 +89,9 @@ require_relative "lib/map_loader"
|
|||||||
require_relative "lib/manifest"
|
require_relative "lib/manifest"
|
||||||
require_relative "lib/map"
|
require_relative "lib/map"
|
||||||
|
|
||||||
|
require_relative "lib/crosshair"
|
||||||
|
require_relative "lib/demo"
|
||||||
|
|
||||||
require_relative "lib/window"
|
require_relative "lib/window"
|
||||||
|
|
||||||
if ARGV.join.include?("--profile")
|
if ARGV.join.include?("--profile")
|
||||||
|
|||||||
16
lib/crosshair.rb
Normal file
16
lib/crosshair.rb
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
class IMICFPS
|
||||||
|
class Crosshair
|
||||||
|
include CommonMethods
|
||||||
|
|
||||||
|
def initialize(color: Gosu::Color.rgb(255,127,0), size: 10, thickness: 3)
|
||||||
|
@color = color
|
||||||
|
@size = size
|
||||||
|
@thickness = thickness
|
||||||
|
end
|
||||||
|
|
||||||
|
def draw
|
||||||
|
draw_rect(window.width/2-@size, (window.height/2-@size)-@thickness/2, @size*2, @thickness, @color, 0, :default)
|
||||||
|
draw_rect((window.width/2)-@thickness/2, window.height/2-(@size*2), @thickness, @size*2, @color, 0, :default)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
108
lib/demo.rb
Normal file
108
lib/demo.rb
Normal file
@@ -0,0 +1,108 @@
|
|||||||
|
class IMICFPS
|
||||||
|
class Demo
|
||||||
|
def initialize(camera:, player:, demo:, mode:)
|
||||||
|
@camera = camera
|
||||||
|
@player = player
|
||||||
|
@demo = demo
|
||||||
|
@mode = mode
|
||||||
|
|
||||||
|
@index= 0
|
||||||
|
@tick = 0
|
||||||
|
@changed = false
|
||||||
|
|
||||||
|
if ARGV.join.include?("--playdemo")
|
||||||
|
@data = File.exist?(demo) ? File.read("./demo.dat").lines : ""
|
||||||
|
|
||||||
|
elsif ARGV.join.include?("--savedemo")
|
||||||
|
@file = File.open(demo, "w")
|
||||||
|
|
||||||
|
@last_pitch = @camera.orientation.z
|
||||||
|
@last_yaw = @camera.orientation.y
|
||||||
|
|
||||||
|
at_exit { @file.close }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def button_down(id)
|
||||||
|
if recording?
|
||||||
|
unless @last_written_index == @index
|
||||||
|
@last_written_index = @index
|
||||||
|
@file.puts("tick #{@index}")
|
||||||
|
end
|
||||||
|
|
||||||
|
@file.puts("down #{InputMapper.action(id)}")
|
||||||
|
@changed = true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def button_up(id)
|
||||||
|
if recording?
|
||||||
|
unless @last_written_index == @index
|
||||||
|
@last_written_index = @index
|
||||||
|
@file.puts("tick #{@index}")
|
||||||
|
end
|
||||||
|
|
||||||
|
@file.puts("up #{InputMapper.action(id)}")
|
||||||
|
@changed = true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def update
|
||||||
|
play if playing?
|
||||||
|
record if recording?
|
||||||
|
|
||||||
|
@tick += 1
|
||||||
|
end
|
||||||
|
|
||||||
|
def playing?; @mode == :play; end
|
||||||
|
def recording?; !playing?; end
|
||||||
|
|
||||||
|
def play
|
||||||
|
if @data[@index]&.start_with?("tick")
|
||||||
|
if @tick == @data[@index].split(" ").last.to_i
|
||||||
|
@index+=1
|
||||||
|
|
||||||
|
until(@data[@index]&.start_with?("tick"))
|
||||||
|
break unless @data[@index]
|
||||||
|
|
||||||
|
data = @data[@index].split(" ")
|
||||||
|
if data.first == "up"
|
||||||
|
input = InputMapper.get(data.last.to_sym)
|
||||||
|
key = input.is_a?(Array) ? input.first : input
|
||||||
|
$window.current_state.button_up(key) if key
|
||||||
|
|
||||||
|
elsif data.first == "down"
|
||||||
|
input = InputMapper.get(data.last.to_sym)
|
||||||
|
key = input.is_a?(Array) ? input.first : input
|
||||||
|
$window.current_state.button_down(key) if key
|
||||||
|
|
||||||
|
elsif data.first == "mouse"
|
||||||
|
@camera.orientation.z = data[1].to_f
|
||||||
|
@player.orientation.y = (data[2].to_f * -1) - 180
|
||||||
|
else
|
||||||
|
# hmm
|
||||||
|
end
|
||||||
|
|
||||||
|
@index += 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def record
|
||||||
|
if @camera.orientation.z != @last_pitch || @camera.orientation.y != @last_yaw
|
||||||
|
unless @last_written_index == @index
|
||||||
|
@last_written_index = @index
|
||||||
|
@file.puts("tick #{@index}")
|
||||||
|
end
|
||||||
|
|
||||||
|
@file.puts("mouse #{@camera.orientation.z} #{@camera.orientation.y}")
|
||||||
|
@last_pitch = @camera.orientation.z
|
||||||
|
@last_yaw = @camera.orientation.y
|
||||||
|
end
|
||||||
|
|
||||||
|
@changed = false
|
||||||
|
@index += 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -10,35 +10,22 @@ class IMICFPS
|
|||||||
@camera = Camera.new(position: @player.position.clone)
|
@camera = Camera.new(position: @player.position.clone)
|
||||||
@camera.attach_to(@player)
|
@camera.attach_to(@player)
|
||||||
|
|
||||||
@crosshair_size = 10
|
@crosshair = Crosshair.new
|
||||||
@crosshair_thickness = 3
|
|
||||||
@crosshair_color = Gosu::Color.rgb(255,127,0)
|
|
||||||
|
|
||||||
@text = Text.new("Pending...", x: 10, y: 10, z: 1, size: 18, font: "DejaVu Sans", shadow_color: Gosu::Color::BLACK)
|
@text = Text.new("Pending...", x: 10, y: 10, z: 1, size: 18, font: "DejaVu Sans", shadow_color: Gosu::Color::BLACK)
|
||||||
|
|
||||||
if ARGV.join.include?("--playdemo")
|
if ARGV.join.include?("--playdemo")
|
||||||
@demo_data = File.exist?("./demo.dat") ? File.read("./demo.dat").lines : ""
|
@demo = Demo.new(camera: @camera, player: @player, demo: "./demo.dat", mode: :play) if File.exist?("./demo.dat")
|
||||||
@demo_index= 0
|
|
||||||
@demo_tick = 0
|
|
||||||
|
|
||||||
elsif ARGV.join.include?("--savedemo")
|
elsif ARGV.join.include?("--savedemo")
|
||||||
@demo_file = File.open("./demo.dat", "w")
|
@demo = Demo.new(camera: @camera, player: @player, demo: "./demo.dat", mode: :record)
|
||||||
@demo_index= 0
|
|
||||||
@demo_changed = false
|
|
||||||
|
|
||||||
@demo_last_pitch = @camera.orientation.z
|
|
||||||
@demo_last_yaw = @camera.orientation.y
|
|
||||||
|
|
||||||
at_exit { @demo_file.close }
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def draw
|
def draw
|
||||||
@map.render(@camera)
|
@map.render(@camera)
|
||||||
# Draw crosshair
|
|
||||||
draw_rect(window.width/2-@crosshair_size, (window.height/2-@crosshair_size)-@crosshair_thickness/2, @crosshair_size*2, @crosshair_thickness, @crosshair_color, 0, :default)
|
|
||||||
draw_rect((window.width/2)-@crosshair_thickness/2, window.height/2-(@crosshair_size*2), @crosshair_thickness, @crosshair_size*2, @crosshair_color, 0, :default)
|
|
||||||
|
|
||||||
|
@crosshair.draw
|
||||||
@text.draw
|
@text.draw
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -61,55 +48,7 @@ class IMICFPS
|
|||||||
@text.text = ""
|
@text.text = ""
|
||||||
end
|
end
|
||||||
|
|
||||||
if ARGV.join.include?("--playdemo")
|
@demo.update if @demo
|
||||||
if @demo_data[@demo_index]&.start_with?("tick")
|
|
||||||
if @demo_tick == @demo_data[@demo_index].split(" ").last.to_i
|
|
||||||
@demo_index+=1
|
|
||||||
|
|
||||||
until(@demo_data[@demo_index]&.start_with?("tick"))
|
|
||||||
break unless @demo_data[@demo_index]
|
|
||||||
|
|
||||||
data = @demo_data[@demo_index].split(" ")
|
|
||||||
if data.first == "up"
|
|
||||||
input = InputMapper.get(data.last.to_sym)
|
|
||||||
key = input.is_a?(Array) ? input.first : input
|
|
||||||
self.button_up(key)
|
|
||||||
|
|
||||||
elsif data.first == "down"
|
|
||||||
input = InputMapper.get(data.last.to_sym)
|
|
||||||
key = input.is_a?(Array) ? input.first : input
|
|
||||||
self.button_down(key)
|
|
||||||
|
|
||||||
elsif data.first == "mouse"
|
|
||||||
@camera.orientation.z = data[1].to_f
|
|
||||||
@player.orientation.y = (data[2].to_f * -1) - 180
|
|
||||||
else
|
|
||||||
# hmm
|
|
||||||
end
|
|
||||||
|
|
||||||
@demo_index += 1
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if ARGV.join.include?("--savedemo")
|
|
||||||
if @camera.orientation.z != @demo_last_pitch || @camera.orientation.y != @demo_last_yaw
|
|
||||||
unless @demo_last_written_index == @demo_index
|
|
||||||
@demo_last_written_index = @demo_index
|
|
||||||
@demo_file.puts("tick #{@demo_index}")
|
|
||||||
end
|
|
||||||
|
|
||||||
@demo_file.puts("mouse #{@camera.orientation.z} #{@camera.orientation.y}")
|
|
||||||
@demo_last_pitch = @camera.orientation.z
|
|
||||||
@demo_last_yaw = @camera.orientation.y
|
|
||||||
end
|
|
||||||
|
|
||||||
@demo_changed = false
|
|
||||||
@demo_index += 1
|
|
||||||
end
|
|
||||||
|
|
||||||
@demo_tick += 1 if @demo_tick
|
|
||||||
|
|
||||||
window.close if window.button_down?(Gosu::KbEscape)
|
window.close if window.button_down?(Gosu::KbEscape)
|
||||||
window.number_of_vertices = 0
|
window.number_of_vertices = 0
|
||||||
@@ -146,14 +85,8 @@ eos
|
|||||||
end
|
end
|
||||||
|
|
||||||
def button_down(id)
|
def button_down(id)
|
||||||
if ARGV.join.include?("--savedemo")
|
@demo.button_down(id) if @demo
|
||||||
unless @demo_last_written_index == @demo_index
|
|
||||||
@demo_last_written_index = @demo_index
|
|
||||||
@demo_file.puts("tick #{@demo_index}")
|
|
||||||
end
|
|
||||||
@demo_file.puts("down #{InputMapper.action(id)}")
|
|
||||||
@demo_changed = true
|
|
||||||
end
|
|
||||||
InputMapper.keydown(id)
|
InputMapper.keydown(id)
|
||||||
Publisher.instance.publish(:button_down, nil, id)
|
Publisher.instance.publish(:button_down, nil, id)
|
||||||
|
|
||||||
@@ -163,14 +96,8 @@ eos
|
|||||||
end
|
end
|
||||||
|
|
||||||
def button_up(id)
|
def button_up(id)
|
||||||
if ARGV.join.include?("--savedemo")
|
@demo.button_up(id) if @demo
|
||||||
unless @demo_last_written_index == @demo_index
|
|
||||||
@demo_last_written_index = @demo_index
|
|
||||||
@demo_file.puts("tick #{@demo_index}")
|
|
||||||
end
|
|
||||||
@demo_file.puts("up #{InputMapper.action(id)}")
|
|
||||||
@demo_changed = true
|
|
||||||
end
|
|
||||||
InputMapper.keyup(id)
|
InputMapper.keyup(id)
|
||||||
Publisher.instance.publish(:button_up, nil, id)
|
Publisher.instance.publish(:button_up, nil, id)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user