Made model/entity manifest a first class object

This commit is contained in:
2019-09-25 11:52:19 -05:00
parent f41078b14b
commit 954c6899be
6 changed files with 52 additions and 18 deletions

View File

@@ -9,7 +9,8 @@ class IMICFPS
attr_accessor :position, :orientation, :velocity
attr_reader :name, :debug_color, :bounding_box, :collision, :physics, :mass, :drag, :camera
def initialize(map_entity: nil, spawnpoint: nil, backface_culling: true, auto_manage: true)
def initialize(manifest:, map_entity: nil, spawnpoint: nil, backface_culling: true, auto_manage: true)
@manifest = manifest
@position = map_entity ? map_entity.position : spawnpoint.position
@orientation = map_entity ? map_entity.orientation : spawnpoint.orientation
@scale = map_entity ? map_entity.scale : 1.0
@@ -56,7 +57,7 @@ class IMICFPS
end
def bind_model(package, name)
model = ModelLoader.new(manifest_file: IMICFPS.assets_path + "/#{package}/#{name}/manifest.yaml", entity: @dummy_entity)
model = ModelLoader.new(manifest: @manifest, entity: @dummy_entity)
raise "model isn't a model!" unless model.is_a?(ModelLoader)
@bound_model = model

View File

@@ -8,13 +8,11 @@ class IMICFPS
attr_reader :model, :name, :debug_color
def initialize(manifest_file:, entity: nil)
@manifest = YAML.load(File.read(manifest_file))
# pp @manifest
@file_path = File.expand_path("./../model/", manifest_file) + "/#{@manifest["model"]}"
@name = @manifest["name"]
def initialize(manifest:, entity: nil)
@name = manifest.name
@model_file = model_file = manifest.file_path + "/model/#{manifest.model}"
@type = File.basename(@file_path).split(".").last.to_sym
@type = File.basename(@model_file).split(".").last.to_sym
@debug_color = Color.new(0.0, 1.0, 0.0)
@model = nil
@@ -23,7 +21,7 @@ class IMICFPS
unless load_model_from_cache
case @type
when :obj
@model = Wavefront::Model.new(file_path: @file_path, entity: entity)
@model = Wavefront::Model.new(file_path: @model_file, entity: entity)
else
raise "Unsupported model type, supported models are: #{@supported_models.join(', ')}"
end
@@ -38,9 +36,9 @@ class IMICFPS
def load_model_from_cache
found = false
if CACHE[@type].is_a?(Hash)
if CACHE[@type][@file_path]
@model = CACHE[@type][@file_path]#.dup # Don't know why, but adding .dup improves performance with Sponza (1 fps -> 20 fps)
puts "Used cached model for: #{@file_path.split('/').last}" if $debug.get(:stats)
if CACHE[@type][@model_file]
@model = CACHE[@type][@model_file]#.dup # Don't know why, but adding .dup improves performance with Sponza (1 fps -> 20 fps)
puts "Used cached model for: #{@model_file.split('/').last}" if $debug.get(:stats)
found = true
end
end
@@ -50,7 +48,7 @@ class IMICFPS
def cache_model
CACHE[@type] = {} unless CACHE[@type].is_a?(Hash)
CACHE[@type][@file_path] = @model
CACHE[@type][@model_file] = @model
end
end
end