diff --git a/lib/objects/game_object.rb b/lib/objects/game_object.rb index b8c6c54..308d164 100644 --- a/lib/objects/game_object.rb +++ b/lib/objects/game_object.rb @@ -8,8 +8,8 @@ class IMICFPS attr_accessor :x, :y, :z, :scale attr_accessor :visible, :renderable, :backface_culling attr_accessor :x_rotation, :y_rotation, :z_rotation - attr_reader :model, :name, :debug_color - def initialize(x: 0, y: 0, z: 0, bound_model: nil, scale: MODEL_METER_SCALE, backface_culling: true, auto_manage: true) + attr_reader :model, :name, :debug_color, :terrain, :width, :height, :depth + def initialize(x: 0, y: 0, z: 0, bound_model: nil, scale: MODEL_METER_SCALE, backface_culling: true, auto_manage: true, terrain: nil) @x,@y,@z,@scale = x,y,z,scale @bound_model = bound_model @backface_culling = backface_culling @@ -17,16 +17,29 @@ class IMICFPS @renderable = true @x_rotation,@y_rotation,@z_rotation = 0,0,0 @debug_color = Color.new(0.0, 1.0, 0.0) + @terrain = terrain + @width, @height, @depth = 0,0,0 ObjectManager.add_object(self) if auto_manage setup + if @bound_model + box = normalize_bounding_box(@bound_model.model.bounding_box) + @width = box.max_x-box.min_x + @height = box.max_y-box.min_y + @depth = box.max_z-box.min_z + end + return self end def bind_model(model) raise "model isn't a model!" unless model.is_a?(ModelLoader) @bound_model = model + box = normalize_bounding_box(@bound_model.model.bounding_box) + @width = box.max_x-box.min_x + @height = box.max_y-box.min_y + @depth = box.max_z-box.min_z end def model diff --git a/lib/objects/player.rb b/lib/objects/player.rb index b39b818..1a729a9 100644 --- a/lib/objects/player.rb +++ b/lib/objects/player.rb @@ -5,10 +5,7 @@ class IMICFPS attr_accessor :speed attr_reader :name, :bound_model, :first_person_view - def initialize(x: 0, y: 0, z: 0, bound_model: nil, scale: MODEL_METER_SCALE, backface_culling: true, auto_manage: true, terrain: nil) - @terrain = terrain - super(x: x, y: y, z: z, bound_model: model, scale: scale, backface_culling: backface_culling, auto_manage: auto_manage) - end + def setup bind_model(ModelLoader.new(type: :obj, file_path: "objects/biped.obj", game_object: self)) @speed = 2.5 # meter's per second diff --git a/lib/objects/terrain.rb b/lib/objects/terrain.rb index f85495a..71bc535 100644 --- a/lib/objects/terrain.rb +++ b/lib/objects/terrain.rb @@ -29,7 +29,7 @@ class IMICFPS end def find_nearest_vertex(vertex) - _canidate_for_floor = nil + nearest = nil smaller_list = [] smaller_list << @nearest_vertex_lookup.dig(vertex.x.round-1, vertex.y.round-1) smaller_list << @nearest_vertex_lookup.dig(vertex.x.round, vertex.y.round) @@ -38,16 +38,16 @@ class IMICFPS smaller_list.each do |vert| next if vert.nil? - if _canidate_for_floor - if Gosu.distance(vertex.x, vertex.z, vert.x, vert.z) < Gosu.distance(_canidate_for_floor.x, _canidate_for_floor.z, vert.x, vert.z) - _canidate_for_floor = vert + if nearest + if Gosu.distance(vertex.x, vertex.z, vert.x, vert.z) < Gosu.distance(nearest.x, nearest.z, vert.x, vert.z) + nearest = vert end end - _canidate_for_floor = vert unless _canidate_for_floor + nearest = vert unless nearest end - return _canidate_for_floor + return nearest end end end diff --git a/lib/objects/tree.rb b/lib/objects/tree.rb index a3aba91..21be33f 100644 --- a/lib/objects/tree.rb +++ b/lib/objects/tree.rb @@ -2,6 +2,7 @@ class IMICFPS class Tree < GameObject def setup bind_model(ModelLoader.new(type: :obj, file_path: "objects/tree.obj", game_object: self)) + self.y = @terrain.height_at(self) end # def update diff --git a/lib/window.rb b/lib/window.rb index ad0ffb6..2773548 100644 --- a/lib/window.rb +++ b/lib/window.rb @@ -17,20 +17,25 @@ class IMICFPS end $window = self @needs_cursor = false + @number_of_faces = 0 @delta_time = Gosu.milliseconds - @number_of_faces = 0 + + @terrain = Terrain.new#(size: 170, height: 0) @draw_skydome = true @skydome = Skydome.new(scale: 0.08, backface_culling: false, auto_manage: false) - Tree.new(x: 1, y: 0, z: -5) - Tree.new(x: 5, y: 0, z: 5) - Tree.new(x: -5, y: 0, z: 1) + + 25.times do + p @terrain.width + Tree.new(x: rand(@terrain.width)-(@terrain.width/2.0), z: rand(@terrain.depth)-(@terrain.depth/2.0), terrain: @terrain) + end + # Tree.new(x: 1, z: -5, terrain: @terrain) + # Tree.new(x: 5, z: 5, terrain: @terrain) # TestObject.new(scale: 1) p ObjectManager.objects.map {|o| o.name} # Model.new(type: :obj, file_path: "objects/tree.obj", z: -5) # Model.new(type: :obj, file_path: "objects/tree.obj", x: -2, z: -6) # Model.new(type: :obj, file_path: "objects/sponza.obj", scale: 1, y: -0.2) - @terrain = Terrain.new#(size: 170, height: 0) @player = Player.new(x: 1, y: 0, z: -1, terrain: @terrain) @camera = Camera.new(x: 0, y: -2, z: 1)