yeah 0.2.2 → 0.3.3

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 (44) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +1 -1
  3. data/README.md +52 -20
  4. data/bin/yeah +30 -0
  5. data/lib/yeah.rb +6 -9
  6. data/lib/yeah/_platform/asset.rb +24 -0
  7. data/lib/yeah/_platform/display.rb +181 -0
  8. data/lib/yeah/_platform/image.rb +16 -0
  9. data/lib/yeah/_platform/keyboard.rb +37 -0
  10. data/lib/yeah/_platform/mouse.rb +30 -0
  11. data/lib/yeah/_platform/sound.rb +15 -0
  12. data/lib/yeah/_platform/ticker.rb +21 -0
  13. data/lib/yeah/_template/Gemfile +4 -0
  14. data/lib/yeah/_template/code/code.rb +4 -0
  15. data/lib/yeah/_template/code/game.rb +7 -0
  16. data/lib/yeah/_web.rb +9 -0
  17. data/lib/yeah/color.rb +45 -13
  18. data/lib/yeah/constants.rb +6 -0
  19. data/lib/yeah/game.rb +89 -41
  20. data/lib/yeah/vector.rb +122 -99
  21. data/lib/yeah/version.rb +3 -0
  22. data/lib/yeah/web/dependencies.rb +2 -0
  23. data/lib/yeah/web/runner.html.erb +61 -0
  24. data/lib/yeah/web/server.rb +60 -0
  25. data/lib/yeah/web/setup.rb +5 -0
  26. data/lib/yeah/web/start.rb +1 -0
  27. data/opal/yeah/web.rb +8 -0
  28. data/opal/yeah/web/asset.opal +38 -0
  29. data/opal/yeah/web/constants.opal +5 -0
  30. data/opal/yeah/web/display.opal +244 -0
  31. data/opal/yeah/web/image.opal +23 -0
  32. data/opal/yeah/web/keyboard.opal +139 -0
  33. data/opal/yeah/web/mouse.opal +58 -0
  34. data/opal/yeah/web/sound.opal +19 -0
  35. data/opal/yeah/web/ticker.opal +39 -0
  36. metadata +111 -19
  37. data/CHANGELOG.md +0 -28
  38. data/lib/monkey/numeric.rb +0 -6
  39. data/lib/yeah/basic_physics.rb +0 -11
  40. data/lib/yeah/desktop.rb +0 -72
  41. data/lib/yeah/entity.rb +0 -137
  42. data/lib/yeah/map.rb +0 -40
  43. data/lib/yeah/rectangle.rb +0 -21
  44. data/lib/yeah/surface.rb +0 -66
@@ -1,11 +0,0 @@
1
- module Yeah::BasicPhysics
2
- attr_writer :velocity
3
-
4
- def velocity
5
- @velocity ||= V[0, 0, 0]
6
- end
7
-
8
- def move
9
- self.position += @velocity
10
- end
11
- end
data/lib/yeah/desktop.rb DELETED
@@ -1,72 +0,0 @@
1
- require 'rubygame'
2
-
3
- # Bindings to the native desktop powered by Rubygame.
4
- class Yeah::Desktop
5
- # @!attribute [r] screen
6
- # @return [Rubygame::Screen]
7
- # @!attribute resolution
8
- # @return [Vector] size of game window
9
- # @!attribute tickrate
10
- # @return [Integer] target ticks per second
11
- attr_reader :screen, :resolution, :tickrate
12
-
13
- def initialize(resolution=V[320, 180])
14
- self.resolution = resolution
15
-
16
- @clock = Rubygame::Clock.new
17
- self.tickrate = 30
18
-
19
- @pressables = {}
20
- pressables_keys = [(:a..:z).to_a, (:A..:Z).to_a, (0..9).to_a,
21
- :up, :down, :left, :right].flatten
22
- pressables_keys.each { |pk| @pressables[pk] = false }
23
- end
24
-
25
- def resolution=(value)
26
- @screen = Rubygame::Screen.new(value.components[0..1])
27
- @resolution = value
28
- end
29
-
30
- def tickrate=(value)
31
- @clock.target_framerate = value
32
- @tickrate = value
33
- end
34
-
35
- # Project a surface onto screen.
36
- # @param [Surface]
37
- def render(surface)
38
- masks = [0x0000ff, 0x00ff00, 0xff0000, 0]
39
- rg_surface = Rubygame::Surface.new(surface.size.to_a[0..1], masks: masks)
40
- rg_surface.pixels = surface.data
41
- rg_surface.blit(screen, [0, 0])
42
- screen.update
43
- end
44
-
45
- # Execute passed block on each tick.
46
- # @yield
47
- def each_tick
48
- loop do
49
- yield
50
- @clock.tick
51
- end
52
- end
53
-
54
- # Press a key or button.
55
- # @param [Symbol|Integer] key or button
56
- def press(pressable)
57
- @pressables[pressable] = true
58
- end
59
-
60
- # Release a key or button.
61
- # @param [Symbol|Integer] key or button
62
- def release(pressable)
63
- @pressables[pressable] = false
64
- end
65
-
66
- # Is a key or button being pressed?
67
- # @param [Symbol|Integer] key or button
68
- def pressing?(*pressables)
69
- raise ArgumentError if pressables.empty?
70
- pressables.any? { |p| @pressables[p] }
71
- end
72
- end
data/lib/yeah/entity.rb DELETED
@@ -1,137 +0,0 @@
1
- # Game object.
2
- class Yeah::Entity
3
- # @!attribute position
4
- # @return [Vector] position within a game
5
- # @!attribute size
6
- # @return [NilClass|Vector] visual size
7
- # @!attribute state
8
- # @return [Symbol] state in game
9
- # @!attribute visual
10
- # @return [Visual] visual representation within a game
11
- # @!attribute game
12
- # @return [Game] game to which this belongs to
13
- attr_accessor :position, :state, :visual
14
- attr_reader :game
15
- attr_writer :size
16
-
17
- def initialize(position=V[])
18
- @position = position
19
- end
20
-
21
- class << self
22
- def define_position_helpers
23
- %w(x y z).each_with_index do |coord, i|
24
- define_method(coord) { @position[i] }
25
- define_method("#{coord}=") { |val| @position[i] = val }
26
- end
27
- end
28
- end
29
-
30
- def size
31
- @size || visual && visual.size || V[]
32
- end
33
-
34
- def game=(value)
35
- @game = value
36
- @game.entities << self unless @game.entities.include? self
37
- end
38
-
39
- # @!attribute x
40
- # @return [Vector] position.x
41
- # @!attribute y
42
- # @return [Vector] position.y
43
- # @!attribute z
44
- # @return [Vector] position.z
45
- define_position_helpers
46
-
47
- # Update entity.
48
- def update
49
- end
50
-
51
- # Get visual representation from visual.
52
- # @return [Surface] visual representation
53
- def draw
54
- visual.draw if visual
55
- end
56
-
57
- def pressing?(pressable)
58
- game.pressing? pressable
59
- end
60
-
61
- def control(attrName, input, value)
62
- if input.class == Array
63
- polarity = 0
64
- polarity += 1 if game.platform.pressing?(input.first)
65
- polarity -= 1 if game.platform.pressing?(input.last)
66
- else
67
- polarity = game.platform.pressing?(input) ? 1 : -1
68
- end
69
-
70
- self.instance_eval("#{attrName} += #{value} * #{polarity}")
71
- end
72
-
73
- # X of right edge.
74
- # @return [Integer]
75
- def right
76
- position.x + size.x
77
- end
78
-
79
- # X of left edge.
80
- # @return [Integer]
81
- def left
82
- position.x
83
- end
84
-
85
- # Y of top edge.
86
- # @return [Integer]
87
- def top
88
- position.y + size.y
89
- end
90
-
91
- # Y of bottom edge.
92
- # @return [Integer]
93
- def bottom
94
- position.y
95
- end
96
-
97
- # Z of front edge.
98
- # @return [Integer]
99
- def front
100
- position.z + size.z
101
- end
102
-
103
- # Z of back edge.
104
- # @return [Integer]
105
- def back
106
- position.z
107
- end
108
-
109
- # Coordinate of center.
110
- # @return [Vector]
111
- def center
112
- position + size / 2
113
- end
114
-
115
- # Is intersected with other entity or entity of subclass?
116
- # @return [Boolean]
117
- def touching?(other)
118
- return false if other == self
119
-
120
- if other.is_a?(Class)
121
- if game
122
- return game.entities.select { |e| e.is_a? other }
123
- .any? { |e| touching? e }
124
- else
125
- return false
126
- end
127
- end
128
-
129
- return false if size == V[] || other.size == V[]
130
-
131
- not_touching_x = left > other.right || right < other.left
132
- not_touching_y = bottom > other.top || top < other.bottom
133
- not_touching_z = back > other.front || front < other.back
134
-
135
- !(not_touching_x && not_touching_y && not_touching_z)
136
- end
137
- end
data/lib/yeah/map.rb DELETED
@@ -1,40 +0,0 @@
1
- # A map of entities for a Game.
2
- class Yeah::Map
3
- # @!attribute background
4
- # @return [Color] background color
5
- # @!attribute key
6
- # @return [Hash] tile key
7
- # @!attribute tile_size
8
- # @return [Vector] size of each character in #tiles
9
- # @!attribute tiles
10
- # @return [Array<String>] entities at relative positions
11
- attr_accessor :background, :key, :tile_size, :tiles
12
-
13
- def self.background(background)
14
- @@background = background
15
- end
16
-
17
- def self.key(key)
18
- @@key = key
19
- end
20
-
21
- def self.tile_size(tile_size)
22
- @@tile_size = tile_size
23
- end
24
-
25
- def self.tiles(tiles)
26
- @@tiles = tiles
27
- end
28
-
29
- def initialize
30
- @background = @@background || Color[]
31
- @key = @@key ||= {}
32
- @tile_size = @@tile_size ||= nil
33
- @tiles = @@tiles ||= []
34
- end
35
-
36
- def key=(key)
37
- @key = key
38
- self.tile_size = @key.first.last.new.size if @key.first && tile_size.nil?
39
- end
40
- end
@@ -1,21 +0,0 @@
1
- # Colored rectangle Visual.
2
- class Yeah::Rectangle
3
- # @!attribute size
4
- # @return [Vector]
5
- # @!attribute color
6
- # @return [Color]
7
- attr_accessor :size, :color
8
-
9
- def initialize(size=V[], color=Color[*[255]*4])
10
- @size = size
11
- @color = color
12
- end
13
-
14
- # Surface representation.
15
- # @return [Surface]
16
- def draw
17
- surface = Surface.new(size)
18
- surface.fill(color)
19
- surface
20
- end
21
- end
data/lib/yeah/surface.rb DELETED
@@ -1,66 +0,0 @@
1
- # Rectangular pixel data.
2
- class Yeah::Surface
3
- # @!attribute size
4
- # @return [Vector]
5
- # @!attribute data
6
- # @param [Symbol] color byte order (:rgba or :bgra)
7
- # @return [String] pixel data as string of bytes
8
- attr_reader :size
9
- attr_accessor :data
10
-
11
- def initialize(size=V[])
12
- self.size = size
13
- end
14
-
15
- def size=(value)
16
- @size = value
17
- @data = "\x00" * 4 * size.x * size.y
18
- end
19
-
20
- # Color of pixel at a position.
21
- # @param [Vector] position of pixel
22
- # @return [Color]
23
- def color_at(position)
24
- data_lines = data.scan(/.{#{size.x*4}}/)
25
- line = data_lines[position.y]
26
- color_string = line[position.x*4..position.x*4+3]
27
- color_bytes = color_string.unpack('H*')[0].
28
- scan(/.{2}/).map { |b| b.to_i(16) }
29
- Color[*color_bytes]
30
- end
31
-
32
- # Fill a rectangular area with a color.
33
- # @param [Color] fill color
34
- # @param [Vector] position of first corner
35
- # @param [Vector] position of other corner
36
- def fill(color, position1=V[0, 0], position2=size-1)
37
- color_byte_string = color.rgba_bytes.pack('C*')
38
- data_lines = data.scan(/.{#{size.x*4}}/)
39
-
40
- rect_width = (position2.x - position1.x).abs + 1
41
- (position1.y..position2.y).each do |i|
42
- line = data_lines[i]
43
- color_bytes_row = color_byte_string * rect_width
44
- line[position1.x*4...(position2.x+1)*4] = color_bytes_row
45
- end
46
-
47
- @data = data_lines.join
48
- end
49
-
50
- # Draw onto other surface.
51
- # @param [Surface] surface to draw on
52
- # @param [Vector] position to draw on other surface
53
- def draw(surface, position=V[0, 0])
54
- data_lines = data.scan(/.{#{size.x*4}}/)
55
- surface_data_lines = surface.data.scan(/.{#{surface.size.x*4}}/)
56
-
57
- (position.y...position.y+surface.size.height).each_with_index do |y, i|
58
- line = data_lines[y]
59
- surface_line = surface_data_lines[i]
60
-
61
- line[position.x*4...(position.x+surface.size.width)*4] = surface_line
62
- end
63
-
64
- @data = data_lines.join
65
- end
66
- end