Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Adding Shot

  • Loading branch information...
commit 95e9533df77eec708a04520b4bcc07c24ca6f674 1 parent f5319e5
Thomas R. Koll authored December 12, 2010
14  Enemy.rb
@@ -15,7 +15,7 @@ class Enemy
15 15
   include Visibility
16 16
 
17 17
   
18  
-  attr_accessor :team, :view_angle
  18
+  attr_accessor :team, :view_angle, :shots
19 19
 
20 20
   def initialize(team)
21 21
     self.team = team
@@ -24,6 +24,7 @@ def initialize(team)
24 24
     self.orientation = rand(360)
25 25
     self.speed = 2
26 26
     self.turns = []
  27
+    self.shots = []
27 28
     self.view_width = 40
28 29
     self.view_angle = [self.orientation + view_width / 2.0, self.orientation - view_width / 2.0]
29 30
   end
@@ -35,8 +36,8 @@ def redraw(tick)
35 36
     colour
36 37
     Plane.draw
37 38
     self.draw
38  
-
39 39
     glPopMatrix
  40
+    self.shots = self.shots.map(&:redraw).compact
40 41
   end
41 42
   
42 43
   def colour
@@ -103,11 +104,16 @@ def circle(radius, segments = 20)
103 104
 
104 105
   def find_target(enemies)
105 106
     self.speedUp(rand * 2 - 1)
  107
+    # right
106 108
     enemy_to_follow = self.objects_in_view(enemies).first
107 109
     if enemy_to_follow.nil?
108  
-      self.turn(rand(self.turns_sum||1) * (rand -0.5))
  110
+      self.turn(rand(self.turns_sum||1) * (rand - 0.5))
109 111
     else
110  
-    a = - [[(self.orientation - enemy_to_follow[1]) / self.view_width.to_f, 0.5].min, -0.5].max
  112
+      if self.shots.size < 20
  113
+        puts enemy_to_follow[1]
  114
+        self.shots << Shot.new(self.x, self.y, (self.orientation - enemy_to_follow[1]) % 360)
  115
+      end
  116
+      a = - [[(self.orientation - enemy_to_follow[1]) / self.view_width.to_f, 0.5].min, -0.5].max
111 117
       self.turn(a)
112 118
     end
113 119
   end
7  GameLoop.rb
@@ -7,7 +7,8 @@
7 7
 #
8 8
 
9 9
 class GameLoop
10  
-  attr_accessor :view, :timer, :player, :elements, :scene, :teams, :stop
  10
+  attr_accessor :view
  11
+  attr_accessor :timer, :player, :elements, :scene, :teams, :stop, :shots
11 12
 
12 13
   def initialize(view)
13 14
     self.view = view
@@ -63,5 +64,9 @@ def set_viewport_rectangle(bounds)
63 64
 		glMatrixMode(GL_PROJECTION)
64 65
 		glLoadIdentity
65 66
 		gluPerspective(30, bounds.size.width / bounds.size.height, 1.0, 1000.0)
  67
+
  68
+#		glMatrixMode(GL_MODELVIEW)
  69
+#		glLoadIdentity
  70
+
66 71
 	end
67 72
 end
8  Movable.rb
@@ -43,9 +43,11 @@ def move(turn = nil)
43 43
     glRotatef(self.orientation, 0, 0, 1)
44 44
 
45 45
     # Move x and y coords
46  
-    self.turn(turn || self.last_turn || 0)
47  
-    self.turns.shift if turn.nil?
48  
-    self.turns.unshift(0)
  46
+    if self.turns
  47
+      self.turn(turn || self.last_turn || 0)
  48
+      self.turns.shift if turn.nil?
  49
+      self.turns.unshift(0)
  50
+    end
49 51
     self.x += Math.cos(self.radians(self.orientation)) * self.speed * SPEED_FACTOR
50 52
     self.y += Math.sin(self.radians(self.orientation)) * self.speed * SPEED_FACTOR
51 53
   end
44  Shot.rb
... ...
@@ -0,0 +1,44 @@
  1
+#
  2
+#  Shot.rb
  3
+#  The Flying Camera
  4
+#
  5
+#  Created by Thomas R. Koll on 12.12.10.
  6
+#  Copyright (c) 2010 ananasblau. All rights reserved.
  7
+#
  8
+
  9
+class Shot
  10
+  include Movable
  11
+  include Placable
  12
+
  13
+  attr_accessor :x, :y, :orientation
  14
+  
  15
+
  16
+  def initialize(x,y,orientation)
  17
+    self.x = x
  18
+    self.y = y
  19
+    self.z = 0
  20
+    self.speed = 10
  21
+    self.orientation = orientation
  22
+  end
  23
+  
  24
+  def redraw
  25
+    glPushMatrix
  26
+  
  27
+    # shot is leaving view field
  28
+    out_of_view = self.x > 1 || self.x < -1 || self.y > 1 || self.y < -1
  29
+
  30
+    place
  31
+    self.move(0)
  32
+    glColor3f(1,1,1)
  33
+    glBegin(GL_QUADS)
  34
+      glVertex3f( 0.0,   0.0, 0.0); 
  35
+      glVertex3f( 0.0,   0.002, 0.0); 
  36
+      glVertex3f(-0.002, 0.002, 0.0); 
  37
+      glVertex3f(-0.002, 0.0, 0.0); 
  38
+      glVertex3f( 0.0,   0.0, 0.0); 
  39
+    glEnd
  40
+    glPopMatrix
  41
+   return nil if out_of_view
  42
+   return self
  43
+  end
  44
+end
4  The Flying Camera.xcodeproj/project.pbxproj
@@ -49,6 +49,7 @@
49 49
 		0EB704E312B39CC2006FF01B /* Visibility.rb in Resources */ = {isa = PBXBuildFile; fileRef = 0EB704E212B39CC2006FF01B /* Visibility.rb */; };
50 50
 		0EB706C812B3B10C006FF01B /* README in Resources */ = {isa = PBXBuildFile; fileRef = 0EB706C712B3B10C006FF01B /* README */; };
51 51
 		0ED0536812B535330095EAC6 /* Score.rb in Resources */ = {isa = PBXBuildFile; fileRef = 0ED0536712B535330095EAC6 /* Score.rb */; };
  52
+		0ED4F78D12B557E60064E48A /* Shot.rb in Resources */ = {isa = PBXBuildFile; fileRef = 0ED4F78C12B557E60064E48A /* Shot.rb */; };
52 53
 		0ED8FB4212B24A0700296022 /* Movable.rb in Resources */ = {isa = PBXBuildFile; fileRef = 0ED8FB4112B24A0700296022 /* Movable.rb */; };
53 54
 		0ED8FB9612B24C6100296022 /* Plane.rb in Resources */ = {isa = PBXBuildFile; fileRef = 0ED8FB9512B24C6100296022 /* Plane.rb */; };
54 55
 		0ED8FBAD12B24FAF00296022 /* Enemy.rb in Resources */ = {isa = PBXBuildFile; fileRef = 0ED8FBAC12B24FAF00296022 /* Enemy.rb */; };
@@ -87,6 +88,7 @@
87 88
 		0EB704E212B39CC2006FF01B /* Visibility.rb */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.ruby; path = Visibility.rb; sourceTree = "<group>"; };
88 89
 		0EB706C712B3B10C006FF01B /* README */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = README; sourceTree = "<group>"; };
89 90
 		0ED0536712B535330095EAC6 /* Score.rb */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.ruby; path = Score.rb; sourceTree = "<group>"; };
  91
+		0ED4F78C12B557E60064E48A /* Shot.rb */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.ruby; path = Shot.rb; sourceTree = "<group>"; };
90 92
 		0ED8FB4112B24A0700296022 /* Movable.rb */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.ruby; path = Movable.rb; sourceTree = "<group>"; };
91 93
 		0ED8FB9512B24C6100296022 /* Plane.rb */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.ruby; path = Plane.rb; sourceTree = "<group>"; };
92 94
 		0ED8FBAC12B24FAF00296022 /* Enemy.rb */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.ruby; path = Enemy.rb; sourceTree = "<group>"; };
@@ -159,6 +161,7 @@
159 161
 				0EF05DB512B17440009C18E8 /* Placable.rb */,
160 162
 				0ED8FB9512B24C6100296022 /* Plane.rb */,
161 163
 				0EB704E212B39CC2006FF01B /* Visibility.rb */,
  164
+				0ED4F78C12B557E60064E48A /* Shot.rb */,
162 165
 			);
163 166
 			name = lib;
164 167
 			sourceTree = "<group>";
@@ -316,6 +319,7 @@
316 319
 				0EB706C812B3B10C006FF01B /* README in Resources */,
317 320
 				0E319B1312B3D2AC00D0E514 /* Fullscreen.rb in Resources */,
318 321
 				0ED0536812B535330095EAC6 /* Score.rb in Resources */,
  322
+				0ED4F78D12B557E60064E48A /* Shot.rb in Resources */,
319 323
 			);
320 324
 			runOnlyForDeploymentPostprocessing = 0;
321 325
 		};

0 notes on commit 95e9533

Please sign in to comment.
Something went wrong with that request. Please try again.