sottolio 0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +3 -0
  3. data/Gemfile +2 -0
  4. data/Gemfile.lock +31 -0
  5. data/LICENSE +676 -0
  6. data/README.md +26 -0
  7. data/Rakefile +12 -0
  8. data/bin/sottolio +29 -0
  9. data/example/game/include/CanvasText-0.4.js +500 -0
  10. data/example/game/include/canvasinput.js +379 -0
  11. data/example/game/include/howler.min.js +10 -0
  12. data/example/game/include/jquery.min.js +4 -0
  13. data/example/game/index.html +19 -0
  14. data/example/game/resources/backgrounds/city.jpg +0 -0
  15. data/example/game/resources/characters/ambrogia.png +0 -0
  16. data/example/game/resources/characters/rosalinda.png +0 -0
  17. data/example/game/resources/right_arrow.png +0 -0
  18. data/example/game/resources/sounds/Classmate.m4a +0 -0
  19. data/example/scripts/chapter_1.rb +64 -0
  20. data/example/scripts/chapter_2.rb +23 -0
  21. data/example/scripts/script.rb +2 -0
  22. data/lib/sottolio.rb +19 -0
  23. data/lib/sottolio/sottolio.rb +22 -0
  24. data/lib/sottolio/version.rb +21 -0
  25. data/opal/sottolio.rb +38 -0
  26. data/opal/sottolio/application.rb +134 -0
  27. data/opal/sottolio/database.rb +48 -0
  28. data/opal/sottolio/image_manager.rb +46 -0
  29. data/opal/sottolio/lock.rb +41 -0
  30. data/opal/sottolio/script.rb +93 -0
  31. data/opal/sottolio/sottolio.rb +29 -0
  32. data/opal/sottolio/sound_manager.rb +55 -0
  33. data/opal/sottolio/utils.rb +57 -0
  34. data/opal/sottolio/wrapper/background.rb +21 -0
  35. data/opal/sottolio/wrapper/canvas.rb +90 -0
  36. data/opal/sottolio/wrapper/canvas/canvas_button.rb +69 -0
  37. data/opal/sottolio/wrapper/canvas/canvas_input.rb +73 -0
  38. data/opal/sottolio/wrapper/canvas/canvas_text.rb +64 -0
  39. data/opal/sottolio/wrapper/character.rb +21 -0
  40. data/opal/sottolio/wrapper/image.rb +65 -0
  41. data/opal/sottolio/wrapper/sound.rb +55 -0
  42. data/sottolio.gemspec +20 -0
  43. metadata +114 -0
@@ -0,0 +1,29 @@
1
+ #--
2
+ # Copyright(C) 2013-2015 Giovanni Capuano <webmaster@giovannicapuano.net>
3
+ #
4
+ # This file is part of sottolio.
5
+ #
6
+ # sottolio 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
+ # sottolio 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 sottolio. If not, see <http://www.gnu.org/licenses/>.
18
+ #++
19
+ module Sottolio
20
+ class << self
21
+ def get(id)
22
+ `document.getElementById(id)`
23
+ end
24
+
25
+ def add_listener(event, element, callback)
26
+ `element.addEventListener(event, callback, false)`
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,55 @@
1
+ #--
2
+ # Copyright(C) 2013-2015 Giovanni Capuano <webmaster@giovannicapuano.net>
3
+ #
4
+ # This file is part of sottolio.
5
+ #
6
+ # sottolio 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
+ # sottolio 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 sottolio. If not, see <http://www.gnu.org/licenses/>.
18
+ #++
19
+ module Sottolio
20
+ class SoundManager
21
+ def initialize
22
+ @sounds = {}
23
+ end
24
+
25
+ # Actually `id` can be interpreted as an index instead of a label referring to the name of the resource
26
+ def add(id, sound)
27
+ @sounds[id.to_sym] = sound
28
+ end
29
+
30
+ def remove(id)
31
+ stop id
32
+ @sounds.delete id
33
+ end
34
+
35
+ def play(id)
36
+ @sounds[id.to_sym].play
37
+ end
38
+
39
+ def pause(id)
40
+ @sounds[id.to_sym].pause
41
+ end
42
+
43
+ def stop(id)
44
+ @sounds[id.to_sym].stop
45
+ end
46
+
47
+ def mute(id)
48
+ @sounds[id.to_sym].mute
49
+ end
50
+
51
+ def unmute(id)
52
+ @sounds[id.to_sym].unmute
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,57 @@
1
+ #--
2
+ # Copyright(C) 2013-2015 Giovanni Capuano <webmaster@giovannicapuano.net>
3
+ #
4
+ # This file is part of sottolio.
5
+ #
6
+ # sottolio 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
+ # sottolio 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 sottolio. If not, see <http://www.gnu.org/licenses/>.
18
+ #++
19
+ class String
20
+ # TODO: Find a better name for this (tl;dr replace variables with real values)
21
+ def apply(database, pattern = /\#(.+)\#/)
22
+ r = '' # we cannot #tap because of frozen strings
23
+ self.split(pattern).each { |s|
24
+ r += database.has?(s) ? database.get(s) : s
25
+ }
26
+ r
27
+ end
28
+
29
+ def filename
30
+ self.split(?/).last
31
+ end
32
+
33
+ def to_regex
34
+ self.start_with?(?/) && self.end_with?(?/) ? /#{self[1..-2]}/ : self
35
+ end
36
+ end
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
51
+ 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
56
+ end
57
+ end
@@ -0,0 +1,21 @@
1
+ #--
2
+ # Copyright(C) 2013-2015 Giovanni Capuano <webmaster@giovannicapuano.net>
3
+ #
4
+ # This file is part of sottolio.
5
+ #
6
+ # sottolio 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
+ # sottolio 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 sottolio. If not, see <http://www.gnu.org/licenses/>.
18
+ #++
19
+ module Sottolio
20
+ class Background < Image; end
21
+ end
@@ -0,0 +1,90 @@
1
+ #--
2
+ # Copyright(C) 2013-2015 Giovanni Capuano <webmaster@giovannicapuano.net>
3
+ #
4
+ # This file is part of sottolio.
5
+ #
6
+ # sottolio 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
+ # sottolio 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 sottolio. If not, see <http://www.gnu.org/licenses/>.
18
+ #++
19
+ module Sottolio
20
+ class Canvas
21
+ attr_accessor :context, :canvas_id, :canvas
22
+
23
+ def initialize(element, context = '2d')
24
+ @context = context
25
+ @canvas_id = element
26
+ @canvas = `element.getContext(context)`
27
+ end
28
+
29
+ def fill_style=(color)
30
+ `#@canvas.fillStyle = color`
31
+ end
32
+
33
+ def begin_path; `#@canvas.beginPath()` ; end
34
+ def close_path; `#@canvas.closePath()` ; end
35
+ def move_to(x, y); `#@canvas.moveTo(x, y)`; end
36
+ def line_to(x, y); `#@canvas.lineTo(x, y)`; end
37
+ def stroke; `#@canvas.stroke()` ; end
38
+ def fill; `#@canvas.fill()` ; end
39
+
40
+ def quadratic_curve_to(cpx, cpy, x, y)
41
+ `#@canvas.quadraticCurveTo(cpx, cpy, x, y)`
42
+ end
43
+
44
+ def global_alpha=(opacity)
45
+ `#@canvas.globalAlpha = opacity`
46
+ end
47
+
48
+ def clear_rect(x, y, width, height)
49
+ `#@canvas.clearRect(x, y, width, height)`
50
+ end
51
+
52
+ def round_rect(x, y, width, height, radius = 5, fill = true, stroke = true)
53
+ begin_path
54
+ move_to x + radius, y
55
+ line_to x + width - radius, y
56
+ quadratic_curve_to x + width, y, x + width, y + radius
57
+ line_to x + width, y + height - radius
58
+ quadratic_curve_to x + width, y + height, x + width - radius, y + height
59
+ line_to x + radius, y + height
60
+ quadratic_curve_to x, y + height, x, y + height - radius
61
+ line_to x, y + radius
62
+ quadratic_curve_to x, y, x + radius, y
63
+ close_path
64
+
65
+ self.stroke if stroke
66
+ self.fill if fill
67
+ end
68
+
69
+ def collides?(rect, x, y)
70
+ x > rect[:x] && x < rect[:x] + rect[:w] && y > rect[:y] && y < rect[:y] + rect[:h]
71
+ end
72
+
73
+ def draw(ary)
74
+ ary.each { |image|
75
+ draw_image image[:src], image[:x], image[:y]
76
+ }
77
+ end
78
+
79
+ def draw_image(*args)
80
+ image = args.shift
81
+ x, y, width, height = args
82
+ `#@canvas.drawImage(image, x, y)`
83
+ end
84
+
85
+ def clear
86
+ fill_style = '#000'
87
+ round_rect 2, 725, 1276, 120
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,69 @@
1
+ #--
2
+ # Copyright(C) 2013-2015 Giovanni Capuano <webmaster@giovannicapuano.net>
3
+ #
4
+ # This file is part of sottolio.
5
+ #
6
+ # sottolio 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
+ # sottolio 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 sottolio. If not, see <http://www.gnu.org/licenses/>.
18
+ #++
19
+ module Sottolio
20
+ class CanvasButton < CanvasText
21
+ def initialize(element, hash = nil, lock = nil)
22
+ super element, hash
23
+
24
+ @lock = lock || ::Lock.new
25
+ end
26
+
27
+ def get_choice(database, options, key, on_success)
28
+ @lock.lock!
29
+
30
+ fill_style = 'rgba(200, 54, 54, 0.5)'
31
+
32
+ rect = [
33
+ { x: 250, y: 790, w: 300, h: 50, tx: 260, ty: 825 },
34
+ { x: 750, y: 790, w: 300, h: 50, tx: 760, ty: 825 }
35
+ ]
36
+
37
+ rect.each_with_index { |r, i|
38
+ round_rect r[:x], r[:y], r[:w], r[:h]
39
+
40
+ draw_text({
41
+ :x => r[:tx],
42
+ :y => r[:ty],
43
+ :text => options[i][:text],
44
+ :box_width => "#{r[:w]}px"
45
+ })
46
+
47
+ Sottolio.add_listener :click, @canvas_id, -> (e) {
48
+ x = `e.pageX` - `#@canvas_id.offsetLeft`
49
+ y = `e.pageY` - `#@canvas_id.offsetTop`
50
+
51
+ if collides? r, x, y
52
+ database.add key, options[i][:id]
53
+
54
+ rect.each { |tr|
55
+ clear_rect tr[:x] - 10,
56
+ tr[:y] - 10,
57
+ tr[:w] + 20,
58
+ tr[:h] + 15
59
+ }
60
+
61
+ @lock.free!
62
+
63
+ on_success.call
64
+ end
65
+ }
66
+ }
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,73 @@
1
+ #--
2
+ # Copyright(C) 2013-2015 Giovanni Capuano <webmaster@giovannicapuano.net>
3
+ #
4
+ # This file is part of sottolio.
5
+ #
6
+ # sottolio 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
+ # sottolio 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 sottolio. If not, see <http://www.gnu.org/licenses/>.
18
+ #++
19
+ module Sottolio
20
+ class CanvasInput
21
+ def initialize(id, database, key, text = '', x = 10, y = 800)
22
+ @database = database
23
+ @key = key
24
+ @destroyed = false
25
+
26
+ %x{
27
+ #@canvas_input = new CanvasText(id, {
28
+ x: x,
29
+ y: y,
30
+ placeHolder: text,
31
+ width: 300,
32
+ padding: 8
33
+ });
34
+ #@canvas_input.focus();
35
+ }
36
+ end
37
+
38
+ def value
39
+ `#@canvas_input.value`
40
+ end
41
+
42
+ def empty?
43
+ `#@canvas_input.value.length == 0`
44
+ end
45
+
46
+ def save
47
+ @database.add @key, value
48
+ end
49
+
50
+ def destroy
51
+ @destroy = true
52
+
53
+ %x{
54
+ #@canvas_input.unfocus();
55
+ #@canvas_input.refresh = null;
56
+ #@canvas_input.destroy();
57
+ delete #@canvas_input;
58
+ }
59
+ end
60
+
61
+ def present?
62
+ not destroyed?
63
+ end
64
+
65
+ def destroyed?
66
+ @destroy
67
+ end
68
+
69
+ def save_and_destroy
70
+ save && destroy
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,64 @@
1
+ #--
2
+ # Copyright(C) 2013-2015 Giovanni Capuano <webmaster@giovannicapuano.net>
3
+ #
4
+ # This file is part of sottolio.
5
+ #
6
+ # sottolio 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
+ # sottolio 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 sottolio. If not, see <http://www.gnu.org/licenses/>.
18
+ #++
19
+ module Sottolio
20
+ class CanvasText < Canvas
21
+ def initialize(element, hash = nil)
22
+ super element, '2d'
23
+
24
+ @canvas_id = element
25
+
26
+ %x{
27
+ #@canvas_text = new CanvasText;
28
+ }
29
+
30
+ config hash if hash
31
+ end
32
+
33
+ def config(hash)
34
+ %x{
35
+ #@canvas_text.config({
36
+ canvasId: #{hash[:canvas_id]},
37
+ fontFamily: #{hash[:font_family]},
38
+ fontSize: #{hash[:font_size]},
39
+ fontColor: #{hash[:font_color]}
40
+ });
41
+ }
42
+ end
43
+
44
+ def draw_text(hash)
45
+ %x{
46
+ #@canvas_text.drawText({
47
+ x: #{hash[:x]},
48
+ y: #{hash[:y]},
49
+ text: #{hash[:text]},
50
+ boxWidth: #{hash[:box_width] || '130px'}
51
+ });
52
+ }
53
+ end
54
+
55
+ def write(what, keep = false)
56
+ draw_text({
57
+ :x => 5,
58
+ :y => 760,
59
+ :text => what,
60
+ :box_width => '130px'
61
+ })
62
+ end
63
+ end
64
+ end