Skip to content

Instantly share code, notes, and snippets.

@basicxman
Created December 18, 2011 05:23
Show Gist options
  • Select an option

  • Save basicxman/1492456 to your computer and use it in GitHub Desktop.

Select an option

Save basicxman/1492456 to your computer and use it in GitHub Desktop.
require "gosu"
class Character
def initialize(window)
@window = window
@sprite = Gosu::Image.new(@window, "sprite.gif", false)
@x, @y = 100, @window.floor
@counter = 0
@angle = 0
@speed = 1
@rolling_duration = 25
@movement = :halt
@action = :walking
@modes = {
:halt => :movement,
:left => :movement,
:right => :movement,
:walking => :action,
:rolling => :action
}
end
def update
self.send(@action)
@counter += 1
end
def draw
@sprite.draw_rot(@x, @y, 1, @angle)
end
def set_mode(mode)
cur_name = "@#{@modes[mode]}"
cur = instance_variable_get(cur_name)
engage_name = "engage_#{mode}".to_sym
send(engage_name) if cur != mode and respond_to? engage_name
instance_variable_set(cur_name, mode)
end
def walking
return if @rolling_lock
move(@movement)
end
def engage_rolling
@rolling_lock = true
@rolling_direction = @movement
@rolling_counter = 0
@speed = 1.5
end
def rolling
if @rolling_counter >= @rolling_duration and @angle < 10
@speed = 1
@angle = 0
@rolling_lock = false
set_mode(:walking)
return
end
roll_speed = 5 * Math.sin(@rolling_counter * 0.01) + 6
@angle += @rolling_direction == :right ? roll_speed : roll_speed * -1
@angle %= 360
@rolling_counter += 1
move(@rolling_direction)
end
def move(direction)
if direction == :left
@x -= @speed
elsif direction == :right
@x += @speed
end
end
end
class Game < Gosu::Window
attr_accessor :floor
def initialize
super 1024, 800, false
self.caption = "Character Testing"
@floor = 500
@character = Character.new(self)
@roll_key_prev_state = false
end
def update
@character.set_mode(:left) if button_down? Gosu::KbLeft
@character.set_mode(:right) if button_down? Gosu::KbRight
@character.set_mode(:halt) unless button_down? Gosu::KbLeft or button_down? Gosu::KbRight
temp = button_down? Gosu::KbRightShift
@character.set_mode(:rolling) if @roll_key_prev_state and !temp
@roll_key_prev_state = temp
@character.update
end
def draw
@character.draw
end
def button_down(id)
close if id == Gosu::KbEscape or id == Gosu::KbQ
end
end
window = Game.new
window.show
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment