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 source
1 #
2 # Copyright 2012 Mikhail Kryshen <mikhail@kryshen.net>
3 #
4 # This file is part of Charamega.
5 #
6 # Charamega is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # Charamega is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with Charamega. If not, see <http://www.gnu.org/licenses/>.
18 #
20 package net.kryshen.charamega
22 class Card
24 def initialize(symbol:char, time:long)
25 @symbol = symbol
26 @angle = Math.random * Math.PI - Math.PI / 2
28 @flip_interval = long(2E8)
29 @remove_interval = long(2E8)
31 @close_time = time - @flip_interval
32 @match_time = @open_time = @close_time - 1
33 end
35 def symbol
36 @symbol
37 end
39 def angle
40 @angle
41 end
43 def open(time:long):void
44 @open_time = time unless opened?
45 end
47 def close(time:long):void
48 @close_time = time if opened?
49 end
51 def match(time:long):void
52 @match_time = time unless matched?
53 end
55 def opened?
56 @close_time - @open_time <= 0
57 end
59 def matched?
60 @close_time - @match_time <= 0
61 end
63 # -1.0 - closed, 1.0 - opened.
64 def flip_state(time:long)
65 if opened?
66 s = 1.0
67 else
68 s = -1.0
69 end
71 t = Math.max(1, time - Math.max(@close_time, @open_time))
73 return s if t >= @flip_interval
75 s * (t * 2 - @flip_interval) / double(@flip_interval)
76 end
78 # 1.0 - visible, 0.0 - removed.
79 def visible_state(time:long)
80 return 1.0 if !matched?
82 t = Math.max(1, time - @match_time - @flip_interval / 2)
83 Math.max(0.0, 1.0 - t / double(@remove_interval))
84 end
86 def auto_close?(time:long)
87 opened? and time - @open_time > 2E9
88 end
90 def toString
91 String.valueOf(@symbol) +
92 if matched?
93 '[matched]'
94 elsif opened?
95 '[opened]'
96 else
97 ''
98 end
99 end
100 end