mirror of
https://github.com/cyberarm/i-mic-fps.git
synced 2025-12-16 16:12:35 +00:00
Replaced usage of with CommonMethods.window (which uses ), refactored InputMapper to match actions to keys and visa-versa, removed categories from InputMapper, moved input mappings from Camera and Player into InputMapper (for now.)
This commit is contained in:
@@ -1,186 +0,0 @@
|
||||
class IMICFPS
|
||||
class Camera
|
||||
include CommonMethods
|
||||
include OpenGL
|
||||
include GLU
|
||||
|
||||
attr_accessor :field_of_view, :pitch, :yaw, :roll, :mouse_sensitivity
|
||||
attr_reader :entity, :position
|
||||
def initialize(x: 0, y: 0, z: 0, fov: 70.0, view_distance: 100.0)
|
||||
@position = Vector.new(x,y,z)
|
||||
@pitch = 0.0
|
||||
@yaw = 0.0
|
||||
@roll = 0.0
|
||||
@field_of_view = fov
|
||||
@view_distance = view_distance
|
||||
@constant_pitch = 20.0
|
||||
|
||||
@entity = nil
|
||||
@distance = 4
|
||||
@origin_distance = @distance
|
||||
|
||||
self.mouse_x, self.mouse_y = $window.width/2, $window.height/2
|
||||
@true_mouse = Point.new($window.width/2, $window.height/2)
|
||||
@mouse_sensitivity = 20.0 # Less is faster, more is slower
|
||||
@mouse_captured = true
|
||||
@mouse_checked = 0
|
||||
|
||||
InputMapper.set(:camera, :ascend, Gosu::KbSpace)
|
||||
InputMapper.set(:camera, :descend, [Gosu::KbLeftControl, Gosu::KbRightControl])
|
||||
InputMapper.set(:camera, :release_mouse, [Gosu::KbLeftAlt, Gosu::KbRightAlt])
|
||||
InputMapper.set(:camera, :capture_mouse, Gosu::MsLeft)
|
||||
InputMapper.set(:camera, :turn_180, Gosu::KbX)
|
||||
InputMapper.set(:camera, :increase_mouse_sensitivity, Gosu::KB_NUMPAD_PLUS)
|
||||
InputMapper.set(:camera, :decrease_mouse_sensitivity, Gosu::KB_NUMPAD_MINUS)
|
||||
InputMapper.set(:camera, :reset_mouse_sensitivity, Gosu::KB_NUMPAD_MULTIPLY)
|
||||
InputMapper.set(:camera, :increase_view_distance, Gosu::MsWheelUp)
|
||||
InputMapper.set(:camera, :decrease_view_distance, Gosu::MsWheelDown)
|
||||
end
|
||||
|
||||
def attach_to(entity)
|
||||
raise "Not a game object!" unless entity.is_a?(Entity)
|
||||
@entity = entity
|
||||
end
|
||||
|
||||
def detach
|
||||
@entity = nil
|
||||
end
|
||||
|
||||
def distance_from_object
|
||||
@distance
|
||||
end
|
||||
|
||||
def horizontal_distance_from_object
|
||||
distance_from_object * Math.cos(@constant_pitch)
|
||||
end
|
||||
|
||||
def vertical_distance_from_object
|
||||
distance_from_object * Math.sin(@constant_pitch)
|
||||
end
|
||||
|
||||
def position_camera
|
||||
if defined?(@entity.first_person_view)
|
||||
if @entity.first_person_view
|
||||
@distance = 0
|
||||
else
|
||||
@distance = @origin_distance
|
||||
end
|
||||
end
|
||||
|
||||
x_offset = horizontal_distance_from_object * Math.sin(@entity.rotation.y.degrees_to_radians)
|
||||
z_offset = horizontal_distance_from_object * Math.cos(@entity.rotation.y.degrees_to_radians)
|
||||
# p @entity.x, @entity.z;exit
|
||||
@position.x = @entity.position.x - x_offset
|
||||
@position.y = @entity.position.y + 2
|
||||
@position.z = @entity.position.z - z_offset
|
||||
|
||||
# @yaw = 180 - @entity.y_rotation
|
||||
@entity.rotation.y = -@yaw + 180
|
||||
end
|
||||
|
||||
def draw
|
||||
#glMatrixMode(matrix) indicates that following [matrix] is going to get used
|
||||
glMatrixMode(GL_PROJECTION) # The projection matrix is responsible for adding perspective to our scene.
|
||||
glLoadIdentity # Resets current modelview matrix
|
||||
# Calculates aspect ratio of the window. Gets perspective view. 45 is degree viewing angle, (0.1, 100) are ranges how deep can we draw into the screen
|
||||
gluPerspective(@field_of_view, $window.width / $window.height, 0.1, @view_distance)
|
||||
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
|
||||
glRotatef(@pitch,1,0,0)
|
||||
glRotatef(@yaw,0,1,0)
|
||||
glTranslatef(-@position.x, -@position.y, -@position.z)
|
||||
glMatrixMode(GL_MODELVIEW) # The modelview matrix is where object information is stored.
|
||||
glLoadIdentity
|
||||
|
||||
# if $debug && @entity
|
||||
# glBegin(GL_LINES)
|
||||
# glColor3f(1,0,0)
|
||||
# glVertex3f(@x, @y, @z)
|
||||
# glVertex3f(@entity.x, @entity.y, @entity.z)
|
||||
# glEnd
|
||||
# end
|
||||
end
|
||||
|
||||
def update
|
||||
if @mouse_captured
|
||||
position_camera if @entity
|
||||
|
||||
delta = Float(@true_mouse.x-self.mouse_x)/(@mouse_sensitivity*@field_of_view)*70
|
||||
@yaw -= delta
|
||||
@yaw %= 360.0
|
||||
|
||||
@pitch -= Float(@true_mouse.y-self.mouse_y)/(@mouse_sensitivity*@field_of_view)*70
|
||||
@pitch = @pitch.clamp(-90.0, 90.0)
|
||||
|
||||
@entity.rotation.y += delta if @entity
|
||||
free_move unless @entity
|
||||
|
||||
self.mouse_x = $window.width/2 if self.mouse_x <= 1 || $window.mouse_x >= $window.width-1
|
||||
self.mouse_y = $window.height/2 if self.mouse_y <= 1 || $window.mouse_y >= $window.height-1
|
||||
@true_mouse.x, @true_mouse.y = self.mouse_x, self.mouse_y
|
||||
end
|
||||
end
|
||||
|
||||
def free_move
|
||||
relative_y_rotation = (@yaw + 180)
|
||||
relative_speed = 0.5
|
||||
|
||||
if InputMapper.down?(:character, :forward)
|
||||
@z+=Math.cos(relative_y_rotation * Math::PI / 180)*relative_speed
|
||||
@x-=Math.sin(relative_y_rotation * Math::PI / 180)*relative_speed
|
||||
end
|
||||
|
||||
if InputMapper.down?(:character, :backward)
|
||||
@z-=Math.cos(relative_y_rotation * Math::PI / 180)*relative_speed
|
||||
@x+=Math.sin(relative_y_rotation * Math::PI / 180)*relative_speed
|
||||
end
|
||||
|
||||
if InputMapper.down?(:character, :strife_left)
|
||||
@z+=Math.sin(relative_y_rotation * Math::PI / 180)*relative_speed
|
||||
@x+=Math.cos(relative_y_rotation * Math::PI / 180)*relative_speed
|
||||
end
|
||||
|
||||
if InputMapper.down?(:character, :strife_right)
|
||||
@z-=Math.sin(relative_y_rotation * Math::PI / 180)*relative_speed
|
||||
@x-=Math.cos(relative_y_rotation * Math::PI / 180)*relative_speed
|
||||
end
|
||||
|
||||
if InputMapper.down?(:camera, :ascend)
|
||||
@y+=relative_speed
|
||||
end
|
||||
if InputMapper.down?(:camera, :descend)
|
||||
@y-=relative_speed
|
||||
end
|
||||
end
|
||||
|
||||
def button_up(id)
|
||||
if InputMapper.is?(:camera, :release_mouse, id)
|
||||
@mouse_captured = false
|
||||
$window.needs_cursor = true
|
||||
elsif InputMapper.is?(:camera, :capture_mouse, id)
|
||||
@mouse_captured = true
|
||||
$window.needs_cursor = false
|
||||
elsif InputMapper.is?(:camera, :increase_mouse_sensitivity, id)
|
||||
@mouse_sensitivity+=1
|
||||
@mouse_sensitivity = @mouse_sensitivity.clamp(1.0, 100.0)
|
||||
elsif InputMapper.is?(:camera, :decrease_mouse_sensitivity, id)
|
||||
@mouse_sensitivity-=1
|
||||
@mouse_sensitivity = @mouse_sensitivity.clamp(1.0, 100.0)
|
||||
elsif InputMapper.is?(:camera, :reset_mouse_sensitivity, id)
|
||||
@mouse_sensitivity = 20.0
|
||||
elsif InputMapper.is?(:camera, :increase_view_distance, id)
|
||||
# @field_of_view += 1
|
||||
# @field_of_view = @field_of_view.clamp(1, 100)
|
||||
@view_distance += 1
|
||||
@view_distance = @view_distance.clamp(1, 1000)
|
||||
elsif InputMapper.is?(:camera, :decrease_view_distance, id)
|
||||
# @field_of_view -= 1
|
||||
# @field_of_view = @field_of_view.clamp(1, 100)
|
||||
@view_distance -= 1
|
||||
@view_distance = @view_distance.clamp(1, 1000)
|
||||
elsif InputMapper.is?(:camera, :turn_180, id)
|
||||
@rotation.y = @rotation.y+180
|
||||
@rotation.y %= 360
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -8,16 +8,6 @@ class IMICFPS
|
||||
|
||||
def setup
|
||||
bind_model("base", "biped")
|
||||
InputMapper.set(:character, :forward, [Gosu::KbUp, Gosu::KbW])
|
||||
InputMapper.set(:character, :backward, [Gosu::KbDown, Gosu::KbS])
|
||||
InputMapper.set(:character, :strife_left, Gosu::KbA)
|
||||
InputMapper.set(:character, :strife_right, Gosu::KbD)
|
||||
InputMapper.set(:character, :turn_left, Gosu::KbLeft)
|
||||
InputMapper.set(:character, :turn_right, Gosu::KbRight)
|
||||
InputMapper.set(:character, :jump, Gosu::KbSpace)
|
||||
InputMapper.set(:character, :sprint, [Gosu::KbLeftControl])
|
||||
|
||||
InputMapper.set(:character, :toggle_first_person_view, Gosu::KbF)
|
||||
|
||||
@speed = 2.5 # meter's per second
|
||||
@running_speed = 6.8 # meter's per second
|
||||
@@ -98,7 +88,7 @@ class IMICFPS
|
||||
|
||||
def update
|
||||
relative_speed = @speed
|
||||
if InputMapper.down?(:character, :sprint)
|
||||
if InputMapper.down?(:sprint)
|
||||
relative_speed = (@running_speed)*(delta_time)
|
||||
else
|
||||
relative_speed = @speed*(delta_time)
|
||||
@@ -106,27 +96,27 @@ class IMICFPS
|
||||
|
||||
relative_y_rotation = @rotation.y*-1
|
||||
|
||||
if InputMapper.down?(:character, :forward)
|
||||
if InputMapper.down?(:forward)
|
||||
@position.z+=Math.cos(relative_y_rotation * Math::PI / 180)*relative_speed
|
||||
@position.x-=Math.sin(relative_y_rotation * Math::PI / 180)*relative_speed
|
||||
end
|
||||
if InputMapper.down?(:character, :backward)
|
||||
if InputMapper.down?(:backward)
|
||||
@position.z-=Math.cos(relative_y_rotation * Math::PI / 180)*relative_speed
|
||||
@position.x+=Math.sin(relative_y_rotation * Math::PI / 180)*relative_speed
|
||||
end
|
||||
if InputMapper.down?(:character, :strife_left)
|
||||
if InputMapper.down?(:strife_left)
|
||||
@position.z+=Math.sin(relative_y_rotation * Math::PI / 180)*relative_speed
|
||||
@position.x+=Math.cos(relative_y_rotation * Math::PI / 180)*relative_speed
|
||||
end
|
||||
if InputMapper.down?(:character, :strife_right)
|
||||
if InputMapper.down?(:strife_right)
|
||||
@position.z-=Math.sin(relative_y_rotation * Math::PI / 180)*relative_speed
|
||||
@position.x-=Math.cos(relative_y_rotation * Math::PI / 180)*relative_speed
|
||||
end
|
||||
|
||||
if InputMapper.down?(:character, :turn_left)
|
||||
if InputMapper.down?(:turn_left)
|
||||
@rotation.y+=(relative_speed*1000)*delta_time
|
||||
end
|
||||
if InputMapper.down?(:character, :turn_right)
|
||||
if InputMapper.down?(:turn_right)
|
||||
@rotation.y-=(relative_speed*1000)*delta_time
|
||||
end
|
||||
|
||||
@@ -135,7 +125,7 @@ class IMICFPS
|
||||
@velocity.y-=(IMICFPS::GRAVITY*air_time)*delta_time
|
||||
end
|
||||
|
||||
if InputMapper.down?(:character, :jump) && !@jumping
|
||||
if InputMapper.down?(:jump) && !@jumping
|
||||
@jumping = true
|
||||
@_time_in_air = Gosu.milliseconds
|
||||
elsif !@jumping && @position.y > @floor
|
||||
@@ -149,7 +139,7 @@ class IMICFPS
|
||||
end
|
||||
end
|
||||
if @jumping && !@falling
|
||||
if InputMapper.down?(:character, :jump)
|
||||
if InputMapper.down?(:jump)
|
||||
@velocity.y = 1.5
|
||||
@falling = true
|
||||
end
|
||||
@@ -161,9 +151,13 @@ class IMICFPS
|
||||
end
|
||||
|
||||
def button_up(id)
|
||||
if InputMapper.is?(:character, :toggle_first_person_view, id)
|
||||
if InputMapper.is?(:toggle_first_person_view, id)
|
||||
@first_person_view = !@first_person_view
|
||||
@visible = !@first_person_view
|
||||
puts "First Person? #{@first_person_view}"
|
||||
elsif InputMapper.is?(:turn_180, id)
|
||||
@rotation.y = @rotation.y+180
|
||||
@rotation.y %= 360
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user