pugnacious_juices 1.0.1 → 1.0.2

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.
@@ -1,4 +1,4 @@
1
1
  #!/usr/bin/env ruby
2
2
  require_relative '../lib/pugnacious'
3
3
 
4
- Pugnacious::Application.run
4
+ Pugnacious::Application.run(ARGV)
@@ -1,15 +1,17 @@
1
1
  module Pugnacious
2
2
  MAP_SIZE = 500
3
- MOLECULE_SIZE = 5
3
+ MOLECULE_SIZE = 3
4
4
 
5
- class Application
6
- def self.run
5
+ class Application
6
+ attr_accessor :number_of_molecules
7
+ def self.run(argv)
7
8
  Ray.game "Pugnacious Juices", :size => [MAP_SIZE, MAP_SIZE] do
8
- frames_per_second = 16
9
9
  register { add_hook :quit, method(:exit!) }
10
+
11
+ number_of_molecules = argv[0].to_i | 250
10
12
 
11
13
  FightScene.bind(self)
12
- scenes << :fight_scene
14
+ push_scene(:fight_scene, number_of_molecules)
13
15
  end
14
16
  end
15
17
  end
@@ -2,53 +2,22 @@ module Pugnacious
2
2
  class FightScene < Ray::Scene
3
3
  scene_name :fight_scene
4
4
 
5
- def setup
6
- @gc_counter = 0
7
- #self.frames_per_second = 5
5
+ def setup(number_of_molecules)
8
6
  @player1 = Player.new(
9
7
  color: Ray::Color.blue,
10
8
  position: [0, 0],
11
- control_keys: [:up, :right, :down, :left])
9
+ control_keys: {:up => :up, :right => :right, :down => :down, :left => :left})
12
10
 
13
11
  @player2 = Player.new(
14
12
  color: Ray::Color.red,
15
13
  position: [0, 0],
16
- control_keys: [:w, :d, :s, :a])
14
+ control_keys: {:up => :w, :right => :d, :down => :s, :left => :a})
17
15
 
18
- @player2.pointer.pos = [300, 300]
19
- @player1.pointer.pos = [400, 300]
16
+ @player2.pointer.pos = [50, 300]
17
+ @player1.pointer.pos = [450, 300]
20
18
 
21
19
  @game_map = GameMap.generate_empty_map(MAP_SIZE, MAP_SIZE)
22
-
23
- @molecules = []
24
-
25
- @game_map.size.times do |i|
26
- @molecules << Molecule.new(
27
- :player => @player1,
28
- :rival => @player2,
29
- :molecules => @molecules,
30
- :pos => [i,10],
31
- :map => @game_map)
32
- @molecules << Molecule.new(
33
- :player => @player1,
34
- :rival => @player2,
35
- :molecules => @molecules,
36
- :pos => [i,11],
37
- :map => @game_map)
38
-
39
- @molecules << Molecule.new(
40
- :player => @player2,
41
- :rival => @player1,
42
- :molecules => @molecules,
43
- :pos => [i,72],
44
- :map => @game_map)
45
- @molecules << Molecule.new(
46
- :player => @player2,
47
- :rival => @player1,
48
- :molecules => @molecules,
49
- :pos => [i,75],
50
- :map => @game_map)
51
- end
20
+ @molecules = GameMap.generate_molecules(@game_map, @player1, @player2, number_of_molecules)
52
21
 
53
22
  @players = [@player1, @player2]
54
23
  end
@@ -56,18 +25,22 @@ module Pugnacious
56
25
  def register
57
26
  always do
58
27
  @players.each do |player|
59
- player.control_keys.each do |direction|
28
+ player.control_keys.values.each do |direction|
60
29
  if holding? direction then player.move direction end
61
30
  end
62
31
  end
63
32
 
64
- @molecules.each &:move
33
+ @molecules.each &:move
34
+ if @molecules.all?{|m| m.player == @player1} or @molecules.all?{|m| m.player == @player2}
35
+ @winner = text("You are the winner!!", :at => [200, 200], :size => 30)
36
+ end
65
37
  end
66
38
  end
67
39
 
68
40
  def render(window)
69
41
  @molecules.each { |molecule| window.draw molecule.body}
70
42
  @players.each { |player| window.draw player.pointer }
43
+ window.draw(@winner) unless @winner.nil?
71
44
  end
72
45
 
73
46
  def clean_up
@@ -78,6 +51,6 @@ module Pugnacious
78
51
  @players = nil
79
52
 
80
53
  Ray::ImageSet.clear
81
- end
54
+ end
82
55
  end
83
56
  end
@@ -1,21 +1,49 @@
1
1
  module Pugnacious
2
2
  class GameMap
3
- attr_accessor :map
4
-
5
- def initialize(number_of_lines, number_of_columns)
6
- generate_empty_map(number_of_lines, number_of_columns)
7
- end
8
3
 
9
4
  def self.generate_empty_map(number_of_lines, number_of_columns)
10
5
  game_map = []
11
6
  (number_of_columns/MOLECULE_SIZE).times do |l|
12
7
  game_map << []
13
8
  (number_of_lines/MOLECULE_SIZE).times do |c|
14
- game_map[l][c] = :empty
9
+ game_map[l][c] = [l, c]
15
10
  end
16
11
  end
17
12
  game_map
18
13
  end
19
14
 
15
+ def self.generate_molecules(map, player1, player2, number)
16
+ molecules = []
17
+
18
+ positions = map.flatten(1).sample(number)
19
+
20
+ positions[0..(number/2)].each do |p|
21
+ molecules << Molecule.new(
22
+ :player => player1,
23
+ :rival => player2,
24
+ :molecules => molecules,
25
+ :pos => p,
26
+ :map => map)
27
+ end
28
+
29
+ positions[(number/2)..number].each do |p|
30
+ molecules << Molecule.new(
31
+ :player => player2,
32
+ :rival => player1,
33
+ :molecules => molecules,
34
+ :pos => p,
35
+ :map => map)
36
+ end
37
+
38
+ map.each do |l|
39
+ l.each_with_index do |c, index|
40
+ if c.kind_of?(Array)
41
+ l[index] = :empty
42
+ end
43
+ end
44
+ end
45
+
46
+ molecules
47
+ end
20
48
  end
21
49
  end
@@ -4,7 +4,7 @@ module Pugnacious
4
4
 
5
5
  attr_accessor :player, :life, :body, :molecules, :pointer, :position, :game_map, :life, :rival
6
6
 
7
- MAX_LIFE = 40
7
+ MAX_LIFE = 30
8
8
 
9
9
  def initialize(options = {})
10
10
  @player = options[:player]
@@ -21,9 +21,9 @@ module Pugnacious
21
21
  end
22
22
 
23
23
  def receive_damage()
24
- @life -=1
24
+ @life -=3
25
25
  if @life <= 0
26
- @player = @rival
26
+ @rival, @player = @player, @rival
27
27
  @body.color = Ray::Color.new(100, 100, 0) + @player.color
28
28
  @life = MAX_LIFE
29
29
  @pointer = @player.pointer
@@ -11,42 +11,76 @@ module Pugnacious
11
11
  :north_west => [-1, -1]}
12
12
 
13
13
  def move
14
- try_to_go(where_is_the_pointer?) unless where_is_the_pointer? == :here
14
+ unless pointer_direction() == :here
15
+ posible_directions = posible_directions(pointer_direction())
16
+
17
+ posible_directions.each do |direction|
18
+ case check_tile(direction)
19
+ when :empty
20
+ move!(direction)
21
+ return
22
+ when :enemy
23
+ fight(direction)
24
+ return
25
+ end
26
+ end
27
+ end
28
+ end
29
+
30
+ def fight(direction)
31
+ xw, yw = DIRECTIONS[direction]
32
+
33
+ x = xw + @position[0]
34
+ y = yw + @position[1]
35
+
36
+ tile = @game_map[x][y]
37
+
38
+ tile.receive_damage
15
39
  end
16
40
 
17
- def try_to_go(direction)
18
- directions = DIRECTIONS.keys
19
- intention_index = directions.find_index(direction)
20
- directions.push(:north)
21
-
22
- if can_i_move_there?(direction)
23
- move_there(direction)
24
- elsif can_i_move_there?(directions[intention_index+1])
25
- move_there(directions[intention_index+1])
26
- elsif can_i_move_there?(directions[intention_index-1])
27
- move_there(directions[intention_index-1])
41
+ def posible_directions(direction)
42
+ directions = []
43
+ index = DIRECTIONS.keys.find_index(direction)
44
+ indexes = []
45
+
46
+ # The order is important.
47
+ indexes << index
48
+ indexes << index - 1
49
+ indexes << index + 1
50
+ indexes << index - 2
51
+ indexes << index + 2
52
+
53
+ directions = indexes.map do |i|
54
+ if i>7
55
+ DIRECTIONS.keys[i-7]
56
+ else
57
+ DIRECTIONS.keys[i]
58
+ end
28
59
  end
60
+
61
+ directions
29
62
  end
30
63
 
31
- def can_i_move_there?(direction)
64
+ def check_tile(direction)
32
65
  xw, yw = DIRECTIONS[direction]
33
66
 
34
67
  x = xw + @position[0]
35
68
  y = yw + @position[1]
36
69
 
37
- if @game_map[x][y] == :empty
38
- return true
39
- elsif @game_map[x][y].class == Molecule
40
- if @game_map[x][y].player != self.player
41
- @game_map[x][y].receive_damage()
42
- end
43
- return false
44
- else
45
- return false
70
+ tile = @game_map[x][y]
71
+
72
+ if tile == :empty
73
+ return :empty
74
+ elsif tile.class == Molecule
75
+ if tile.player != self.player
76
+ return :enemy
77
+ else
78
+ return :friend
79
+ end
46
80
  end
47
81
  end
48
82
 
49
- def move_there(direction)
83
+ def move!(direction)
50
84
  xw, yw = DIRECTIONS[direction]
51
85
 
52
86
  x = xw + @position[0]
@@ -59,51 +93,62 @@ module Pugnacious
59
93
  @position = [x, y]
60
94
  end
61
95
 
62
- def where_is_the_pointer?
63
- if pointer_at_south then return :south end
64
- if pointer_at_north then return :north end
65
- if pointer_at_east then return :east end
66
- if pointer_at_west then return :west end
67
- if pointer_at_south_east then return :south_east end
68
- if pointer_at_north_east then return :north_east end
69
- if pointer_at_south_west then return :south_west end
70
- if pointer_at_north_west then return :north_west end
71
- if here then return :here end
96
+ def pointer_direction()
97
+ case
98
+ when pointer_at_south
99
+ :south
100
+ when pointer_at_north
101
+ :north
102
+ when pointer_at_east
103
+ :east
104
+ when pointer_at_west
105
+ :west
106
+ when pointer_at_south_east
107
+ :south_east
108
+ when pointer_at_north_east
109
+ :north_east
110
+ when pointer_at_south_west
111
+ :south_west
112
+ when pointer_at_north_west
113
+ :north_west
114
+ when here
115
+ :here
116
+ end
72
117
  end
73
118
 
74
- def pointer_at_south
119
+ def pointer_at_south()
75
120
  @pointer.x == body.x and @pointer.y > body.y
76
121
  end
77
122
 
78
- def pointer_at_north
123
+ def pointer_at_north()
79
124
  @pointer.x == body.x and @pointer.y < body.y
80
125
  end
81
126
 
82
- def pointer_at_east
127
+ def pointer_at_east()
83
128
  @pointer.x > body.x and @pointer.y == body.y
84
129
  end
85
130
 
86
- def pointer_at_west
131
+ def pointer_at_west()
87
132
  @pointer.x < body.x and @pointer.y == body.y
88
133
  end
89
134
 
90
- def pointer_at_south_east
135
+ def pointer_at_south_east()
91
136
  @pointer.x > body.x and @pointer.y > body.y
92
137
  end
93
138
 
94
- def pointer_at_north_east
139
+ def pointer_at_north_east()
95
140
  @pointer.x > body.x and @pointer.y < body.y
96
141
  end
97
142
 
98
- def pointer_at_south_west
143
+ def pointer_at_south_west()
99
144
  @pointer.x < body.x and @pointer.y > body.y
100
145
  end
101
146
 
102
- def pointer_at_north_west
147
+ def pointer_at_north_west()
103
148
  @pointer.x < body.x and @pointer.y < body.y
104
149
  end
105
150
 
106
- def here
151
+ def here()
107
152
  @pointer.x == body.x and @pointer.y == body.y
108
153
  end
109
154
  end
@@ -10,7 +10,6 @@ module Pugnacious
10
10
  position = options[:position] || [200, 200]
11
11
 
12
12
  @control_keys = options[:control_keys]
13
- @control_keys ||= [:up, :right, :down, :left]
14
13
 
15
14
  @pointer = Ray::Polygon.circle(position, POINTER_SIZE, @color , 3, @color)
16
15
  @pointer.filled = false
@@ -18,36 +17,15 @@ module Pugnacious
18
17
 
19
18
  def move(direction)
20
19
  case direction
21
- when control_keys_up
20
+ when control_keys[:up]
22
21
  pointer.y -= SPEED unless (pointer.pos.y -= SPEED) < 0 + POINTER_SIZE
23
- when control_keys_right
22
+ when control_keys[:right]
24
23
  pointer.x += SPEED unless (pointer.pos.x += SPEED) > MAP_SIZE - POINTER_SIZE
25
- when control_keys_down
24
+ when control_keys[:down]
26
25
  pointer.y += SPEED unless (pointer.pos.y += SPEED) > MAP_SIZE - POINTER_SIZE
27
- when control_keys_left
26
+ when control_keys[:left]
28
27
  pointer.x -= SPEED unless (pointer.pos.x -= SPEED) < 0 + POINTER_SIZE
29
28
  end
30
29
  end
31
-
32
- ########################
33
- # Control keys aliases
34
- #######################
35
-
36
- def control_keys_up
37
- control_keys[0]
38
- end
39
-
40
- def control_keys_right
41
- control_keys[1]
42
- end
43
-
44
- def control_keys_down
45
- control_keys[2]
46
- end
47
-
48
- def control_keys_left
49
- control_keys[3]
50
- end
51
-
52
30
  end
53
31
  end
@@ -1,3 +1,3 @@
1
1
  module Pugnacious
2
- VERSION = '1.0.1'
2
+ VERSION = '1.0.2'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pugnacious_juices
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2011-09-14 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: ray
16
- requirement: &70118925055960 !ruby/object:Gem::Requirement
16
+ requirement: &70135957685520 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,11 +21,11 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70118925055960
24
+ version_requirements: *70135957685520
25
25
  description: It is a clone of Liquid Wars.
26
26
  email: siotopo@gmail.com
27
27
  executables:
28
- - pugnacious.rb
28
+ - pugnacious
29
29
  extensions: []
30
30
  extra_rdoc_files: []
31
31
  files:
@@ -37,7 +37,7 @@ files:
37
37
  - lib/pugnacious/player.rb
38
38
  - lib/pugnacious/version.rb
39
39
  - lib/pugnacious.rb
40
- - bin/pugnacious.rb
40
+ - bin/pugnacious
41
41
  homepage: https://github.com/Nerian/Pugnacious-Juices
42
42
  licenses: []
43
43
  post_install_message: