chingu 0.8.1 → 0.9rc1

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.
@@ -0,0 +1,67 @@
1
+ #--
2
+ #
3
+ # Chingu -- OpenGL accelerated 2D game framework for Ruby
4
+ # Copyright (C) 2009 ippa / ippa@rubylicio.us
5
+ #
6
+ # This library is free software; you can redistribute it and/or
7
+ # modify it under the terms of the GNU Lesser General Public
8
+ # License as published by the Free Software Foundation; either
9
+ # version 2.1 of the License, or (at your option) any later version.
10
+ #
11
+ # This library 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 GNU
14
+ # Lesser General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Lesser General Public
17
+ # License along with this library; if not, write to the Free Software
18
+ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
+ #
20
+ #++
21
+
22
+ module Chingu
23
+ module Async
24
+
25
+ class TaskList
26
+ # extend Forwardable
27
+ # def_delegator :@queue, :push, :enq
28
+ # def_delegator :@queue, :shift, :deq
29
+ # def_delegator :@queue, :first, :front
30
+ # def_delegator :@queue, :clear
31
+
32
+ def initialize
33
+ @queue = []
34
+ end
35
+
36
+ #
37
+ # Processes the first task on the queue, each tick removing the
38
+ # task once it has finished.
39
+ #
40
+ def update(object)
41
+ if task = front
42
+ task.update object
43
+ deq if task.finished?
44
+ task
45
+ end
46
+ end
47
+
48
+ def front
49
+ @queue.first
50
+ end
51
+
52
+ def enq(*tasks)
53
+ @queue.push(*tasks)
54
+ end
55
+
56
+ def deq
57
+ @queue.shift
58
+ end
59
+
60
+ def clear
61
+ @queue.clear
62
+ end
63
+
64
+ end
65
+
66
+ end
67
+ end
@@ -0,0 +1,48 @@
1
+ #--
2
+ #
3
+ # Chingu -- OpenGL accelerated 2D game framework for Ruby
4
+ # Copyright (C) 2009 ippa / ippa@rubylicio.us
5
+ #
6
+ # This library is free software; you can redistribute it and/or
7
+ # modify it under the terms of the GNU Lesser General Public
8
+ # License as published by the Free Software Foundation; either
9
+ # version 2.1 of the License, or (at your option) any later version.
10
+ #
11
+ # This library 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 GNU
14
+ # Lesser General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Lesser General Public
17
+ # License along with this library; if not, write to the Free Software
18
+ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
+ #
20
+ #++
21
+
22
+ module Chingu
23
+ module AsyncTasks
24
+
25
+ #
26
+ # Single method call as an asynchronous task.
27
+ #
28
+ class Call < Chingu::Async::BasicTask
29
+
30
+ def initialize(method, *args)
31
+ super()
32
+ @method, @args = method, args
33
+ @finished = false
34
+ end
35
+
36
+ def update(object)
37
+ object.send(@method, *@args)
38
+ @finished = true
39
+ end
40
+
41
+ def finished?
42
+ !!@finished
43
+ end
44
+
45
+ end
46
+
47
+ end
48
+ end
@@ -0,0 +1,48 @@
1
+ #--
2
+ #
3
+ # Chingu -- OpenGL accelerated 2D game framework for Ruby
4
+ # Copyright (C) 2009 ippa / ippa@rubylicio.us
5
+ #
6
+ # This library is free software; you can redistribute it and/or
7
+ # modify it under the terms of the GNU Lesser General Public
8
+ # License as published by the Free Software Foundation; either
9
+ # version 2.1 of the License, or (at your option) any later version.
10
+ #
11
+ # This library 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 GNU
14
+ # Lesser General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Lesser General Public
17
+ # License along with this library; if not, write to the Free Software
18
+ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
+ #
20
+ #++
21
+
22
+ module Chingu
23
+ module AsyncTasks
24
+
25
+ #
26
+ # Block execution as an asynchronous task.
27
+ #
28
+ class Exec < Chingu::Async::BasicTask
29
+
30
+ def initialize(&block)
31
+ super()
32
+ @block = block
33
+ @finished = false
34
+ end
35
+
36
+ def update(object)
37
+ @block[]
38
+ @finished = true
39
+ end
40
+
41
+ def finished?
42
+ !!@finished
43
+ end
44
+
45
+ end
46
+
47
+ end
48
+ end
@@ -0,0 +1,45 @@
1
+ #--
2
+ #
3
+ # Chingu -- OpenGL accelerated 2D game framework for Ruby
4
+ # Copyright (C) 2009 ippa / ippa@rubylicio.us
5
+ #
6
+ # This library is free software; you can redistribute it and/or
7
+ # modify it under the terms of the GNU Lesser General Public
8
+ # License as published by the Free Software Foundation; either
9
+ # version 2.1 of the License, or (at your option) any later version.
10
+ #
11
+ # This library 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 GNU
14
+ # Lesser General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Lesser General Public
17
+ # License along with this library; if not, write to the Free Software
18
+ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
+ #
20
+ #++
21
+
22
+ require "#{CHINGU_ROOT}/chingu/async_tasks/tween"
23
+
24
+ module Chingu
25
+ module AsyncTasks
26
+
27
+ #
28
+ # Syntactic sugar for tween(duration, :x => x, :y => y, :angle => angle)
29
+ #
30
+ class Move < Tween
31
+
32
+ def initialize(duration, x, y, angle = nil)
33
+ properties = { }
34
+
35
+ properties[:x] = x unless x.nil?
36
+ properties[:y] = y unless y.nil?
37
+ properties[:angle] = angle unless angle.nil?
38
+
39
+ super(duration, properties)
40
+ end
41
+
42
+ end
43
+
44
+ end
45
+ end
@@ -0,0 +1,63 @@
1
+ #--
2
+ #
3
+ # Chingu -- OpenGL accelerated 2D game framework for Ruby
4
+ # Copyright (C) 2009 ippa / ippa@rubylicio.us
5
+ #
6
+ # This library is free software; you can redistribute it and/or
7
+ # modify it under the terms of the GNU Lesser General Public
8
+ # License as published by the Free Software Foundation; either
9
+ # version 2.1 of the License, or (at your option) any later version.
10
+ #
11
+ # This library 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 GNU
14
+ # Lesser General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Lesser General Public
17
+ # License along with this library; if not, write to the Free Software
18
+ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
+ #
20
+ #++
21
+
22
+ module Chingu
23
+ module AsyncTasks
24
+
25
+ #
26
+ # Executes all subtasks in parallel.
27
+ #
28
+ class Parallel < Chingu::Async::BasicTask
29
+
30
+ def initialize(&block)
31
+ @subtasks = []
32
+
33
+ # Make @subtasks behave like a TaskList for the TaskBuilder.
34
+ # This is probably a dirty hack!
35
+ class <<@subtasks
36
+ alias :enq :push
37
+ alias :deq :shift
38
+ alias :front :first
39
+ end
40
+
41
+ add_tasks(&block)
42
+ end
43
+
44
+ def add_tasks(&block)
45
+ builder = Chingu::Async::TaskBuilder.new(@subtasks)
46
+ block[builder]
47
+ end
48
+
49
+ #
50
+ # Returns true if all subtasks have finished executing.
51
+ #
52
+ def finished?
53
+ @subtasks.empty? or @subtasks.all?(&:finished?)
54
+ end
55
+
56
+ def update(object)
57
+ @subtasks.each { |task| task.update(object) }
58
+ end
59
+
60
+ end
61
+
62
+ end
63
+ end
@@ -0,0 +1,73 @@
1
+ #--
2
+ #
3
+ # Chingu -- OpenGL accelerated 2D game framework for Ruby
4
+ # Copyright (C) 2009 ippa / ippa@rubylicio.us
5
+ #
6
+ # This library is free software; you can redistribute it and/or
7
+ # modify it under the terms of the GNU Lesser General Public
8
+ # License as published by the Free Software Foundation; either
9
+ # version 2.1 of the License, or (at your option) any later version.
10
+ #
11
+ # This library 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 GNU
14
+ # Lesser General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Lesser General Public
17
+ # License along with this library; if not, write to the Free Software
18
+ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
+ #
20
+ #++
21
+
22
+ module Chingu
23
+ module AsyncTasks
24
+
25
+ #
26
+ # Basic tweening for numerical properties.
27
+ #
28
+ # game_object.async.tween 1000, :property => new_value
29
+ #
30
+ class Tween < Chingu::Async::BasicTask
31
+
32
+ # TODO Tweening is pretty dumb...make it smarter.
33
+
34
+ def initialize(duration, properties)
35
+ super()
36
+ @have_initial_values = false
37
+ @age, @life = 0, duration
38
+ @properties = properties
39
+ end
40
+
41
+ def update(object)
42
+ set_initial_values(object) unless have_initial_values?
43
+
44
+ @age += $window.milliseconds_since_last_tick
45
+ t = @age.to_f / @life
46
+ t = 1.0 if t > 1
47
+ @properties.each do |name, range|
48
+ object.send "#{name}=", range.interpolate(t)
49
+ end
50
+ end
51
+
52
+ def finished?
53
+ @age >= @life
54
+ end
55
+
56
+ private
57
+
58
+ def set_initial_values(object)
59
+ # TODO find a better and more general way to set up tweening.
60
+ @properties.each do |name, value|
61
+ @properties[name] = object.send(name) .. value
62
+ end
63
+ @have_initial_values = true
64
+ end
65
+
66
+ def have_initial_values?
67
+ !!@have_initial_values
68
+ end
69
+
70
+ end
71
+
72
+ end
73
+ end
@@ -0,0 +1,54 @@
1
+ #--
2
+ #
3
+ # Chingu -- OpenGL accelerated 2D game framework for Ruby
4
+ # Copyright (C) 2009 ippa / ippa@rubylicio.us
5
+ #
6
+ # This library is free software; you can redistribute it and/or
7
+ # modify it under the terms of the GNU Lesser General Public
8
+ # License as published by the Free Software Foundation; either
9
+ # version 2.1 of the License, or (at your option) any later version.
10
+ #
11
+ # This library 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 GNU
14
+ # Lesser General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Lesser General Public
17
+ # License along with this library; if not, write to the Free Software
18
+ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
+ #
20
+ #++
21
+
22
+ module Chingu
23
+ module AsyncTasks
24
+
25
+ #
26
+ # Halts processing of tasks until the passed-in block returns a
27
+ # true value, or the timeout expires. If no block is given, behaves as if
28
+ # the block returned nil.
29
+ #
30
+ class Wait < Chingu::Async::BasicTask
31
+
32
+ attr_accessor :timeout, :condition
33
+ attr_reader :result
34
+
35
+ def initialize(timeout = nil, &condition)
36
+ super()
37
+ @result = nil
38
+ @age, @life = 0, timeout
39
+ @condition = condition
40
+ end
41
+
42
+ def update(object)
43
+ @age += $window.milliseconds_since_last_tick
44
+ @result = (@condition and @condition[])
45
+ end
46
+
47
+ def finished?
48
+ !!@result or (@life != nil and @age >= @life)
49
+ end
50
+
51
+ end
52
+
53
+ end
54
+ end
@@ -180,7 +180,7 @@ module Chingu
180
180
  def alpha=(value)
181
181
  value = 0 if value < 0
182
182
  value = 255 if value > 255
183
- @color.alpha = value
183
+ @color.alpha = value.to_i
184
184
  end
185
185
 
186
186
  #
@@ -0,0 +1,13 @@
1
+ class Range
2
+
3
+ #
4
+ # Linearly interpolates a value between begin and end.
5
+ #
6
+ def interpolate(t)
7
+ # TODO possibly allow geometric or arbitrary interpolation
8
+ a, b = self.begin, self.end
9
+ ta, tb = (1.0 - t), t
10
+ a * ta + b * tb
11
+ end
12
+
13
+ end
@@ -30,8 +30,9 @@ module Chingu
30
30
 
31
31
  def initialize(options = {})
32
32
  super
33
-
34
- Text.create("<u>Please enter your name</u>", :rotation_center => :top_center, :x => $window.width/2, :y => 10, :size => 40)
33
+
34
+ #@title = options[:title] || "<u>Please enter your name</u>"
35
+ #Text.create(@title, :rotation_center => :top_center, :x => $window.width/2, :y => 10, :size => 40)
35
36
 
36
37
  on_input([:holding_up, :holding_w, :holding_gamepad_up], :up)
37
38
  on_input([:holding_down, :holding_s, :holding_gamepad_down], :down)
@@ -41,16 +42,16 @@ module Chingu
41
42
  on_input(:esc, :pop_game_state)
42
43
 
43
44
  @callback = options[:callback]
44
- @columns = options[:columns] || 10
45
+ @columns = options[:columns] || 14
45
46
 
46
47
  @string = []
47
48
  @texts = []
48
- @index = 1
49
+ @index = 0
49
50
  @letter_size = 30
50
- @letters = %w[ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z ! " # % & ( ) [ ] / \\ - = * SPACE DEL ENTER ]
51
+ @letters = %w[ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z ! " # % & $ ( ) [ ] / \\ - + = * . 1 2 3 4 5 6 7 8 9 0 SPACE DEL ENTER ]
51
52
 
52
53
  @y = 140
53
- @x = ($window.width - 350)/2
54
+ @x = ($window.width - 600)/2
54
55
 
55
56
  @letters.each_with_index do |letter, index|
56
57
  @texts << Text.create(letter, :x => @x, :y => @y, :size => @letter_size)
@@ -62,7 +63,7 @@ module Chingu
62
63
  end
63
64
  end
64
65
 
65
- @selected_color = Color::RED
66
+ @texts[@index].color = Color::RED
66
67
  @name = Text.create("", :rotaion_center => :top_center, :x => $window.width/2, :y => 60, :size => 80)
67
68
  end
68
69
 
@@ -80,14 +81,23 @@ module Chingu
80
81
 
81
82
  # Move cursor any given value (positive or negative). Used by left() and right()
82
83
  def move_cursor(amount = 1)
83
- @index += amount
84
- @index = 0 if @index >= @letters.size
85
- @index = @letters.size-1 if @index < 0
84
+ #
85
+ # Curser will wrap
86
+ #
87
+ #@index += amount
88
+ #@index = 0 if @index >= @letters.size
89
+ #@index = @letters.size-1 if @index < 0
90
+
91
+ #
92
+ # Cursor won't wrap
93
+ #
94
+ new_value = @index + amount
95
+ @index = new_value if new_value < @letters.size && new_value >= 0
86
96
 
87
97
  @texts.each { |text| text.color = Color::WHITE }
88
98
  @texts[@index].color = Color::RED
89
99
 
90
- sleep(0.1)
100
+ sleep(0.15)
91
101
  end
92
102
 
93
103
  def action