gembots 0.0.1 → 0.0.02

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e04845b98f6b322b989127ae52b9950cf0195822
4
- data.tar.gz: 2f8d1107d1f1d3bbe4943a58fe0062fa1b7a10a0
3
+ metadata.gz: 573baa7dd49ebfa9c61459cb561216065d5eff8b
4
+ data.tar.gz: 0b506e01537918e8d553a4a37538139db3286a1f
5
5
  SHA512:
6
- metadata.gz: 8e6a933bb920b8956d6aea6f96c58d9319c6956d5e5e4aac41b71cca3f5ca3bdd32447f9a52d3ba5f66a5085920d297ad4d8abf9106c4f4149f613391f4c6198
7
- data.tar.gz: 8154a3df22f26eca2e57d435d467c8008c0b79ce36f6633705b2fc2c3a9f08ba9389cdea31a18931ec23e2b3f344bd0793f67c684ca38ca8a713ee7b233e02ca
6
+ metadata.gz: 63d16728df0f12fd991d020b75e72bfa23b603fda1c81465a5d1686d1f71765d42dbf400a9b31bad040ed55e386efdd695e4b1bc08fec47cdfd7c2e011406525
7
+ data.tar.gz: 7c269dafb617699296e1ab794436e9c17e81421a86b473242058e59e353f21b5976cae9ed4622a811ef12f0d3a6da5c2a5c0be1e2050725d3e17d39c2992ea08
data/lib/gembots/arena.rb CHANGED
@@ -1,33 +1,39 @@
1
1
  require 'gembots'
2
2
 
3
- class Gembots::Arena < Gosu::Window
4
- attr_reader :bots
5
- def initialize *bots
6
- super 640, 480, false
7
- self.caption = "Gembots battle"
8
- @bots = []
3
+ # The Arena class is used to create and simulate arenas.
4
+ class Gembots::Arena
5
+ # This is a hash table containing the ids of each object.
6
+ attr_reader :objects
9
7
 
10
- bots.each do |bot_class|
11
- bot = bot_class.new self
12
- bot.warp 320, 240
13
- @bots << bot
14
- end
15
- end
8
+ # 2-dimensional array of the current arena.
9
+ # By default this is a 20x20 board
10
+ attr_reader :board
16
11
 
17
- def draw
18
- @bots.each &:draw
19
- end
12
+ def initialize *bots
13
+ @objects = Hash.new
14
+ @board = Array.new 20, (Array.new 20, [])
15
+
16
+ # define each bots' update function and add to players hash
17
+ bots.each do |bot|
18
+ bot.arena = self
19
+ @objects[bot.id] = bot
20
+ @board[bot.x_pos][bot.y_pos] << bot.id
20
21
 
21
- def update
22
- @bots.each &:update
23
- @bots.each do |bot|
24
- if bot.actions.empty?
25
- bot.on_idle
22
+ def bot.update arena
23
+ arena.update_bot self
26
24
  end
27
25
  end
28
26
  end
29
27
 
30
- def button_down id
31
- close if id == Gosu::KbEscape
28
+ # Used for activating the robot's custom functions based on active events.
29
+ def update_bot robot
30
+ # something here
31
+ end
32
+
33
+ # Spawn object into board and objects array.
34
+ # Most used for spawning projectiles.
35
+ def spawn object
36
+ @objects[object.id] = object
37
+ @board[object.x_pos][object.y_pos] << object.id
32
38
  end
33
39
  end
data/lib/gembots/bot.rb CHANGED
@@ -1,59 +1,112 @@
1
1
  require 'gembots'
2
2
 
3
+ # The Robot class is used to create and define robots.
3
4
  class Gembots::Robot
4
- attr_reader :x, :y, :angle, :actions
5
+ # String contains the robot's name.
6
+ # This will typically used for things like announcements during battles.
7
+ attr_accessor :name
5
8
 
6
- def initialize window
7
- @actions = []
8
- @window = window
9
- @images = Gosu::Image::load_tiles(window, "media/tank.png", 32, 32, false)
10
- @image = Gosu::Image.new window, "media/cannon.png", false
11
- @x = @y = @angle = @cur_image = 0.0
9
+ # X position relative to the arena.
10
+ attr_reader :x_pos
11
+
12
+ # Y position relative to the arena.
13
+ attr_reader :y_pos
14
+
15
+ # Number between 0 and 360 representing the robot's current facing angle.
16
+ attr_reader :angle
17
+
18
+ # This is simply the robot's player ID.
19
+ # It is typically the Object ID, but may be changed to fit other uses.
20
+ attr_reader :id
21
+
22
+ # This is the ID of a cloned robot's parent.
23
+ # Otherwise if the robot is not a cloned one, the value will be nil.
24
+ attr_accessor :parent_id
25
+
26
+ # This is set and used by the current robot's arena, if there is one.
27
+ attr_accessor :arena
28
+
29
+ def initialize name = 'Robot'
30
+ @name = name
31
+
32
+ @x_pos = 0
33
+ @y_pos = 0
34
+ @angle = 0
35
+
36
+ @id = self.object_id
37
+ @parent_id = nil
12
38
  end
13
39
 
14
- def warp x, y
15
- @x, @y = x, y
40
+ # Returns a duplicate robot(clone) with it's own +parent_id+.
41
+ def clone
42
+ clone = self.dup
43
+ clone.parent_id = self.id
44
+ clone
16
45
  end
17
46
 
18
- def turn angle=10
19
- @actions << [:turn, angle]
47
+ # Returns true if robot is a clone of target_robot.
48
+ def is_clone_of? target_robot
49
+ @parent_id == target_robot.id
20
50
  end
21
51
 
22
- def move dist=10
23
- @actions << [:move, dist]
52
+ # Moves the robot forward along it's angle for the distance specified.
53
+ # To move backward just use a negative value.
54
+ # Currently it only supports movement along 8 directions.
55
+ def move dist = 1
56
+ # Eventually some math using rotation_matrix will be here, in order to calculate all 360 directions.
57
+ # For now I'm only implementing 8 directions.
58
+ directions = [
59
+ [1, 0], # 0
60
+ [1, 1], # 45
61
+ [0, 1], # 90
62
+ [-1, 1], # 135
63
+ [-1, 0], # 180
64
+ [-1, -1], # 225
65
+ [0, -1], # 270
66
+ [1, -1] # 315
67
+ ]
68
+ @y_pos += dist * directions[360 / @angle - 1][0]
69
+ @x_pos += dist * directions[360 / @angle - 1][1]
70
+
71
+ self.update @arena
24
72
  end
25
73
 
26
- def update
27
- return if @actions.empty?
28
- case @actions[0][0]
29
- when :move then
30
- # I probably should implement dist = @actions[0][1]; dist %= 0.5 or something
31
- # but right now I'm tired, and worried I will screw up the math if I try that...
32
- dist = @actions[0][1] <= 0.9 ? @actions[0][1] : 1.0
33
- @x += Gosu::offset_x @angle, dist
34
- @y += Gosu::offset_y @angle, dist
35
- @x %= 640
36
- @y %= 480
74
+ # Rotates angle in degrees clockwise.
75
+ # To rotate counter-clockwise just use a negative number.
76
+ def turn angle
77
+ @angle += angle
78
+
79
+ # Used for wrapping:
80
+ @angle -= 360 if @angle > 360
81
+ @angle += 360 if @angle < 0
37
82
 
38
- @cur_image += 0.1
39
- @cur_image %= 7.0
83
+ self.update @arena
84
+ end
40
85
 
41
- @actions[0][1] -= 0.5
42
- @actions.shift if @actions[0][1] == 0.0
86
+ # Spawns projectile of type `bullet` in arena(if there is one).
87
+ def fire
88
+ arena or return
89
+ @arena.spawn Projectile.new 'bullet', @id, @x_pos, @y_pos, @angle
90
+ end
43
91
 
44
- when :turn then
45
- deg = @actions[0][1] <= 9 ? @actions[0][1] : 10
92
+ # These functions are just for documentation and to prevent erros when calling these undefined.
46
93
 
47
- @angle += deg
48
- @angle %= 360
49
- @actions[0][1] -= deg
94
+ # Called whenever the game is in idle state.
95
+ # It is likely used as the robot's main loop.
96
+ def when_idle robot
97
+ end
98
+
99
+ # Called whenever another robot is seen within the facing angle.
100
+ # It usually has some code to fire at the target robot.
101
+ # Make sure to check that it is not your robot's clone!
102
+ def when_find_robot robot, target_robot
103
+ end
50
104
 
51
- @actions.shift if @actions[0][1] == 0
52
- end
105
+ # Called whenever this robot has ended up colliding with another robot.
106
+ def when_robot_collision robot, target_robot
53
107
  end
54
108
 
55
- def draw
56
- @images[@cur_image].draw_rot @x, @y, 1, @angle - 90 % 360
57
- @image.draw_rot @x, @y, 1, @angle - 90 % 360
109
+ # This function is used by the arena for updates and delays and stuff.
110
+ def update *i
58
111
  end
59
112
  end
data/test/test_bot.rb ADDED
@@ -0,0 +1,33 @@
1
+ require 'test/unit'
2
+ require 'gembots/bot'
3
+
4
+ def make_bot
5
+ bot = Gembots::Robot.new
6
+ bot.name = "test"
7
+ bot.turn 90
8
+ bot.move 5
9
+ bot
10
+ end
11
+
12
+ class BotTest < Test::Unit::TestCase
13
+ def test_name
14
+ bot = make_bot
15
+ assert_equal "test", bot.name
16
+ end
17
+
18
+ def test_position
19
+ bot = make_bot
20
+ assert_equal 5, bot.x_pos
21
+ end
22
+
23
+ def test_clone
24
+ bot = make_bot
25
+ bot_clone = bot.clone
26
+ assert_equal bot_clone.parent_id, bot.id
27
+ end
28
+
29
+ def test_id
30
+ bot = make_bot
31
+ assert_equal bot.object_id, bot.id
32
+ end
33
+ end
metadata CHANGED
@@ -1,29 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gembots
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.02
5
5
  platform: ruby
6
6
  authors:
7
7
  - L8D
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-07-22 00:00:00.000000000 Z
11
+ date: 2013-06-20 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: gosu
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - '>='
18
- - !ruby/object:Gem::Version
19
- version: '0'
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - '>='
25
- - !ruby/object:Gem::Version
26
- version: '0'
27
13
  - !ruby/object:Gem::Dependency
28
14
  name: bundler
29
15
  requirement: !ruby/object:Gem::Requirement
@@ -61,7 +47,7 @@ extra_rdoc_files: []
61
47
  files:
62
48
  - lib/gembots/arena.rb
63
49
  - lib/gembots/bot.rb
64
- - test/test_arena.rb
50
+ - test/test_bot.rb
65
51
  homepage: http://github.com/L8D/gembots
66
52
  licenses:
67
53
  - MIT
@@ -82,9 +68,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
82
68
  version: '0'
83
69
  requirements: []
84
70
  rubyforge_project:
85
- rubygems_version: 2.0.3
71
+ rubygems_version: 2.0.2
86
72
  signing_key:
87
73
  specification_version: 4
88
74
  summary: Create your own gembots and battle them in a cli arena
89
75
  test_files:
90
- - test/test_arena.rb
76
+ - test/test_bot.rb
data/test/test_arena.rb DELETED
@@ -1,25 +0,0 @@
1
- require 'test/unit'
2
- require 'gembots'
3
- require 'gembots/arena'
4
- require 'gembots/bot'
5
-
6
- class MyBot1 < Gembots::Robot
7
- def on_idle
8
- self.move
9
- self.turn
10
- end
11
- end
12
-
13
- class MyBot2 < Gembots::Robot
14
- def on_idle
15
- self.move 50
16
- self.turn 180
17
- end
18
- end
19
-
20
- class ArenaTest < Test::Unit::TestCase
21
- def test_arena_window
22
- arena = Gembots::Arena.new MyBot1, MyBot2
23
- arena.show
24
- end
25
- end