view src/net/kryshen/charamega/card.mirah @ 0:91ecd24948de

Imported.
author Mikhail Kryshen <mikhail@kryshen.net>
date Sat, 14 Jul 2012 05:46:29 +0400
parents
children
line wrap: on
line source

#
#  Copyright 2012 Mikhail Kryshen <mikhail@kryshen.net>
#
#  This file is part of Charamega.
#
#  Charamega is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  Charamega is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with Charamega.  If not, see <http://www.gnu.org/licenses/>.
#

package net.kryshen.charamega

class Card

  def initialize(symbol:char, time:long)
    @symbol = symbol
    @angle = Math.random * Math.PI - Math.PI / 2

    @flip_interval = long(2E8)
    @remove_interval = long(2E8)

    @close_time = time - @flip_interval
    @match_time = @open_time = @close_time - 1
  end

  def symbol
    @symbol
  end

  def angle
    @angle
  end

  def open(time:long):void
    @open_time = time  unless opened?
  end

  def close(time:long):void
    @close_time = time  if opened?
  end

  def match(time:long):void
    @match_time = time  unless matched?
  end

  def opened?
    @close_time - @open_time <= 0
  end

  def matched?
    @close_time - @match_time <= 0
  end

  # -1.0 - closed, 1.0 - opened.
  def flip_state(time:long)
    if opened?
      s = 1.0
    else
      s = -1.0
    end

    t = Math.max(1, time - Math.max(@close_time, @open_time))
    
    return s  if t >= @flip_interval

    s * (t * 2 - @flip_interval) / double(@flip_interval)
  end   
  
  # 1.0 - visible, 0.0 - removed.
  def visible_state(time:long)
    return 1.0  if !matched?

    t = Math.max(1, time - @match_time - @flip_interval / 2)
    Math.max(0.0, 1.0 - t / double(@remove_interval))
  end

  def auto_close?(time:long)
    opened? and time - @open_time > 2E9
  end

  def toString
    String.valueOf(@symbol) + 
      if matched?
        '[matched]'
      elsif opened?
        '[opened]'
      else
        ''
      end     
  end
end