chobo 0.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.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in chobo.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Erik
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # Chobo
2
+
3
+ Get started with game programming using Ruby/Gosu as quickly as possible using Chobo!
4
+
5
+ ## Installation
6
+
7
+ Install the gem
8
+
9
+ gem install chobo
10
+
11
+ Create a new game
12
+
13
+ chobo new my_awesome_game
14
+
15
+ This will create the following file structure
16
+
17
+ .
18
+ |____assets
19
+ |____config.rb
20
+ |____content
21
+ | |____audio
22
+ | |____gfx
23
+ |____game_window.rb
24
+ |____lib
25
+ | |____behaviors
26
+ | |____game.rb
27
+ |____my_awesome_game.rb
28
+
29
+ Run the game!
30
+
31
+ ruby my_awesome_game.rb
32
+
33
+ ## Usage
34
+
35
+ # TODO, behaviors and stuff. Game structure ex...
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/chobo ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'chobo'
4
+ Chobo::Commands.run(ARGV)
data/chobo.gemspec ADDED
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'chobo/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "chobo"
8
+ gem.version = Chobo::VERSION
9
+ gem.authors = ["Erik Skoglund"]
10
+ gem.email = ["erikskoglund88@gmail.com"]
11
+ gem.description = %q{Chobo is a simple way to start new game projects with gosu}
12
+ gem.summary = %q{The purpose of Chobo is to create boilerplate code for gosu projects}
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ gem.add_dependency 'gosu', '0.7.45'
20
+ end
data/lib/chobo.rb ADDED
@@ -0,0 +1,12 @@
1
+ require "chobo/version"
2
+
3
+ require 'gosu'
4
+ require "chobo/commands"
5
+ require "chobo/vec2"
6
+ require "chobo/helpers"
7
+ require "chobo/game_object"
8
+ require "chobo/entity"
9
+ require "chobo/behavior"
10
+
11
+ module Chobo
12
+ end
@@ -0,0 +1,40 @@
1
+ module Chobo
2
+ class Animation
3
+
4
+ attr_accessor :frames, :interval
5
+
6
+ def initialize frames, interval, looping = true
7
+ @frames = frames
8
+ @interval = interval
9
+ @looping = looping
10
+ @state = :running
11
+ reset()
12
+ end
13
+
14
+ def reset
15
+ @current = 0.0
16
+ @current_frame = 0
17
+ @state = :running
18
+ end
19
+
20
+ def update dt
21
+ if @state == :running
22
+ @current += dt
23
+ if @current > @interval
24
+ @current_frame += 1
25
+ if @current_frame > @frames.size - 1
26
+ if @looping
27
+ reset()
28
+ else
29
+ @state = :stopped
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+
36
+ def frame
37
+ @frames[@current_frame]
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,14 @@
1
+ module Chobo
2
+ class Behavior
3
+ def on_added game_object
4
+ # called when added to a game_object
5
+ end
6
+
7
+ def on_removed game_object
8
+ # called when removed from a game_object
9
+ end
10
+
11
+ def update dt, game_object
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,186 @@
1
+ class String
2
+ def colorize(color_code)
3
+ "\e[#{color_code}m#{self}\e[0m"
4
+ end
5
+
6
+ def red
7
+ colorize(31)
8
+ end
9
+ def green
10
+ colorize(32)
11
+ end
12
+ def yellow
13
+ colorize(33)
14
+ end
15
+ def pink
16
+ colorize(35)
17
+ end
18
+ end
19
+
20
+ module Chobo
21
+ class Commands
22
+
23
+ def self.run args
24
+ case args.first
25
+ when "new"
26
+ if args.length > 1
27
+ create(args.last)
28
+ else
29
+ puts "Please supply a name for the game.".yellow
30
+ puts " ex: chobo new my_first_game".yellow
31
+ end
32
+ when "-v"
33
+ print_version()
34
+ when "-version"
35
+ print_version()
36
+ when "help"
37
+ print_help()
38
+ when nil
39
+ puts "Command not found".red
40
+ print_help()
41
+ when ""
42
+ puts "Command not found".red
43
+ print_help()
44
+ else
45
+ puts "Command not found".red
46
+ print_help
47
+ end
48
+ end
49
+
50
+ private
51
+ def self.print_version
52
+ puts "chobo-#{VERSION}"
53
+ end
54
+
55
+ def self.print_help
56
+ puts "commands:".yellow
57
+ puts " help # show help".yellow
58
+ puts " new [name] # create a new game template".yellow
59
+ puts " -v # show version".yellow
60
+ end
61
+
62
+ def self.create name
63
+ name = name.capitalize
64
+ puts "Creating game #{name}".green
65
+
66
+ Dir.mkdir('content')
67
+ Dir.mkdir('content/gfx')
68
+ Dir.mkdir('content/audio')
69
+ Dir.mkdir('assets')
70
+ Dir.mkdir('lib')
71
+ Dir.mkdir('lib/behaviors')
72
+
73
+ File.open("#{name.downcase}.rb", 'w+') do |f|
74
+ f.write get_main name
75
+ end
76
+
77
+ File.open('game_window.rb', 'w+') do |f|
78
+ f.write get_game_window name
79
+ end
80
+
81
+ File.open('config.rb', 'w+') do |f|
82
+ f.write get_config name
83
+ end
84
+
85
+ File.open('lib/game.rb', 'w+') do |f|
86
+ f.write get_game name
87
+ end
88
+
89
+ puts "Run with $ruby #{name.downcase}.rb".green
90
+ `ruby #{name.downcase}.rb`
91
+ end
92
+
93
+ def self.get_config name
94
+ <<-EOF
95
+ module #{name}
96
+ WIDTH = 1280
97
+ HEIGHT = 720
98
+ FULLSCREEN = false
99
+ TITLE = #{name}
100
+ CONTENT_ROOT = 'content'
101
+ CLEAR_COLOR = Chobo.color(255, 255, 255)
102
+ end
103
+
104
+ require_relative 'lib/game'
105
+ EOF
106
+ end
107
+
108
+ def self.get_game_window name
109
+ <<-EOF
110
+ module #{name}
111
+ class GameWindow < Gosu::Window
112
+
113
+ def initialize
114
+ super(WIDTH, HEIGHT, FULLSCREEN)
115
+ self.caption = TITLE
116
+ @game = Game.new self
117
+ end
118
+
119
+ def update
120
+ dt = 16.0
121
+ @game.update dt
122
+ end
123
+
124
+ def draw
125
+ clear
126
+ @game.draw
127
+ end
128
+
129
+ def clear
130
+ draw_quad(
131
+ 0, 0, CLEAR_COLOR,
132
+ WIDTH, 0, CLEAR_COLOR,
133
+ WIDTH, HEIGHT, CLEAR_COLOR,
134
+ 0, HEIGHT, CLEAR_COLOR
135
+ )
136
+ end
137
+ end
138
+ end
139
+ EOF
140
+ end
141
+
142
+ def self.get_game name
143
+ <<-EOF
144
+ module #{name}
145
+ class Game
146
+ attr_accessor :window
147
+
148
+ def initialize window
149
+ @window = window
150
+ @font = Gosu::Font.new(window, Gosu::default_font_name, 64)
151
+ @color = Chobo.color(126, 126, 126)
152
+ @fade_in = 2000
153
+ @process = 0.0
154
+ end
155
+
156
+ def update dt
157
+ if @window.button_down? Gosu::KbEscape
158
+ @window.close
159
+ end
160
+ @fade_in -= dt
161
+ end
162
+
163
+ def draw
164
+ if @fade_in >= 0.0
165
+ @process = (2000.0 - @fade_in) / 2000.0
166
+ @color.alpha = Chobo.qlerp(0, 255, @process).to_i
167
+ end
168
+ @font.draw_rel("/Chobo/", WIDTH / 2.0, HEIGHT / 2.0, 0, 0.5, 0.5, Chobo.qlerp(0.0, 1.0, @process), Chobo.qlerp(0.5, 1.0, @process), @color)
169
+ end
170
+ end
171
+ end
172
+ EOF
173
+ end
174
+
175
+ def self.get_main name
176
+ <<-EOF
177
+ require 'chobo'
178
+ require_relative 'config'
179
+ require_relative 'game_window'
180
+
181
+ game_window = #{name}::GameWindow.new
182
+ game_window.show
183
+ EOF
184
+ end
185
+ end
186
+ end
@@ -0,0 +1,81 @@
1
+ module Chobo
2
+ class Entity < GameObject
3
+
4
+ attr_accessor :image, :behaviors, :width, :height, :attributes, :time, :color, :flipped
5
+
6
+ def initialize(image)
7
+ super()
8
+ @image = image
9
+ @width = image.width
10
+ @height = image.height
11
+ @color = Chobo.color()
12
+ @attributes = {}
13
+ @time = 0.0
14
+ @behaviors = []
15
+ @flipped = false
16
+ end
17
+
18
+ def collides? other
19
+ if @position.x - (@width / 2.0) > other.position.x + (other.width / 2.0)
20
+ return false
21
+ end
22
+ if @position.x + (@width / 2.0) < other.position.x - (other.width / 2.0)
23
+ return false
24
+ end
25
+ if @position.y - (@width / 2.0) > other.position.y + (other.height / 2.0)
26
+ return false
27
+ end
28
+ if @position.y + (@width / 2.0) < other.position.y - (other.height / 2.0)
29
+ return false
30
+ end
31
+ return true
32
+ end
33
+
34
+ def set_color r = 255, g = 255, b = 255, a = 255
35
+ @color.red = r
36
+ @color.green = g
37
+ @color.blue = b
38
+ @color.alpha = a
39
+ self
40
+ end
41
+
42
+ def set_attribute attribute, value
43
+ @attributes[attribute] = value
44
+ self
45
+ end
46
+
47
+ def add_behavior behavior
48
+ @behaviors.push behavior
49
+ behavior.on_added self
50
+ self
51
+ end
52
+
53
+ def remove_behavior behavior
54
+ @behaviors.delete behavior
55
+ behavior.on_removed self
56
+ self
57
+ end
58
+
59
+ def get_behavior type
60
+ @behaviors.each do |b|
61
+ if b.is_a? type
62
+ return b
63
+ end
64
+ end
65
+ nil
66
+ end
67
+
68
+ def update dt
69
+ @time += dt
70
+ @behaviors.each{|b| b.update dt, self }
71
+ end
72
+
73
+ def draw
74
+ if @flipped
75
+ @image.draw_rot(@position.x.to_i, @position.y.to_i, 0, @rotation.to_degrees - 180, @origin.x, @origin.y, @scale.x * -1.0, @scale.y, @color)
76
+ else
77
+ @image.draw_rot(@position.x.to_i, @position.y.to_i, 0, @rotation.to_degrees, @origin.x, @origin.y, @scale.x, @scale.y, @color)
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,51 @@
1
+ module Chobo
2
+ class GameObject
3
+ attr_accessor :position, :velocity, :origin, :rotation, :scale, :color
4
+
5
+ def initialize
6
+ @position = Vec2.new
7
+ @velocity = Vec2.new
8
+ @origin = Vec2.new(0.5, 0.5)
9
+ @rotation = 0.0
10
+ @scale = Vec2.new(1.0, 1.0)
11
+ @color = Chobo.color()
12
+ end
13
+
14
+ def set_position x, y
15
+ @position.x, @position.y = x, y
16
+ self
17
+ end
18
+
19
+ def set_velocity x, y
20
+ @velocity.x, @velocity.y = x, y
21
+ self
22
+ end
23
+
24
+ def set_scale scale_x, scale_y = nil
25
+ if scale_y
26
+ @scale.x = scale_x
27
+ @scale.y = scale_y
28
+ else
29
+ @scale.x = scale_x
30
+ @scale.y = scale_x
31
+ end
32
+ self
33
+ end
34
+
35
+ def set_rotation rotation
36
+ @rotation = rotation
37
+ self
38
+ end
39
+
40
+ def set_color r = 255, g = 255, b = 255, a = 255
41
+ @color.red = r
42
+ @color.green = g
43
+ @color.blue = b
44
+ @color.alpha = a
45
+ self
46
+ end
47
+
48
+ def update dt
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,91 @@
1
+ module Chobo
2
+
3
+ @images = {}
4
+
5
+ def self.load_image(window, name)
6
+ return @images[name] if @images[name]
7
+ return @images[name] = Gosu::Image.new(window, "#{CONTENT_ROOT}/gfx/#{name}.png")
8
+ end
9
+
10
+ def self.load_tiles(window, name, tile_width, tile_height)
11
+ Gosu::Image.load_tiles(window, "#{CONTENT_ROOT}/gfx/#{name}.png", tile_width, tile_height, true)
12
+ end
13
+
14
+ def self.load_sample(window, name)
15
+ Gosu::Sample.new(window, "#{CONTENT_ROOT}/audio/#{name}.wav")
16
+ end
17
+
18
+ def self.load_song(window, name)
19
+ Gosu::Song.new(window, "#{CONTENT_ROOT}/audio/#{name}.wav")
20
+ end
21
+
22
+ def self.color(r = 255, g = 255, b = 255, a = 255)
23
+ clr = Gosu::Color.new
24
+ clr.red = r
25
+ clr.green = g
26
+ clr.blue = b
27
+ clr.alpha = a
28
+ clr
29
+ end
30
+
31
+ def self.wrap_angle(angle)
32
+ while angle > 360.to_radians
33
+ angle -= 360.to_radians
34
+ end
35
+ while angle < 0.0
36
+ angle += 360.to_radians
37
+ end
38
+ angle
39
+ end
40
+
41
+ def self.lerp_angle from, to, frac
42
+ if to - from > 180.to_radians
43
+ to -= 360.to_radians
44
+ end
45
+ if to - from < -180.to_radians
46
+ to += 360.to_radians
47
+ end
48
+ from + frac * (to - from)
49
+ end
50
+
51
+ # linear interpolation
52
+ def self.lerp x, y, w
53
+ x + (y - x) * w
54
+ end
55
+
56
+ # cubic interpolation using a hermite spline, aka smooth step
57
+ def self.qlerp(min, max, weight)
58
+ hermite(min, 0.0, max, 0.0, weight)
59
+ end
60
+
61
+ # value1, tangent1, value2, tangent2
62
+ def self.hermite(v1, t1, v2, t2, weight)
63
+ sCubed = weight * weight * weight
64
+ sSquared = weight * weight
65
+ result = 0.0
66
+ if weight == 0.0
67
+ result = v1
68
+ elsif weight == 1.0
69
+ result = v2
70
+ else
71
+ result = (2 * v1 - 2 * v2 + t2 + t1) * sCubed + (3 * v2 - 3 * v1 - 2 * t1 - t2) * sSquared + t1 * weight + v1
72
+ end
73
+ result
74
+ end
75
+
76
+ def self.clamp(value, min, max)
77
+ return min if value < min
78
+ return max if value > max
79
+ value
80
+ end
81
+ end
82
+
83
+ class Numeric
84
+ def to_degrees
85
+ self * 180.0 / Math::PI
86
+ end
87
+
88
+ def to_radians
89
+ self * Math::PI / 180.0
90
+ end
91
+ end
data/lib/chobo/vec2.rb ADDED
@@ -0,0 +1,9 @@
1
+ module Chobo
2
+ class Vec2
3
+ attr_accessor :x, :y
4
+
5
+ def initialize x = 0.0, y = 0.0
6
+ @x, @y = x, y
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module Chobo
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chobo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Erik Skoglund
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-04 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: gosu
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - '='
20
+ - !ruby/object:Gem::Version
21
+ version: 0.7.45
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - '='
28
+ - !ruby/object:Gem::Version
29
+ version: 0.7.45
30
+ description: Chobo is a simple way to start new game projects with gosu
31
+ email:
32
+ - erikskoglund88@gmail.com
33
+ executables:
34
+ - chobo
35
+ extensions: []
36
+ extra_rdoc_files: []
37
+ files:
38
+ - .gitignore
39
+ - Gemfile
40
+ - LICENSE.txt
41
+ - README.md
42
+ - Rakefile
43
+ - bin/chobo
44
+ - chobo.gemspec
45
+ - lib/chobo.rb
46
+ - lib/chobo/animation.rb
47
+ - lib/chobo/behavior.rb
48
+ - lib/chobo/commands.rb
49
+ - lib/chobo/entity.rb
50
+ - lib/chobo/game_object.rb
51
+ - lib/chobo/helpers.rb
52
+ - lib/chobo/vec2.rb
53
+ - lib/chobo/version.rb
54
+ homepage: ''
55
+ licenses: []
56
+ post_install_message:
57
+ rdoc_options: []
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ! '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirements: []
73
+ rubyforge_project:
74
+ rubygems_version: 1.8.25
75
+ signing_key:
76
+ specification_version: 3
77
+ summary: The purpose of Chobo is to create boilerplate code for gosu projects
78
+ test_files: []