Skip to content

Instantly share code, notes, and snippets.

@Shinpeim
Last active December 12, 2015 08:39
Show Gist options
  • Save Shinpeim/4745440 to your computer and use it in GitHub Desktop.
Save Shinpeim/4745440 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
class Player
attr_reader :position
# Cで言うenum的なやつが増えた
module MOVING_MODE
NORMAL = 1
FAST = 2
end
def initialize(position)
@position = position
@moving_mode = MOVING_MODE::NORMAL # 状態をインスタンス変数で管理
end
def to_fast_mode
@moving_mode = MOVING_MODE::FAST
end
def move(direction)
# 状態変数を見て動くスピードを変えよう!
case @moving_mode
when MOVING_MODE::NORMAL
speed = 10
when MOVING_MODE::FAST
speed = 20
end
case direction
when :up
@position[:y] -= speed
when :down
@position[:y] += speed
when :left
@position[:x] -= speed
when :right
@position[:x] += speed
end
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}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment