-
-
Save ledsun/4970631 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 -*- | |
module Movable | |
def position(position=nil) | |
return {:x=>@x, :y=>@y} unless position | |
@x = position[:x] | |
@y = position[:y] | |
end | |
def set_position_or_player(position) | |
position (position.is_a?(Movable) ? position.position : position) | |
end | |
def set_ratio(x, y) | |
@x_ratio = x | |
@y_ratio = y | |
end | |
def move(direction) | |
case direction | |
when :up | |
@y -= 10 * @y_ratio | |
when :down | |
@y += 10 * @y_ratio | |
when :left | |
@x -= 10 * @x_ratio | |
when :right | |
@x += 10 * @x_ratio | |
end | |
end | |
end | |
class NormalPlayer | |
include Movable | |
def initialize(position) | |
set_position_or_player position | |
set_ratio 1, 1 | |
end | |
end | |
class FastPlayer < NormalPlayer | |
include Movable | |
def initialize(position) | |
super | |
set_ratio 2, 2 | |
end | |
end | |
class KaniPlayer < NormalPlayer | |
include Movable | |
def initialize(position) | |
super | |
set_ratio 4, 0.5 | |
end | |
end | |
class NechigaePlayer < NormalPlayer | |
include Movable | |
def position(position=nil) | |
return {:x=>@y, :y=>@x} unless position | |
@x = position[:y] | |
@y = position[:x] | |
end | |
end | |
player = NormalPlayer.new :x => 100, :y => 100 | |
player.move :up | |
p player.position # => {:x => 100, :y => 90} | |
player = FastPlayer.new player | |
player.move :down | |
p player.position #=> {:x => 100, :y => 110} | |
player = KaniPlayer.new player | |
player.move :up | |
player.move :left | |
p player.position #=> {:x => 60, :y => 105} | |
player = NechigaePlayer.new player | |
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