scorched_earth 5.0.0.pre-java
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.travis.yml +3 -0
- data/Gemfile +5 -0
- data/LICENSE.txt +21 -0
- data/README.md +50 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/exe/scorched +5 -0
- data/lib/scorched_earth/color_palette.rb +54 -0
- data/lib/scorched_earth/events/entity_created.rb +5 -0
- data/lib/scorched_earth/events/game_ending.rb +5 -0
- data/lib/scorched_earth/events/mouse_moved.rb +5 -0
- data/lib/scorched_earth/events/mouse_pressed.rb +5 -0
- data/lib/scorched_earth/events/mouse_released.rb +5 -0
- data/lib/scorched_earth/explosion.rb +21 -0
- data/lib/scorched_earth/game.rb +103 -0
- data/lib/scorched_earth/game_window.rb +100 -0
- data/lib/scorched_earth/helpers.rb +46 -0
- data/lib/scorched_earth/mouse.rb +75 -0
- data/lib/scorched_earth/player.rb +17 -0
- data/lib/scorched_earth/shot.rb +41 -0
- data/lib/scorched_earth/terrain.rb +71 -0
- data/lib/scorched_earth/version.rb +3 -0
- data/lib/scorched_earth.rb +6 -0
- data/scorched_earth.gemspec +27 -0
- metadata +141 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e60a9daf9b54210e93dc7b3f976b483b327e9227
|
4
|
+
data.tar.gz: 3c33b9d2d7e31751af4a2b70b919684f70da8e62
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b71e277cf4f593bdab66c9d42c47890074f54f81cf9a1987a38e813ec69fe6cd53d173b797f12f04d7e95e8632365b2ef443542f30da32ae3e24ede439acd9a0
|
7
|
+
data.tar.gz: 2e0a0f18b34c94a69aaed1d8f3ec60f09650c59eac02c19188f6577abcfe69dcf566740a87fe9cad20dfe158f7537f8ddac27cc8ab2e6872ef2ff60001598e09
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 James Moriarty
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
# ScorchedEarth
|
2
|
+
|
3
|
+
[![Code Climate](https://codeclimate.com/github/jamesmoriarty/scorched-earth-rb/badges/gpa.svg)](https://codeclimate.com/github/jamesmoriarty/scorched-earth-rb) [![Test Coverage](https://codeclimate.com/github/jamesmoriarty/scorched-earth-rb/badges/coverage.svg)](https://codeclimate.com/github/jamesmoriarty/scorched-earth-rb/coverage)
|
4
|
+
|
5
|
+
A little [Scorched Earth](https://en.wikipedia.org/wiki/ScorchedEarth_Earth_(video_game)) clone in the Ruby Programming Language.
|
6
|
+
|
7
|
+
With a segue into procedurally generated color palettes:
|
8
|
+
- [TriadMixing](http://devmag.org.za/2012/07/29/how-to-choose-colours-procedurally-algorithms/)
|
9
|
+
- [CIE94](https://en.wikipedia.org/wiki/Color_difference#CIE94)
|
10
|
+
|
11
|
+
![Color Palettes](https://pbs.twimg.com/media/CsYukUuUMAECQG3.jpg)
|
12
|
+
|
13
|
+
## Installation
|
14
|
+
|
15
|
+
```
|
16
|
+
brew install glew libsndfile
|
17
|
+
```
|
18
|
+
|
19
|
+
Add this line to your application's Gemfile:
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
gem 'scorched_earth'
|
23
|
+
```
|
24
|
+
|
25
|
+
And then execute:
|
26
|
+
|
27
|
+
$ bundle
|
28
|
+
|
29
|
+
Or install it yourself as:
|
30
|
+
|
31
|
+
$ gem install scorched_earth
|
32
|
+
|
33
|
+
## Usage
|
34
|
+
|
35
|
+
$ scorched
|
36
|
+
|
37
|
+
## Development
|
38
|
+
|
39
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
40
|
+
|
41
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
42
|
+
|
43
|
+
## Contributing
|
44
|
+
|
45
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/scorched. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
46
|
+
|
47
|
+
|
48
|
+
## License
|
49
|
+
|
50
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "scorched_earth"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/exe/scorched
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
include Java
|
2
|
+
|
3
|
+
import java.awt.Color
|
4
|
+
|
5
|
+
module ScorchedEarth
|
6
|
+
class ColorPalette
|
7
|
+
include Enumerable
|
8
|
+
|
9
|
+
attr_reader :cache, :colors, :strategy
|
10
|
+
|
11
|
+
def initialize(*colors)
|
12
|
+
@cache = {}
|
13
|
+
@colors = colors
|
14
|
+
@strategy = Strategies::TriadMixing
|
15
|
+
end
|
16
|
+
|
17
|
+
def get(key)
|
18
|
+
cache.fetch(key) do |key|
|
19
|
+
cache[key] = first
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def each
|
24
|
+
return enum_for(:each) unless block_given?
|
25
|
+
|
26
|
+
loop do
|
27
|
+
color = strategy.color(colors)
|
28
|
+
yield color
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
module Strategies
|
33
|
+
module TriadMixing
|
34
|
+
def self.color(colors)
|
35
|
+
sum = 0.0
|
36
|
+
|
37
|
+
colors.map do |color|
|
38
|
+
sum += ratio = rand
|
39
|
+
|
40
|
+
[color, ratio]
|
41
|
+
end.inject(Color.new(0, 0, 0)) do |mix, (color, ratio)|
|
42
|
+
scale = ratio / sum
|
43
|
+
|
44
|
+
Color.new(
|
45
|
+
(mix.red + color.red * scale).to_i,
|
46
|
+
(mix.green + color.green * scale).to_i,
|
47
|
+
(mix.blue + color.blue * scale).to_i,
|
48
|
+
)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module ScorchedEarth
|
2
|
+
class Explosion
|
3
|
+
attr_reader :x, :y, :color, :radius
|
4
|
+
|
5
|
+
def initialize(x, y)
|
6
|
+
@x, @y, @radius = x, y, 25
|
7
|
+
end
|
8
|
+
|
9
|
+
def update(delta)
|
10
|
+
@radius += 25
|
11
|
+
@x -= 1_000_000 if radius > 100
|
12
|
+
end
|
13
|
+
|
14
|
+
def draw(graphics)
|
15
|
+
height = graphics.destination.height
|
16
|
+
|
17
|
+
graphics.set_color Color.white
|
18
|
+
graphics.fill_oval x - radius / 2, height - y - radius / 2, radius, radius
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,103 @@
|
|
1
|
+
require "scorched_earth/helpers"
|
2
|
+
require "scorched_earth/terrain"
|
3
|
+
require "scorched_earth/player"
|
4
|
+
require "scorched_earth/shot"
|
5
|
+
require "scorched_earth/explosion"
|
6
|
+
require "scorched_earth/mouse"
|
7
|
+
require "scorched_earth/color_palette"
|
8
|
+
require "scorched_earth/events/entity_created"
|
9
|
+
require "scorched_earth/events/game_ending"
|
10
|
+
|
11
|
+
include Java
|
12
|
+
|
13
|
+
import java.awt.Color
|
14
|
+
|
15
|
+
module ScorchedEarth
|
16
|
+
class Game
|
17
|
+
include Helpers
|
18
|
+
|
19
|
+
attr_reader :width, :height,
|
20
|
+
:color_palette, :entities, :mouse, :players, :terrain,
|
21
|
+
:events, :event_listeners
|
22
|
+
|
23
|
+
def initialize width, height
|
24
|
+
@width, @height = width, height
|
25
|
+
end
|
26
|
+
|
27
|
+
def setup
|
28
|
+
@color_palette = ColorPalette.new Color::RED, Color::YELLOW, Color::WHITE
|
29
|
+
@entities = []
|
30
|
+
@events = Queue.new
|
31
|
+
@event_listeners = []
|
32
|
+
@players = 2.times.map { |index| Player.new rand(width), color_palette.get("player_#{index}") }
|
33
|
+
@terrain = Terrain.new width, height, rand(10), color_palette.get("terrain")
|
34
|
+
@mouse = Mouse.new self
|
35
|
+
|
36
|
+
on Events::EntityCreated do |event|
|
37
|
+
entities << event.entity
|
38
|
+
end
|
39
|
+
|
40
|
+
on Events::GameEnding do |event|
|
41
|
+
if event.time < Time.now
|
42
|
+
setup
|
43
|
+
else
|
44
|
+
emit event
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def update delta
|
50
|
+
process_events!
|
51
|
+
|
52
|
+
radius = 50
|
53
|
+
@entities.each { |entity| entity.update delta }
|
54
|
+
@entities, @dead = *@entities.partition { |entity| terrain.fetch(entity.x, 0) < entity.y }
|
55
|
+
@dead
|
56
|
+
.select { |entity| entity.is_a? Shot }
|
57
|
+
.select { |entity| entity.x < terrain.width && entity.x > 0 }
|
58
|
+
.each { |entity| terrain.bite entity.x, radius }
|
59
|
+
.each { |entity| emit Events::EntityCreated.new(Explosion.new entity.x, entity.y) }
|
60
|
+
.select { |entity| players.any? { |player| inside_radius? entity.x - player.x, 0, radius } }
|
61
|
+
.each { emit Events::GameEnding.new(Time.now + 1) }
|
62
|
+
end
|
63
|
+
|
64
|
+
def render graphics
|
65
|
+
graphics.set_color color_palette.get("sky")
|
66
|
+
graphics.fill_rect 0, 0, width, height
|
67
|
+
|
68
|
+
entities.each do |entity|
|
69
|
+
entity.draw graphics
|
70
|
+
end
|
71
|
+
|
72
|
+
players.each do |player|
|
73
|
+
player.draw graphics, terrain[player.x]
|
74
|
+
end
|
75
|
+
|
76
|
+
mouse.draw graphics
|
77
|
+
|
78
|
+
terrain.draw graphics
|
79
|
+
end
|
80
|
+
|
81
|
+
def emit event
|
82
|
+
events << event
|
83
|
+
end
|
84
|
+
|
85
|
+
def on klass, &block
|
86
|
+
event_listeners << [klass, block]
|
87
|
+
end
|
88
|
+
|
89
|
+
private
|
90
|
+
|
91
|
+
def process_events!
|
92
|
+
until events.empty? do
|
93
|
+
event = events.pop
|
94
|
+
|
95
|
+
event_listeners.each do |klass, block|
|
96
|
+
if event.is_a? klass
|
97
|
+
block.call event
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
@@ -0,0 +1,100 @@
|
|
1
|
+
include Java
|
2
|
+
|
3
|
+
import java.awt.event.WindowEvent
|
4
|
+
import java.awt.image.BufferStrategy
|
5
|
+
import javax.swing.JFrame
|
6
|
+
import java.awt.Canvas
|
7
|
+
import java.awt.Dimension
|
8
|
+
import javax.swing.JPanel
|
9
|
+
import java.awt.Color
|
10
|
+
|
11
|
+
require "scorched_earth/game"
|
12
|
+
require "scorched_earth/events/mouse_pressed"
|
13
|
+
require "scorched_earth/events/mouse_released"
|
14
|
+
require "scorched_earth/events/mouse_moved"
|
15
|
+
|
16
|
+
module ScorchedEarth
|
17
|
+
class GameWindow
|
18
|
+
attr_reader :buffer_strategy, :canvas, :game
|
19
|
+
|
20
|
+
def initialize width, height
|
21
|
+
@container = JFrame.new
|
22
|
+
@canvas = Canvas.new
|
23
|
+
@panel = @container.get_content_pane
|
24
|
+
|
25
|
+
@container.setDefaultCloseOperation JFrame::EXIT_ON_CLOSE
|
26
|
+
|
27
|
+
@panel.set_preferred_size Dimension.new width, height
|
28
|
+
@panel.set_layout nil
|
29
|
+
@panel.add @canvas
|
30
|
+
|
31
|
+
@container.pack
|
32
|
+
@container.set_resizable false
|
33
|
+
@container.set_visible true
|
34
|
+
|
35
|
+
@canvas.set_bounds 0, 0, width, height
|
36
|
+
|
37
|
+
@canvas.add_mouse_listener self
|
38
|
+
|
39
|
+
@canvas.set_ignore_repaint true
|
40
|
+
@canvas.create_buffer_strategy 2
|
41
|
+
@buffer_strategy = @canvas.get_buffer_strategy
|
42
|
+
|
43
|
+
@game = ScorchedEarth::Game.new canvas.width, canvas.height
|
44
|
+
end
|
45
|
+
|
46
|
+
def run
|
47
|
+
last_time = Time.now
|
48
|
+
|
49
|
+
game.setup
|
50
|
+
|
51
|
+
loop do
|
52
|
+
delta = Time.now - last_time
|
53
|
+
last_time = Time.now
|
54
|
+
graphics = buffer_strategy.get_draw_graphics
|
55
|
+
|
56
|
+
graphics.set_color Color.black
|
57
|
+
graphics.fill_rect 0, 0, canvas.width, canvas.height
|
58
|
+
|
59
|
+
game.update delta
|
60
|
+
game.render graphics
|
61
|
+
|
62
|
+
graphics.dispose
|
63
|
+
buffer_strategy.show
|
64
|
+
|
65
|
+
sleep 1.0/60
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
private
|
70
|
+
|
71
|
+
def mouse_moved event
|
72
|
+
x = event.point.x
|
73
|
+
y = event.point.y
|
74
|
+
|
75
|
+
Events::MouseMoved.new x, y
|
76
|
+
end
|
77
|
+
|
78
|
+
def mouse_dragged *args; end
|
79
|
+
|
80
|
+
def mouse_pressed event
|
81
|
+
x = event.point.x
|
82
|
+
y = event.point.y
|
83
|
+
|
84
|
+
game.emit Events::MousePressed.new x, y
|
85
|
+
end
|
86
|
+
|
87
|
+
def mouse_released event
|
88
|
+
x = event.point.x
|
89
|
+
y = event.point.y
|
90
|
+
|
91
|
+
game.emit Events::MouseReleased.new x, y
|
92
|
+
end
|
93
|
+
|
94
|
+
def mouse_clicked *args; end
|
95
|
+
|
96
|
+
def mouse_entered *args; end
|
97
|
+
|
98
|
+
def mouse_exited *args; end
|
99
|
+
end
|
100
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module ScorchedEarth
|
2
|
+
module Helpers
|
3
|
+
def normalize_degrees(degrees)
|
4
|
+
(degrees + 360 * 2e10) % 360
|
5
|
+
end
|
6
|
+
|
7
|
+
def angle(x, y)
|
8
|
+
normalize_degrees radians_to_degrees Math.atan2(y, x)
|
9
|
+
end
|
10
|
+
|
11
|
+
def radians_to_degrees(radians)
|
12
|
+
radians * (180 / Math::PI)
|
13
|
+
end
|
14
|
+
|
15
|
+
def degrees_to_radians(degrees)
|
16
|
+
degrees / 180 * Math::PI
|
17
|
+
end
|
18
|
+
|
19
|
+
def offset_x(degrees, radius)
|
20
|
+
Math.sin(degrees_to_radians(degrees)) * radius
|
21
|
+
end
|
22
|
+
|
23
|
+
def offset_y(degrees, radius)
|
24
|
+
- Math.cos(degrees_to_radians(degrees)) * radius
|
25
|
+
end
|
26
|
+
|
27
|
+
def circle(radius)
|
28
|
+
return enum_for(:circle, radius) unless block_given?
|
29
|
+
|
30
|
+
x = -radius
|
31
|
+
|
32
|
+
loop do
|
33
|
+
break unless x <= radius
|
34
|
+
y = Math.sqrt radius**2 - x**2
|
35
|
+
yield x, y
|
36
|
+
x += 1
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def inside_radius?(x, y, radius)
|
41
|
+
x ** 2 + y ** 2 < radius ** 2
|
42
|
+
end
|
43
|
+
|
44
|
+
module_function :angle, :circle, :inside_radius?, :radians_to_degrees, :degrees_to_radians, :normalize_degrees, :offset_x, :offset_y
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
require "scorched_earth/helpers"
|
2
|
+
require "scorched_earth/events/mouse_pressed"
|
3
|
+
require "scorched_earth/events/mouse_released"
|
4
|
+
require "scorched_earth/events/mouse_moved"
|
5
|
+
|
6
|
+
module ScorchedEarth
|
7
|
+
class Mouse
|
8
|
+
include Helpers
|
9
|
+
|
10
|
+
attr_reader :game, :mouse_pressed_at, :x, :y
|
11
|
+
|
12
|
+
def initialize game
|
13
|
+
@game = game
|
14
|
+
|
15
|
+
game.on Events::MousePressed, &method(:mouse_pressed)
|
16
|
+
game.on Events::MouseReleased, &method(:mouse_released)
|
17
|
+
game.on Events::MouseMoved, &method(:mouse_moved)
|
18
|
+
end
|
19
|
+
|
20
|
+
def terrain
|
21
|
+
game.terrain
|
22
|
+
end
|
23
|
+
|
24
|
+
def players
|
25
|
+
game.players
|
26
|
+
end
|
27
|
+
|
28
|
+
def current_player
|
29
|
+
players.first
|
30
|
+
end
|
31
|
+
|
32
|
+
def next_player
|
33
|
+
players.rotate!
|
34
|
+
end
|
35
|
+
|
36
|
+
def mouse_released event
|
37
|
+
return unless mouse_pressed_at
|
38
|
+
|
39
|
+
delta = (Time.now - mouse_pressed_at) * 1000 + 1000
|
40
|
+
x1, y1 = event.x, event.y
|
41
|
+
x2, y2 = current_player.x, height - terrain[current_player.x]
|
42
|
+
degrees = angle(y2 - y1, x2 - x1) + 180
|
43
|
+
velocity_x = offset_x(degrees, delta)
|
44
|
+
velocity_y = offset_y(degrees, delta)
|
45
|
+
shot = Shot.new current_player.x, terrain[current_player.x], velocity_x, velocity_y, Color.white
|
46
|
+
|
47
|
+
game.emit Events::EntityCreated.new shot
|
48
|
+
|
49
|
+
next_player
|
50
|
+
end
|
51
|
+
|
52
|
+
def mouse_pressed event
|
53
|
+
@mouse_pressed_at = Time.now
|
54
|
+
end
|
55
|
+
|
56
|
+
def mouse_moved event
|
57
|
+
@x, @y = event.x, event.y
|
58
|
+
end
|
59
|
+
|
60
|
+
def height
|
61
|
+
game.height
|
62
|
+
end
|
63
|
+
|
64
|
+
def draw(graphics)
|
65
|
+
return unless x && y
|
66
|
+
|
67
|
+
height = graphics.destination.height
|
68
|
+
x1, y1 = x, y
|
69
|
+
x2, y2 = current_player.x, height - terrain[current_player.x]
|
70
|
+
|
71
|
+
graphics.set_color player.color
|
72
|
+
graphics.draw_line x1, y1, x2, y2
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module ScorchedEarth
|
2
|
+
class Player
|
3
|
+
attr_reader :x, :color
|
4
|
+
|
5
|
+
def initialize(x, color)
|
6
|
+
@x, @color = x, color
|
7
|
+
end
|
8
|
+
|
9
|
+
def draw(graphics, y)
|
10
|
+
height = graphics.destination.height
|
11
|
+
radius = 20
|
12
|
+
|
13
|
+
graphics.set_color color
|
14
|
+
graphics.fill_oval x - radius / 2, height - y - radius / 2, radius, radius
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require "scorched_earth/helpers"
|
2
|
+
|
3
|
+
module ScorchedEarth
|
4
|
+
class Shot
|
5
|
+
include Helpers
|
6
|
+
|
7
|
+
attr_reader :x, :y, :velocity_x, :velocity_y, :color
|
8
|
+
|
9
|
+
def initialize x, y, velocity_x, velocity_y, color
|
10
|
+
@x, @y, @velocity_x, @velocity_y, @color = x, y, velocity_x, velocity_y, color
|
11
|
+
end
|
12
|
+
|
13
|
+
def update delta
|
14
|
+
@velocity_y -= gravity * delta
|
15
|
+
@x += velocity_x * delta
|
16
|
+
@y += velocity_y * delta
|
17
|
+
end
|
18
|
+
|
19
|
+
def draw graphics
|
20
|
+
height = graphics.destination.height
|
21
|
+
degrees = angle(velocity_y, velocity_x)
|
22
|
+
x1, y1 = x, height - y
|
23
|
+
x2, y2 = x1 + offset_x(degrees, length), y1 + offset_y(degrees, length)
|
24
|
+
|
25
|
+
graphics.set_color color
|
26
|
+
graphics.draw_line x1, y1, x2, y2
|
27
|
+
end
|
28
|
+
|
29
|
+
def length
|
30
|
+
10
|
31
|
+
end
|
32
|
+
|
33
|
+
def width
|
34
|
+
3
|
35
|
+
end
|
36
|
+
|
37
|
+
def gravity
|
38
|
+
2000
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require "scorched_earth/helpers"
|
2
|
+
|
3
|
+
include Java
|
4
|
+
|
5
|
+
import java.awt.Transparency
|
6
|
+
|
7
|
+
module ScorchedEarth
|
8
|
+
class Terrain < Array
|
9
|
+
include Helpers
|
10
|
+
|
11
|
+
attr_reader :color, :height
|
12
|
+
|
13
|
+
def initialize(width, height, cycles, color)
|
14
|
+
@color, @height = color, height
|
15
|
+
|
16
|
+
super(width) do |index|
|
17
|
+
Math.sin(index.to_f / width * cycles) * height / 4 + (height / 4)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def width
|
22
|
+
size
|
23
|
+
end
|
24
|
+
|
25
|
+
def draw(graphics)
|
26
|
+
cached_image = cache do
|
27
|
+
image = graphics.get_device_configuration.create_compatible_image width, height, Transparency.TRANSLUCENT
|
28
|
+
|
29
|
+
each_with_index do |y, x|
|
30
|
+
image.graphics.tap do |graphics|
|
31
|
+
graphics.set_color color
|
32
|
+
graphics.draw_line x, height - y, x, height
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
image
|
37
|
+
end
|
38
|
+
|
39
|
+
graphics.draw_image cached_image, 0, 0, nil
|
40
|
+
end
|
41
|
+
|
42
|
+
def bite(center_x, radius)
|
43
|
+
center_y = self[center_x]
|
44
|
+
|
45
|
+
circle(radius) do |offset_x, offset_y|
|
46
|
+
x = center_x + offset_x
|
47
|
+
y = self[x]
|
48
|
+
next unless y
|
49
|
+
z = offset_y * 2
|
50
|
+
q = center_y - offset_y
|
51
|
+
q = y if q > y
|
52
|
+
|
53
|
+
self[x] = [y - z, q, 0].max
|
54
|
+
end
|
55
|
+
|
56
|
+
clear_cache!
|
57
|
+
end
|
58
|
+
|
59
|
+
private
|
60
|
+
|
61
|
+
def cache
|
62
|
+
@cache ||= begin
|
63
|
+
yield
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def clear_cache!
|
68
|
+
@cache = nil
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'scorched_earth/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "scorched_earth"
|
8
|
+
spec.version = ScorchedEarth::VERSION
|
9
|
+
spec.authors = ["James Moriarty"]
|
10
|
+
spec.email = ["jamespaulmoriarty@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{A simple clone of the classic game ScorchedEarth Earth.}
|
13
|
+
spec.homepage = "https://github.com/jamesmoriarty/scorched-earth"
|
14
|
+
spec.license = "MIT"
|
15
|
+
spec.platform = "java"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "pry"
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.12"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
26
|
+
spec.add_development_dependency "simplecov"
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,141 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: scorched_earth
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 5.0.0.pre
|
5
|
+
platform: java
|
6
|
+
authors:
|
7
|
+
- James Moriarty
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-12-07 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: pry
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.12'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.12'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: minitest
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '5.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '5.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: simplecov
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description:
|
84
|
+
email:
|
85
|
+
- jamespaulmoriarty@gmail.com
|
86
|
+
executables:
|
87
|
+
- scorched
|
88
|
+
extensions: []
|
89
|
+
extra_rdoc_files: []
|
90
|
+
files:
|
91
|
+
- .gitignore
|
92
|
+
- .travis.yml
|
93
|
+
- Gemfile
|
94
|
+
- LICENSE.txt
|
95
|
+
- README.md
|
96
|
+
- Rakefile
|
97
|
+
- bin/console
|
98
|
+
- bin/setup
|
99
|
+
- exe/scorched
|
100
|
+
- lib/scorched_earth.rb
|
101
|
+
- lib/scorched_earth/color_palette.rb
|
102
|
+
- lib/scorched_earth/events/entity_created.rb
|
103
|
+
- lib/scorched_earth/events/game_ending.rb
|
104
|
+
- lib/scorched_earth/events/mouse_moved.rb
|
105
|
+
- lib/scorched_earth/events/mouse_pressed.rb
|
106
|
+
- lib/scorched_earth/events/mouse_released.rb
|
107
|
+
- lib/scorched_earth/explosion.rb
|
108
|
+
- lib/scorched_earth/game.rb
|
109
|
+
- lib/scorched_earth/game_window.rb
|
110
|
+
- lib/scorched_earth/helpers.rb
|
111
|
+
- lib/scorched_earth/mouse.rb
|
112
|
+
- lib/scorched_earth/player.rb
|
113
|
+
- lib/scorched_earth/shot.rb
|
114
|
+
- lib/scorched_earth/terrain.rb
|
115
|
+
- lib/scorched_earth/version.rb
|
116
|
+
- scorched_earth.gemspec
|
117
|
+
homepage: https://github.com/jamesmoriarty/scorched-earth
|
118
|
+
licenses:
|
119
|
+
- MIT
|
120
|
+
metadata: {}
|
121
|
+
post_install_message:
|
122
|
+
rdoc_options: []
|
123
|
+
require_paths:
|
124
|
+
- lib
|
125
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
126
|
+
requirements:
|
127
|
+
- - '>='
|
128
|
+
- !ruby/object:Gem::Version
|
129
|
+
version: '0'
|
130
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
131
|
+
requirements:
|
132
|
+
- - '>'
|
133
|
+
- !ruby/object:Gem::Version
|
134
|
+
version: 1.3.1
|
135
|
+
requirements: []
|
136
|
+
rubyforge_project:
|
137
|
+
rubygems_version: 2.0.14.1
|
138
|
+
signing_key:
|
139
|
+
specification_version: 4
|
140
|
+
summary: A simple clone of the classic game ScorchedEarth Earth.
|
141
|
+
test_files: []
|