Skip to content

Instantly share code, notes, and snippets.

@Shinpeim
Last active December 12, 2015 08:39
Show Gist options
  • Save Shinpeim/4745447 to your computer and use it in GitHub Desktop.
Save Shinpeim/4745447 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
class Player
module MovingMode
class Normal
def move(direction, position)
case direction
when :up
position[:y] -= 10
when :down
position[:y] += 10
when :left
position[:x] -= 10
when :right
position[:x] += 10
end
return position
end
end
class Fast
def move(direction, position)
case direction
when :up
position[:y] -= 20
when :down
position[:y] += 20
when :left
position[:x] -= 20
when :right
position[:x] += 20
end
return position
end
end
class Kani
def move(direction, position)
case direction
when :up
position[:y] -= 5
when :down
position[:y] += 5
when :left
position[:x] -= 40
when :right
position[:x] += 40
end
return position
end
end
# 寝違えモードの振る舞いを追加
class Nechigae
def move(direction, position)
case direction
when :up
position[:x] -= 10
when :down
position[:x] += 10
when :left
position[:y] += 10
when :right
position[:y] -= 10
end
return position
end
end
end
attr_reader :position
def initialize(position)
@position = position
@moving_mode = MovingMode::Normal.new
end
def to_fast_mode
@moving_mode = MovingMode::Fast.new
end
def to_kani_mode
@moving_mode = MovingMode::Kani.new
end
# 寝違えモードを追加
def to_nechigae_mode
@moving_mode = MovingMode::Nechigae.new
end
def move(direction)
@position = @moving_mode.move(direction, @position)
end
end
player = Player.new(:x => 100, :y => 100)
player.move(:up)
p player.position # => {:x => 100, :y => 90}
player.to_fast_mode #ここで倍速モードに
player.move(:down)
p player.position # => {:x => 100, :y => 110}
player.to_kani_mode
player.move(:up)
player.move(:left)
p player.position # => {:x => 60, :y => 105}
player.to_nechigae_mode
player.move(:up)
p player.position # => {:x => 50, :y => 105}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment