sottolio 0.1.4 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0a2bbfd7861b599ec96ba13cec50a24646223d20
4
- data.tar.gz: 03f6041c3a0b834e26be3f4da961a00857026479
3
+ metadata.gz: b73cfc781b858b0c57d0af83350cfe79a9cc3f5a
4
+ data.tar.gz: 6a73dbf6d587e0d75e828b1b5477a98980942d47
5
5
  SHA512:
6
- metadata.gz: 02573a9fbb94f4a0bf0206e1c5c492522ec93249388e50f694047437e6fd38082b785a418844e0fe4bc166d4607185715d7a643cace1f848e464ff5b45f5736b
7
- data.tar.gz: a8eb6f6aa726802af102c4775fbdeeff13d9e96d14e3d3efd6fde4de2aaaeac8d302915ebf480a12ba39fd01c5f164fce1159269048c5a46bba2828eab599203
6
+ metadata.gz: 4d067d963c9d6cce25a5acd680d6dc381bbc2b14ecc60b0af0d7d55a5ed5322b5d3f9fa37e6b730d144c5a7a773735f74f2e9eece22dd450599ca58c896eba23
7
+ data.tar.gz: 9cdcea0dcb220ff9b388649ea95bf62b26d302c43a919cecc30d92dffb24399c6577f42c0dea5dcbb19c0f6f39a0dce783cd8c3935768b28b710d548e6a69b7d
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- sottolio (0.1.4)
4
+ sottolio (0.2)
5
5
  opal (~> 0.8)
6
6
 
7
7
  GEM
@@ -1,64 +1,66 @@
1
1
  (@scripts ||= []) << Proc.new do
2
- background :id => 'city', # it's the placeholder for #remove
3
- :resource => 'resources/backgrounds/city.jpg'
2
+ background :id => 'city', # it's the placeholder for #remove
3
+ :resource => 'resources/backgrounds/city.jpg'
4
4
 
5
- play_sound :id => 'background_theme',
6
- :resource => 'resources/sounds/Classmate.m4a',
7
- :loop => true,
8
- :volume => 0.5 # 0.0 - 1.0
5
+ play_sound :id => 'background_theme',
6
+ :resource => 'resources/sounds/Classmate.m4a',
7
+ :loop => true,
8
+ :volume => 0.5 # 0.0 - 1.0
9
9
 
10
- character :id => 'Rosalinda',
11
- :resource => 'resources/characters/rosalinda.png',
12
- :x => 800,
13
- :y => 120
10
+ character :id => 'Rosalinda',
11
+ :resource => 'resources/characters/rosalinda.png',
12
+ :x => 800,
13
+ :y => 120
14
14
 
15
- character :id => 'Ambrogia',
16
- :resource => 'resources/characters/ambrogia.png',
17
- :x => 10,
18
- :y => 120
15
+ character :id => 'Ambrogia',
16
+ :resource => 'resources/characters/ambrogia.png',
17
+ :x => 10,
18
+ :y => 120
19
19
 
20
- dialogue :name => 'Ambrogia', # optional
21
- :text => 'Hi!'
20
+ dialogue :name => 'Ambrogia', # optional
21
+ :text => 'Hi!'
22
22
 
23
- input :name => 'Ambrogia',
24
- :text => 'My name is Ambrogia, and yours?',
25
- :id => 'name', # the variable where the input will be saved
26
- :request => 'Your name'
23
+ input :name => 'Ambrogia',
24
+ :text => 'My name is Ambrogia, and yours?',
25
+ :id => 'name', # the variable where the input will be saved
26
+ :placeholder => 'Your name:',
27
+ :constraint => '#name# =~ /^[a-zA-Z]{3,10}$/',
28
+ :on_fail => 'Your name has to be contained in 3-10 chars.' # printed if the input won't match the constraint
27
29
 
28
- dialogue :name => 'Ambrogia',
29
- :text => 'Oh, hai #name#!',
30
- :if => '#name# =~ /^[a-zA-Z]+$/'
30
+ dialogue :name => 'Ambrogia',
31
+ :text => 'Oh, hai #name#!',
32
+ :if => '#name# =~ /^[a-zA-Z]+$/'
31
33
 
32
- dialogue :name => 'Ambrogia',
33
- :text => 'Don\'t tease me >:(',
34
- :if_not => '#name# =~ /^[a-zA-Z]+$/'
34
+ dialogue :name => 'Ambrogia',
35
+ :text => 'Don\'t tease me >:(',
36
+ :unless => '#name# =~ /^[a-zA-Z]+$/'
35
37
 
36
- choice :name => 'Ambrogia',
37
- :text => 'How do you feel?',
38
- :id => 'feel',
39
- :options => [
40
- { id: 'good', text: 'Good' },
41
- { id: 'bad', text: 'Bad' }
42
- ]
38
+ choice :name => 'Ambrogia',
39
+ :text => 'How do you feel?',
40
+ :id => 'feel',
41
+ :options => [
42
+ { id: 'good', text: 'Good' },
43
+ { id: 'bad', text: 'Bad' }
44
+ ]
43
45
 
44
- dialogue :name => 'Ambrogia',
45
- :text => 'I\'m glad you feel good, my dear!',
46
- :if => [ '#feel# == good', '#name# == Patrizio' ]
46
+ dialogue :name => 'Ambrogia',
47
+ :text => 'I\'m glad you feel good, my dear!',
48
+ :if => [ '#feel# == good', '#name# == Patrizio' ]
47
49
 
48
- dialogue :name => 'Ambrogia',
49
- :text => 'I\'m glad you feel good!',
50
- :if => [ '#feel# == good', '#name# != Patrizio' ]
50
+ dialogue :name => 'Ambrogia',
51
+ :text => 'I\'m glad you feel good!',
52
+ :if => [ '#feel# == good', '#name# != Patrizio' ]
51
53
 
52
- dialogue :name => 'Ambrogia',
53
- :text => 'I\'m sorry you feel bad!',
54
- :if => '#feel# == bad'
54
+ dialogue :name => 'Ambrogia',
55
+ :text => 'I\'m sorry you feel bad!',
56
+ :if => '#feel# == bad'
55
57
 
56
- dialogue :name => 'Ambrogia',
57
- :text => 'Ok, time to go, cya!'
58
+ dialogue :name => 'Ambrogia',
59
+ :text => 'Ok, time to go, cya!'
58
60
 
59
- remove :id => 'Ambrogia'
61
+ remove :id => 'Ambrogia'
60
62
 
61
- remove :id => 'Rosalinda'
63
+ remove :id => 'Rosalinda'
62
64
 
63
- stop_sound :id => 'background_theme'
65
+ stop_sound :id => 'background_theme'
64
66
  end
@@ -4,7 +4,7 @@ background :id => 'city',
4
4
 
5
5
  character :id => 'Rosalinda',
6
6
  :resource => 'resources/characters/rosalinda.png',
7
- :x => 800,
7
+ :x => 500,
8
8
  :y => 120
9
9
 
10
10
  dialogue :name => 'Rosalinda',
@@ -17,5 +17,5 @@
17
17
  # along with sottolio. If not, see <http://www.gnu.org/licenses/>.
18
18
  #++
19
19
  module Sottolio
20
- VERSION = '0.1.4'
20
+ VERSION = '0.2'
21
21
  end
@@ -27,8 +27,9 @@ module Sottolio
27
27
  go_next = Sottolio.get 'next'
28
28
  canvas = Canvas.new canvas_id
29
29
 
30
- database = Database.new
31
- scripts = Script.new @scripts
30
+ database = Database.new
31
+ scripts = Script.new @scripts
32
+ Utils.database = database
32
33
 
33
34
  config = {
34
35
  canvas_id: 'game',
@@ -44,17 +45,26 @@ module Sottolio
44
45
  image_manager = ImageManager.new
45
46
  input = nil
46
47
 
48
+ previous_dialogue = nil
47
49
  next_dialogue = -> {
48
50
  if lock.free? && (!input || input.destroyed? || (input.present? && !input.empty?)) && scripts.any?
49
- input.save_and_destroy if input.is_a?(CanvasInput) && input.present?
51
+ if input.is_a?(CanvasInput) && input.present?
52
+ input.save_and_destroy
53
+
54
+ if !input.valid? && !previous_dialogue.nil?
55
+ previous_dialogue[:input][:failed] = true
56
+ scripts << previous_dialogue
57
+ end
58
+ end
50
59
 
51
60
  canvas.fill_style = '#fff'
52
61
 
53
- script = scripts.pop
54
- current_command = script.keys.first
62
+ script = scripts.pop
63
+ previous_dialogue = script
64
+ current_command = script.keys.first
55
65
  case current_command
56
66
  when :play_sound
57
- if script[:play_sound].true? database
67
+ if Utils.command_conditions_passes? script[:play_sound]
58
68
  sound = Sound.new script[:play_sound][:resource], script[:play_sound][:loop], script[:play_sound][:volume]
59
69
  sound_manager.add script[:play_sound][:id], sound
60
70
  sound_manager.play script[:play_sound][:id]
@@ -62,13 +72,13 @@ module Sottolio
62
72
 
63
73
  next_dialogue.call
64
74
  when :stop_sound
65
- if script[:stop_sound].true? database
75
+ if Utils.command_conditions_passes? script[:stop_sound]
66
76
  sound_manager.stop script[:stop_sound][:id]
67
77
  end
68
78
 
69
79
  next_dialogue.call
70
80
  when :background
71
- if script[:background].true? database
81
+ if Utils.command_conditions_passes? script[:background]
72
82
  background = Background.new canvas_id, script[:background][:resource], script[:background][:id]
73
83
  image_manager.add background
74
84
  image_manager.draw script[:background][:id]
@@ -76,7 +86,7 @@ module Sottolio
76
86
 
77
87
  next_dialogue.call
78
88
  when :character
79
- if script[:character].true? database
89
+ if Utils.command_conditions_passes? script[:character]
80
90
  character = Character.new canvas_id, script[:character][:resource], script[:character][:id]
81
91
  image_manager.add character
82
92
  image_manager.draw script[:character][:id], script[:character][:x], script[:character][:y]
@@ -88,7 +98,7 @@ module Sottolio
88
98
 
89
99
  next_dialogue.call
90
100
  when :dialogue
91
- if script[:dialogue].true? database
101
+ if Utils.command_conditions_passes? script[:dialogue]
92
102
  canvas.clear
93
103
 
94
104
  if script[:dialogue].include? :name
@@ -100,7 +110,7 @@ module Sottolio
100
110
  next_dialogue.call
101
111
  end
102
112
  when :input
103
- if script[:input].true? database
113
+ if Utils.command_conditions_passes? script[:input]
104
114
  canvas.clear
105
115
 
106
116
  if script[:input].include? :name
@@ -109,12 +119,14 @@ module Sottolio
109
119
  canvas_text.write script[:input][:text].apply(database)
110
120
  end
111
121
 
112
- input = CanvasInput.new canvas_id, database, script[:input][:id], script[:input][:request].apply(database)
122
+ placeholder = (script[:input][:failed] == true ? script[:input][:on_fail] || 'Please check your input' : script[:input][:placeholder]).apply(database)
123
+ input = CanvasInput.new canvas_id, database, script[:input][:id], placeholder, script[:input][:constraint]
124
+ input.focus! if script[:input][:failed] != true
113
125
  else
114
126
  next_dialogue.call
115
127
  end
116
128
  when :choice
117
- if script[:choice].true? database
129
+ if Utils.command_conditions_passes? script[:choice]
118
130
  canvas.clear
119
131
 
120
132
  if script[:choice].include? :name
@@ -29,8 +29,12 @@ module Sottolio
29
29
  @var.reverse!
30
30
  end
31
31
 
32
- def <<(block)
33
- instance_eval &block
32
+ def <<(obj)
33
+ if obj.is_a?(Proc)
34
+ instance_eval &obj
35
+ else
36
+ @var << obj
37
+ end
34
38
  end
35
39
 
36
40
  def_delegators :@var, :pop, :any?
@@ -35,23 +35,35 @@ class String
35
35
  end
36
36
  end
37
37
 
38
- class Hash
39
- # TODO: Find a better name for this (tl;dr eval conditional keys in the script's commands)
40
- def true?(database)
41
- return true unless self.include?(:if) || self.include?(:if_not)
42
-
43
- res = []
44
- sym = self.include?(:if) ? :if : :if_not
45
- [self[sym]].flatten.each { |c| # i.e.: [ '#feel# == good', '#name# == Patrizio' ]
46
- statement = c.apply(database).split(/(.+)(==|!=|=~)(.+)/).delete_if { |s| s.strip.empty? }.map(&:strip)
47
- eval = case statement[1] # #send won't work with !=
48
- when '==' then statement[0] == statement[2]
49
- when '!=' then statement[0] != statement[2]
50
- when '=~' then statement[0] =~ statement[2].to_regex
38
+ module Utils
39
+ class << self
40
+ def database=(database)
41
+ @@database = database
42
+ end
43
+
44
+ def command_conditions_passes?(command)
45
+ return true unless command.include?(:if) || command.include?(:unless)
46
+
47
+ sym = command.include?(:if) ? :if : :unless
48
+ res = acceptable_constraint? [command[sym]]
49
+ sym == :if ? res : !res
50
+ end
51
+
52
+ def acceptable_constraint?(constraint)
53
+ res = []
54
+
55
+ [constraint].flatten.each do |c| # i.e.: [ '#feel# == good', '#name# == Patrizio' ]
56
+ statement = c.apply(@@database).split(/(.+)(==|!=|=~)(.+)/).delete_if { |s| s.strip.empty? }.map(&:strip)
57
+ eval = case statement[1] # #send won't work with !=
58
+ when '==' then statement[0] == statement[2]
59
+ when '!=' then statement[0] != statement[2]
60
+ when '=~' then !!(statement[0] =~ statement[2].to_regex)
61
+ end
62
+
63
+ res << eval
51
64
  end
52
- res << eval # actually this is totally not safe
53
- }
54
- res << res.inject { |sum, x| sum && x }
55
- return sym == :if ? res.last : !res.last
65
+
66
+ res.inject { |sum, x| sum && x }
67
+ end
56
68
  end
57
69
  end
@@ -18,26 +18,25 @@
18
18
  #++
19
19
  module Sottolio
20
20
  class CanvasInput
21
- def initialize(id, database, key, text = '', x = 10, y = 800)
22
- @database = database
23
- @key = key
24
- @destroyed = false
21
+ def initialize(canvas_id, database, key, placeholder, constraint, x = 500, y = 800)
22
+ @database = database
23
+ @key = key
24
+ @constraint = constraint
25
+ @destroyed = false
25
26
 
26
27
  %x{
27
- #@canvas_input = new CanvasText(id, {
28
- x: x,
29
- y: y,
30
- placeHolder: text,
31
- width: 300,
32
- padding: 8
28
+ #@canvas_input = new CanvasText(canvas_id, {
29
+ x: x,
30
+ y: y,
31
+ placeholder: placeholder,
32
+ width: 300,
33
+ padding: 8
33
34
  });
34
35
  }
35
-
36
- focus
37
36
  end
38
37
 
39
- def focus
40
- `#@canvas_input.focus();`
38
+ def focus!
39
+ `#@canvas_input.focus()`
41
40
  end
42
41
 
43
42
  def value
@@ -63,6 +62,10 @@ module Sottolio
63
62
  }
64
63
  end
65
64
 
65
+ def valid?
66
+ Utils.acceptable_constraint?(@constraint)
67
+ end
68
+
66
69
  def present?
67
70
  not destroyed?
68
71
  end
@@ -21,11 +21,8 @@ module Sottolio
21
21
  def initialize(element, hash = nil)
22
22
  super element, '2d'
23
23
 
24
- @canvas_id = element
25
-
26
- %x{
27
- #@canvas_text = new CanvasText;
28
- }
24
+ @canvas_id = element
25
+ @canvas_text = `new CanvasText()`
29
26
 
30
27
  config hash if hash
31
28
  end
@@ -36,7 +33,8 @@ module Sottolio
36
33
  canvasId: #{hash[:canvas_id]},
37
34
  fontFamily: #{hash[:font_family]},
38
35
  fontSize: #{hash[:font_size]},
39
- fontColor: #{hash[:font_color]}
36
+ fontColor: #{hash[:font_color]},
37
+ lineHeight: 30
40
38
  });
41
39
  }
42
40
  end
@@ -44,20 +42,19 @@ module Sottolio
44
42
  def draw_text(hash)
45
43
  %x{
46
44
  #@canvas_text.drawText({
47
- x: #{hash[:x]},
48
- y: #{hash[:y]},
49
- text: #{hash[:text]},
50
- boxWidth: #{hash[:box_width] || '130px'}
45
+ x: #{hash[:x]},
46
+ y: #{hash[:y]},
47
+ text: #{hash[:text]},
48
+ boxWidth: 1280
51
49
  });
52
50
  }
53
51
  end
54
52
 
55
53
  def write(what, keep = false)
56
54
  draw_text({
57
- :x => 5,
58
- :y => 760,
59
- :text => what,
60
- :box_width => '130px'
55
+ :x => 10,
56
+ :y => 760,
57
+ :text => what
61
58
  })
62
59
  end
63
60
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sottolio
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: '0.2'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Giovanni Capuano