Skip to content

Commit

Permalink
* Fixing Enemy KI
Browse files Browse the repository at this point in the history
* Adding annoying ping sound for shots
* Display score
  • Loading branch information
TomK32 committed Dec 15, 2010
1 parent 3db8ac9 commit 0b1cea0
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 22 deletions.
29 changes: 22 additions & 7 deletions Enemy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Enemy
include Visibility


attr_accessor :team, :view_angle, :shots
attr_accessor :team, :view_angle, :shots, :shots_remaining, :shooting

def initialize(team)
self.team = team
Expand All @@ -25,6 +25,7 @@ def initialize(team)
self.speed = 2
self.turns = []
self.shots = []
self.shots_remaining = 0
self.view_width = 40
self.view_angle = [self.orientation + view_width / 2.0, self.orientation - view_width / 2.0]
end
Expand All @@ -38,6 +39,7 @@ def redraw(tick)
self.draw
glPopMatrix
self.shots = self.shots.map(&:redraw).compact
self.shots_remaining = [self.shots_remaining + 1, 10].min
end

def colour
Expand Down Expand Up @@ -106,16 +108,29 @@ def circle(radius, segments = 20)
def find_target(enemies)
self.speedUp(rand * 2 - 1)
# right
enemy_to_follow = self.objects_in_view(enemies).first
enemy_to_follow = nil
self.objects_in_view(enemies).each do |e|
if enemy_to_follow.nil?
enemy_to_follow = e
else
enemy_to_follow = e if e[2] < enemy_to_follow[2]
end
end
if enemy_to_follow.nil?
self.turn(rand(self.turns_sum||1) * (rand - 0.5))
else
if self.shots.size < 20
puts enemy_to_follow[1]
self.shots << Shot.new(self.x, self.y, (self.orientation - enemy_to_follow[1]) % 360)
end
a = - [[(self.orientation - enemy_to_follow[1]) / self.view_width.to_f, 0.5].min, -0.5].max
a = - [[(self.orientation - enemy_to_follow[1]) / self.view_width.to_f, 0.2].min, -0.2].max
self.turn(a)
if (self.shots_remaining % 5 == 0)
if self.shots_remaining > 1 && (self.orientation - enemy_to_follow[1] < 10) &&
enemy_to_follow[2] < 0.4
self.shots_remaining -= 8
self.shots << Shot.new(self.x, self.y, enemy_to_follow[1] % 360)
self.shooting = true
else
self.shooting = false
end
end
end
end

Expand Down
18 changes: 14 additions & 4 deletions GameLoop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,20 @@

class GameLoop
attr_accessor :view
attr_accessor :timer, :player, :elements, :scene, :teams, :stop, :shots
attr_accessor :timer, :player, :elements, :scene, :teams, :stop, :shots, :total_time

def initialize(view)
self.view = view
self.scene = Scene.new
self.teams = [
Team.new('Kaiserliche Fliegertruppe', [0.8, 0.8, 0.8], 2),
Team.new('Royal Flying Corps', [0.6, 0.62, 0.6], 2)
Team.new('Kaiserliche Fliegertruppe', [0.8, 0.8, 0.8], 3),
Team.new('Royal Flying Corps', [0.6, 0.62, 0.6], 3)
]
self.elements ||= self.teams.collect(&:planes).flatten

self.player = Player.new
start_timer
self.total_time = Score.new(-0.95, -0.8, 0)
end

def start_timer
Expand All @@ -38,8 +39,13 @@ def timer_fired(timer)
end

def tick(seconds)
return if self.stop
scene.redraw(seconds)
self.total_time.draw
self.player.score.draw
return if self.stop

self.total_time += seconds


player.redraw(seconds)
elements.map do |e|
Expand All @@ -56,6 +62,10 @@ def stop_timer
end
end

def draw_time

end


def set_viewport_rectangle(bounds)
return
Expand Down
6 changes: 5 additions & 1 deletion GameWindowController.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@ class GameWindowController < NSWindowController
attr_accessor :view, :game_loop, :fullscreen

def awakeFromNib
NSSound.soundNamed('Ping').play
self.game_loop = GameLoop.new(self.view)
end

def keyDown(event)
key = event.characters[0].bytes.to_a[-1]
game_loop.stop = !game_loop.stop if key == 112
if key == 112
game_loop.stop = !game_loop.stop
puts game_loop.player.score.score
end
if key == 32
game_loop.stop = true
if fullscreen.active
Expand Down
3 changes: 3 additions & 0 deletions Placable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ def orientation

def place
# Endless screen
if x > 1 || x < -1 || y > 1 || y < -1
#self.orientation += rand(90) - 90
end
self.x = -1 if x > 1
self.y = -1 if y > 1
self.x = 1 if x < -1
Expand Down
8 changes: 4 additions & 4 deletions Player.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ def initialize
end

def redraw(tick)
self.score.draw
glPushMatrix
glColor3f(0.7,0.7,0.7)
place
Expand Down Expand Up @@ -87,9 +86,10 @@ def make_picture(planes)
score_history.map{|k, history| score_history[k] = history[0..40] }
# TODO set planes that are not in view to []
planes_scored = objects_in_view(planes).each do |hit|
score_history[hit.object_id] ||= []
score_history[hit.object_id].unshift(1)
score_delta += score_history[hit.object_id].inject {|sum, i| sum + i}
score_history[hit.first.object_id] ||= []
score_history[hit.first.object_id].unshift(1)
score_delta += score_history[hit.first.object_id].inject {|sum, i| sum + i}
score_delta += 100 if hit.first.shooting
end
score_delta *= planes_scored.size
self.score + score_delta
Expand Down
8 changes: 4 additions & 4 deletions Score.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ def initialize(x,y,z)
self.z = z
self.score = 0
self.point_size = 0.03
self.binary = false
self.binary = true
end

def draw
(self.binary ?
(self.score/16).to_s(2).split('').reverse :
self.score.to_s.split('').reverse)
(self.score.to_i/16).to_s(2).split('').reverse :
self.score.to_i.to_s.split('').reverse)
.each_with_index do |p, i|
glPushMatrix
c = self.binary ? (p == "1" ? 0.8 : 0.2) : p.to_f/10.0
glColor3f(c, c, c)
glTranslatef(self.x + i * point_size + point_size / 4, self.y, i*0.1)
glTranslatef(self.x + (i * point_size) + (point_size / 4.0), self.y, 0)

glBegin(GL_QUADS)
glVertex3f( 0.0, 0.0, 0.0);
Expand Down
3 changes: 2 additions & 1 deletion Shot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ def initialize(x,y,orientation)
self.x = x
self.y = y
self.z = 0
self.speed = 10
self.speed = 20
self.orientation = orientation
NSSound.soundNamed('Ping').play
end

def redraw
Expand Down
6 changes: 6 additions & 0 deletions The Flying Camera.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@
0E2BDC1012B26E8B00D634EE /* GLUT.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 0E2BDC0F12B26E8B00D634EE /* GLUT.framework */; };
0E319B1312B3D2AC00D0E514 /* Fullscreen.rb in Resources */ = {isa = PBXBuildFile; fileRef = 0E319B1212B3D2AC00D0E514 /* Fullscreen.rb */; };
0E8DA2D712B8F40D00E6C4D2 /* README.markdown in Resources */ = {isa = PBXBuildFile; fileRef = 0E8DA2D612B8F40D00E6C4D2 /* README.markdown */; };
0E9A96AE12B633C200878834 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 29B97325FDCFA39411CA2CEA /* Foundation.framework */; };
0E9A96AF12B633C600878834 /* CoreData.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 13E42FB307B3F0F600E4EEF1 /* CoreData.framework */; };
0E9A96B012B633C700878834 /* AppKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 29B97324FDCFA39411CA2CEA /* AppKit.framework */; };
0EB704E312B39CC2006FF01B /* Visibility.rb in Resources */ = {isa = PBXBuildFile; fileRef = 0EB704E212B39CC2006FF01B /* Visibility.rb */; };
0ED0536812B535330095EAC6 /* Score.rb in Resources */ = {isa = PBXBuildFile; fileRef = 0ED0536712B535330095EAC6 /* Score.rb */; };
0ED4F78D12B557E60064E48A /* Shot.rb in Resources */ = {isa = PBXBuildFile; fileRef = 0ED4F78C12B557E60064E48A /* Shot.rb */; };
Expand Down Expand Up @@ -122,6 +125,9 @@
4DE3BE140D8651D900ECA448 /* MacRuby.framework in Frameworks */,
0EF05CD612B14B2A009C18E8 /* OpenGL.framework in Frameworks */,
0E2BDC1012B26E8B00D634EE /* GLUT.framework in Frameworks */,
0E9A96AE12B633C200878834 /* Foundation.framework in Frameworks */,
0E9A96AF12B633C600878834 /* CoreData.framework in Frameworks */,
0E9A96B012B633C700878834 /* AppKit.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
2 changes: 1 addition & 1 deletion Visibility.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def objects_in_view(objects)
n = view_angle[0]
m = view_angle[1]
if (m < n && gamma > m && gamma < n) || (m > n && (gamma > m || gamma < n))
[object, gamma]
[object, gamma, Math.sqrt(a**2 + b**2)]
else
nil
end
Expand Down

0 comments on commit 0b1cea0

Please sign in to comment.