Last active
December 12, 2015 08:59
-
-
Save jewel12/4748088 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
class Player | |
module MovingMode | |
def self.create(velocity) | |
return lambda{ |direction, position| | |
case direction | |
when :up | |
position[:y] -= velocity[:y] | |
when :down | |
position[:y] += velocity[:y] | |
when :left | |
position[:x] -= velocity[:x] | |
when :right | |
position[:x] += velocity[:x] | |
end | |
return position | |
} | |
end | |
end | |
attr_reader :position | |
def initialize(position) | |
@position = position | |
to_normal_mode | |
end | |
def to_normal_mode | |
@moving_mode = MovingMode.create({x:10, y:10}) | |
end | |
def to_fast_mode | |
@moving_mode = MovingMode.create({x:20, y:20}) | |
end | |
def to_kani_mode | |
@moving_mode = MovingMode.create({x:40, y:5}) | |
end | |
def to_nechigae_mode | |
@moving_mode = MovingMode.create({x:-10, y:-10}) | |
end | |
def move(direction) | |
@position = @moving_mode.call(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