-
Notifications
You must be signed in to change notification settings - Fork 4
/
Enemy.rb
144 lines (132 loc) · 3.38 KB
/
Enemy.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#
# Enemy.rb
# CtF
#
# Created by Thomas R. Koll on 10.12.10.
# Copyright (c) 2010 ananasblau. All rights reserved.
#
require 'placable'
require 'movable'
require 'visibility'
class Enemy
include Placable
include Movable
include Visibility
attr_accessor :team, :view_angle, :shots, :shots_remaining, :shooting
def initialize(team)
self.team = team
self.x = rand * 2 - 1
self.y = rand * 2 - 1
self.orientation = rand(360)
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
def redraw(tick)
glPushMatrix
place
move
colour
Plane.draw
self.draw
glPopMatrix
self.shots = self.shots.map(&:redraw).compact
self.shots_remaining = [self.shots_remaining + 1, 10].min
end
def colour
glColor3f(*self.team.colour)
end
# Markings
def draw
case self.team.name
when 'Kaiserliche Fliegertruppe'
iron_cross(-0.005, 0.05, 0)
iron_cross(-0.005, -0.05, 0)
when 'Royal Flying Corps'
raf(-0.005, 0.05, 0)
raf(-0.005, -0.05, 0)
end
end
def iron_cross(x,y,z)
glPushMatrix
glTranslatef(x, y, z)
glColor3f(0, 0, 0)
s = 0.01
glBegin(GL_QUADS)
glVertex3f( s,-s/3.5, 0.0);
glVertex3f( s, s/3.5, 0.0);
glVertex3f(-s, s/3.5, 0.0);
glVertex3f(-s,-s/3.5, 0.0);
glVertex3f( s,-s/3.5, 0.0);
glEnd
glBegin(GL_QUADS)
glVertex3f( s/3.5,-s, 0.0);
glVertex3f( s/3.5, s, 0.0);
glVertex3f(-s/3.5, s, 0.0);
glVertex3f(-s/3.5,-s, 0.0);
glVertex3f( s/3.5,-s, 0.0);
glEnd
glPopMatrix
end
def raf(x, y, z)
glPushMatrix
glTranslatef(x, y, z)
glColor3f(0, 0.13, 0.48)
circle(0.009)
glColor3f(1, 1, 1)
circle(0.006)
glColor3f(0.8, 0.06, 0.14)
circle(0.002)
glPopMatrix
end
def circle(radius, segments = 20)
glBegin(GL_TRIANGLE_FAN)
glVertex2f(0, 0)
(segments+1).times do |i|
# current angle
theta = 2.0 * Math::PI * i / segments;
x = radius * Math.cos(theta);
y = radius * Math.sin(theta);
glVertex2f(x, y)
end
glEnd
end
def find_target(enemies)
self.speedUp(rand * 2 - 1)
# right
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
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
def turn_with_view(direction)
difference = self.turn_without_view(direction)
self.view_angle[0] = (self.view_angle[0] + difference) % 360
self.view_angle[1] = (self.view_angle[1] + difference) % 360
end
alias_method :turn_without_view, :turn
alias_method :turn, :turn_with_view
end