line-em-up/models/player.rb in line-em-up-0.4.0 vs line-em-up/models/player.rb in line-em-up-2.0.0

- old
+ new

@@ -1,15 +1,26 @@ require_relative 'general_object.rb' require_relative 'rocket_launcher_pickup.rb' +require_relative '../lib/config_setting.rb' +require 'gosu' +require 'opengl' +require 'glut' + + +include OpenGL +include GLUT + class Player < GeneralObject + CONFIG_FILE = "#{CURRENT_DIRECTORY}/../config.txt" + puts "CONFIG SHOULD BE HERE: #{CONFIG_FILE}" SPEED = 7 MAX_ATTACK_SPEED = 3.0 attr_accessor :cooldown_wait, :secondary_cooldown_wait, :attack_speed, :health, :armor, :x, :y, :rockets, :score, :time_alive attr_accessor :bombs, :secondary_weapon, :grapple_hook_cooldown_wait, :damage_reduction, :boost_increase, :damage_increase, :kill_count - attr_accessor :special_attack + attr_accessor :special_attack, :main_weapon, :drawable_items_near_self, :broadside_mode MAX_HEALTH = 200 SECONDARY_WEAPONS = [RocketLauncherPickup::NAME] + %w[bomb] # Range goes clockwise around the 0-360 angle MISSILE_LAUNCHER_MIN_ANGLE = 75 @@ -17,23 +28,113 @@ MISSILE_LAUNCHER_INIT_ANGLE = 90 SPECIAL_POWER = 'laser' SPECIAL_POWER_KILL_MAX = 50 + def initialize(scale, x, y, screen_width, screen_height, options = {}) + super(scale, x, y, screen_width, screen_height, options) + # Top of screen + @min_moveable_height = options[:min_moveable_height] || 0 + # Bottom of the screen + @max_movable_height = options[:max_movable_height] || screen_height + # @right_image = Gosu::Image.new("#{MEDIA_DIRECTORY}/spaceship_right.png") + # @left_image = Gosu::Image.new("#{MEDIA_DIRECTORY}/spaceship_left.png") + # @broadside_image = Gosu::Image.new("#{MEDIA_DIRECTORY}/spaceship_broadside.png") + @score = 0 + @cooldown_wait = 0 + @secondary_cooldown_wait = 0 + @grapple_hook_cooldown_wait = 0 + @attack_speed = 3 + # @attack_speed = 3 + @health = 100.0 + @armor = 0 + @rockets = 50 + # @rockets = 250 + @bombs = 3 + @secondary_weapon = RocketLauncherPickup::NAME + @turn_right = false + @turn_left = false + + @hard_point_items = [RocketLauncherPickup::NAME, 'cannon_launcher', 'cannon_launcher', 'bomb_launcher'] + @rocket_launchers = 0 + @bomb_launchers = 0 + @cannon_launchers = 0 + trigger_hard_point_load + @damage_reduction = options[:handicap] ? options[:handicap] : 1 + invert_handicap = 1 - options[:handicap] + @boost_increase = invert_handicap > 0 ? 1 + (invert_handicap * 1.25) : 1 + @damage_increase = invert_handicap > 0 ? 1 + (invert_handicap) : 1 + @kill_count = 0 + @main_weapon = nil + # @drawable_items_near_self = [] + @broadside_mode = false + ship = ConfigSetting.get_setting(CONFIG_FILE, 'ship', BasicShip) + if ship + ship_class = eval(ship) + @ship = ship_class.new(scale, x, y, screen_width, screen_height, options) + else + @ship = BasicShip.new(scale, x, y, screen_width, screen_height, options) + end + @angle = 90 + end + def get_kill_count_max self.class::SPECIAL_POWER_KILL_MAX end def ready_for_special? @kill_count >= get_kill_count_max end - def special_attack + def special_attack object_groups # Fire special attack. @kill_count = 0 + projectiles = [] + object_groups.each do |group| + group.each do |object| + next if object.nil? + projectiles << Missile.new(@scale, @screen_width, @screen_height, self, object.x, object.y, nil, nil, nil, {damage_increase: @damage_increase}) + end + end + return projectiles end + + def special_attack_2 + # Fire special attack. + @kill_count = 0 + projectiles = [] + # object_groups.each do |group| + # group.each do |object| + # next if object.nil? + # projectiles << Missile.new(@scale, @screen_width, @screen_height, self, object.x, object.y, nil, nil, nil, {damage_increase: @damage_increase}) + # end + # end + + r = 10 * @scale + theta = 0 + count_max = 360 + max_passes = 3 + pass_count = 0 + theta = 0 + # Need a projectile queue system? + while theta < count_max + x = @x + r * Math.cos(theta) + y = @y + r * Math.sin(theta) + if y < @y + + projectiles << Missile.new(@scale, @screen_width, @screen_height, self, x, y, nil, nil, nil, {damage_increase: @damage_increase}) + + end + theta += 5 + end + # where r is the radius of the circle, and h,k are the coordinates of the center. + + return projectiles + end + + # Rocket Launcher, Rocket launcher, Cannon, Cannon, Bomb Launcher HARD_POINTS = 12 def add_kill_count kill_count if @kill_count + kill_count > get_kill_count_max @@ -67,52 +168,59 @@ end count += 1 end end - def get_image - Gosu::Image.new("#{MEDIA_DIRECTORY}/spaceship.png") - end + # Issue with image.. probably shouldn't be using images to define sizes + # def get_image + # # if @inited + # # @ship.get_image + # # end + # if @broadside_mode + # Gosu::Image.new("#{MEDIA_DIRECTORY}/pilotable_ships/basic_ship/spaceship_broadside.png") + # else + # Gosu::Image.new("#{MEDIA_DIRECTORY}/pilotable_ships/basic_ship/spaceship.png") + # end + # end + + # def get_image_path + # "#{MEDIA_DIRECTORY}/spaceship.png" + # end - def initialize(scale, x, y, screen_width, screen_height, options = {}) - super(scale, x, y, screen_width, screen_height, options) - # Top of screen - @min_moveable_height = options[:min_moveable_height] || 0 - # Bottom of the screen - @max_movable_height = options[:max_movable_height] || screen_height - @right_image = Gosu::Image.new("#{MEDIA_DIRECTORY}/spaceship_right.png") - @left_image = Gosu::Image.new("#{MEDIA_DIRECTORY}/spaceship_left.png") - @score = 0 - @cooldown_wait = 0 - @secondary_cooldown_wait = 0 - @grapple_hook_cooldown_wait = 0 - @attack_speed = 1 - # @attack_speed = 3 - @health = 100.0 - @armor = 0 - @rockets = 50 - # @rockets = 250 - @bombs = 3 - @secondary_weapon = RocketLauncherPickup::NAME - @turn_right = false - @turn_left = false - @hard_point_items = [RocketLauncherPickup::NAME, 'cannon_launcher', 'cannon_launcher', 'bomb_launcher'] - @rocket_launchers = 0 - @bomb_launchers = 0 - @cannon_launchers = 0 - trigger_hard_point_load - @damage_reduction = options[:handicap] ? options[:handicap] : 1 - invert_handicap = 1 - options[:handicap] - @boost_increase = invert_handicap > 0 ? 1 + (invert_handicap * 1.25) : 1 - @damage_increase = invert_handicap > 0 ? 1 + (invert_handicap) : 1 - @kill_count = 0 + def rotate_counterclockwise + return @ship.rotate_counterclockwise end + def rotate_clockwise + return @ship.rotate_clockwise + end + # def laser_attack pointer + # if @main_weapon.nil? + # # options = {damage_increase: @damage_increase, relative_y_padding: @image_height_half} + # options = {damage_increase: @damage_increase} + # @main_weapon = LaserBeam.new(@scale, @screen_width, @screen_height, self, options) + # @drawable_items_near_self << @main_weapon + # return { + # projectiles: [@main_weapon.attack], + # cooldown: LaserBeam::COOLDOWN_DELAY + # } + # else + # @main_weapon.active = true if @main_weapon.active == false + # @drawable_items_near_self << @main_weapon + # return { + # projectiles: [@main_weapon.attack], + # cooldown: LaserBeam::COOLDOWN_DELAY + # } + # end + # end + + def take_damage damage - @health -= damage * @damage_reduction + @ship.take_damage(damage) + # @health -= damage * @damage_reduction end def toggle_secondary current_index = SECONDARY_WEAPONS.index(@secondary_weapon) if current_index == SECONDARY_WEAPONS.count - 1 @@ -156,153 +264,102 @@ def get_y @y end def is_alive - health > 0 + @ship.is_alive + # health > 0 end - def get_speed - (SPEED * @scale).round - end + # CAP movement w/ Acceleration!!!!!!!!!!!!!!!!!!! - def move_left - @turn_left = true - @x = [@x - get_speed, (get_width/3)].max + def move_left movement_x = 0, movement_y = 0 + # @x = @ship.move_left + return [movement_x - 1.0, movement_y] end - def move_right - @turn_right = true - @x = [@x + get_speed, (@screen_width - (get_width/3))].min + def move_right movement_x = 0, movement_y = 0 + # @x = @ship.move_right + return [movement_x + 1.0, movement_y] end - def accelerate - # # Top of screen - # @min_moveable_height = options[:min_moveable_height] || 0 - # # Bottom of the screen - # @max_movable_height = options[:max_movable_height] || screen_height - - @y = [@y - get_speed, @min_moveable_height + (get_height/2)].max + def accelerate movement_x = 0, movement_y = 0 + # @y = @ship.accelerate + puts "ACCELLERATE" + return [movement_x, movement_y + 1.0] end - def brake - @y = [@y + get_speed, @max_movable_height - (get_height/2)].min + def brake movement_x = 0, movement_y = 0 + # @y = @ship.brake + return [movement_x, movement_y - 1.0] end - def attack pointer - return { - projectiles: [ - Bullet.new(@scale, @screen_width, @screen_height, self, {side: 'left', damage_increase: @damage_increase}), - Bullet.new(@scale, @screen_width, @screen_height, self, {side: 'right', damage_increase: @damage_increase}) - ], - cooldown: Bullet::COOLDOWN_DELAY - } + def attack_group_1 pointer + @ship.attack_group_1(pointer) end + + def deactivate_group_1 + @ship.deactivate_group_1 + end - def trigger_secondary_attack pointer - return_projectiles = [] - if self.secondary_cooldown_wait <= 0 && self.get_secondary_ammo_count > 0 - results = self.secondary_attack(pointer) - projectiles = results[:projectiles] - cooldown = results[:cooldown] - self.secondary_cooldown_wait = cooldown.to_f.fdiv(self.attack_speed) - - projectiles.each do |projectile| - return_projectiles.push(projectile) - end - end - return return_projectiles + def attack_group_2 pointer + @ship.attack_group_2(pointer) end + + def deactivate_group_2 + @ship.deactivate_group_2 + end - # def toggle_state_secondary_attack - # second_weapon = case @secondary_weapon - # when 'bomb' - # else + + # def trigger_secondary_attack pointer + # return_projectiles = [] + # if self.secondary_cooldown_wait <= 0 && self.get_secondary_ammo_count > 0 + # results = @ship.secondary_attack(pointer) + # projectiles = results[:projectiles] + # cooldown = results[:cooldown] + # self.secondary_cooldown_wait = cooldown.to_f.fdiv(self.attack_speed) + + # projectiles.each do |projectile| + # return_projectiles.push(projectile) + # end # end - # return second_weapon + # return return_projectiles # end - def secondary_attack pointer - projectiles = [] - cooldown = 0 - case @secondary_weapon - when 'bomb' - projectiles << Bomb.new(@scale, @screen_width, @screen_height, self, pointer.x, pointer.y, nil, nil, nil, {damage_increase: @damage_increase}) - cooldown = Bomb::COOLDOWN_DELAY - when RocketLauncherPickup::NAME - # NEEED TO DECRETMENT AMMO BASED OFF OF LAUNCHERS!!!!!!!!!!! - # puts "ROCKET LAUNCHERS: #{@rocket_launchers}" - cooldown = Missile::COOLDOWN_DELAY - if get_secondary_ammo_count == 1 && @rocket_launchers > 0 || @rocket_launchers == 1 - projectiles << Missile.new(@scale, @screen_width, @screen_height, self, pointer.x, pointer.y, MISSILE_LAUNCHER_MIN_ANGLE, MISSILE_LAUNCHER_MAX_ANGLE, MISSILE_LAUNCHER_INIT_ANGLE, {damage_increase: @damage_increase}) - elsif get_secondary_ammo_count == 2 && @rocket_launchers >= 2 || @rocket_launchers == 2 - projectiles << Missile.new(@scale, @screen_width, @screen_height, self, pointer.x - @image_width_half, pointer.y, MISSILE_LAUNCHER_MIN_ANGLE, MISSILE_LAUNCHER_MAX_ANGLE, MISSILE_LAUNCHER_INIT_ANGLE, {side: 'left', damage_increase: @damage_increase}) - projectiles << Missile.new(@scale, @screen_width, @screen_height, self, pointer.x + @image_width_half, pointer.y, MISSILE_LAUNCHER_MIN_ANGLE, MISSILE_LAUNCHER_MAX_ANGLE, MISSILE_LAUNCHER_INIT_ANGLE, {side: 'right', damage_increase: @damage_increase}) - elsif get_secondary_ammo_count == 3 && @rocket_launchers >= 3 || @rocket_launchers == 3 - projectiles << Missile.new(@scale, @screen_width, @screen_height, self, pointer.x, pointer.y, MISSILE_LAUNCHER_MIN_ANGLE, MISSILE_LAUNCHER_MAX_ANGLE, MISSILE_LAUNCHER_INIT_ANGLE, {damage_increase: @damage_increase}) - projectiles << Missile.new(@scale, @screen_width, @screen_height, self, pointer.x - @image_width_half, pointer.y, MISSILE_LAUNCHER_MIN_ANGLE, MISSILE_LAUNCHER_MAX_ANGLE, MISSILE_LAUNCHER_INIT_ANGLE, {side: 'left', damage_increase: @damage_increase}) - projectiles << Missile.new(@scale, @screen_width, @screen_height, self, pointer.x + @image_width_half, pointer.y, MISSILE_LAUNCHER_MIN_ANGLE, MISSILE_LAUNCHER_MAX_ANGLE, MISSILE_LAUNCHER_INIT_ANGLE, {side: 'right', damage_increase: @damage_increase}) - elsif get_secondary_ammo_count == 4 && @rocket_launchers >= 4 || @rocket_launchers == 4 - projectiles << Missile.new(@scale, @screen_width, @screen_height, self, pointer.x - @image_width_half, pointer.y, MISSILE_LAUNCHER_MIN_ANGLE, MISSILE_LAUNCHER_MAX_ANGLE + 15, MISSILE_LAUNCHER_INIT_ANGLE, {side: 'left', damage_increase: @damage_increase}) - projectiles << Missile.new(@scale, @screen_width, @screen_height, self, pointer.x + @image_width_half, pointer.y, MISSILE_LAUNCHER_MIN_ANGLE - 15, MISSILE_LAUNCHER_MAX_ANGLE, MISSILE_LAUNCHER_INIT_ANGLE, {side: 'right', damage_increase: @damage_increase}) - - projectiles << Missile.new(@scale, @screen_width, @screen_height, self, pointer.x - @image_width_half / 2, pointer.y, MISSILE_LAUNCHER_MIN_ANGLE, MISSILE_LAUNCHER_MAX_ANGLE + 5, MISSILE_LAUNCHER_INIT_ANGLE, {side: 'left', damage_increase: @damage_increase, relative_x_padding: (@image_width_half / 2) }) - projectiles << Missile.new(@scale, @screen_width, @screen_height, self, pointer.x + @image_width_half / 2, pointer.y, MISSILE_LAUNCHER_MIN_ANGLE - 5, MISSILE_LAUNCHER_MAX_ANGLE, MISSILE_LAUNCHER_INIT_ANGLE, {side: 'right', damage_increase: @damage_increase, relative_x_padding: -(@image_width_half / 2) }) - elsif get_secondary_ammo_count == 5 && @rocket_launchers >= 5 || @rocket_launchers >= 5 - projectiles << Missile.new(@scale, @screen_width, @screen_height, self, pointer.x, pointer.y, MISSILE_LAUNCHER_MIN_ANGLE, MISSILE_LAUNCHER_MAX_ANGLE, MISSILE_LAUNCHER_INIT_ANGLE, {damage_increase: @damage_increase}) - projectiles << Missile.new(@scale, @screen_width, @screen_height, self, pointer.x - @image_width_half, pointer.y, MISSILE_LAUNCHER_MIN_ANGLE, MISSILE_LAUNCHER_MAX_ANGLE + 15, MISSILE_LAUNCHER_INIT_ANGLE, {side: 'left', damage_increase: @damage_increase}) - projectiles << Missile.new(@scale, @screen_width, @screen_height, self, pointer.x + @image_width_half, pointer.y, MISSILE_LAUNCHER_MIN_ANGLE - 15, MISSILE_LAUNCHER_MAX_ANGLE, MISSILE_LAUNCHER_INIT_ANGLE, {side: 'right', damage_increase: @damage_increase}) - - projectiles << Missile.new(@scale, @screen_width, @screen_height, self, pointer.x - @image_width_half / 2, pointer.y, MISSILE_LAUNCHER_MIN_ANGLE, MISSILE_LAUNCHER_MAX_ANGLE + 5, MISSILE_LAUNCHER_INIT_ANGLE, {side: 'left', damage_increase: @damage_increase, relative_x_padding: (@image_width_half / 2) }) - projectiles << Missile.new(@scale, @screen_width, @screen_height, self, pointer.x + @image_width_half / 2, pointer.y, MISSILE_LAUNCHER_MIN_ANGLE - 5, MISSILE_LAUNCHER_MAX_ANGLE, MISSILE_LAUNCHER_INIT_ANGLE, {side: 'right', damage_increase: @damage_increase, relative_x_padding: -(@image_width_half / 2) }) - else - raise "Should never get here: @secondary_weapon: #{@secondary_weapon} for @rocket_launchers: #{@rocket_launchers} and get_secondary_ammo_count: #{get_secondary_ammo_count}" - end - end - decrement_secondary_ammo_count projectiles.count - return {projectiles: projectiles, cooldown: cooldown} - end - def get_draw_ordering ZOrder::Player end def draw - if @turn_right - image = @right_image - elsif @turn_left - image = @left_image - else - image = @image - end - # super - image.draw(@x - get_width / 2, @y - get_height / 2, get_draw_ordering, @scale, @scale) - @turn_right = false - @turn_left = false + # @drawable_items_near_self.reject! { |item| item.draw } + @ship.draw end + + POINTS_X = 7 + POINTS_Y = 7 + + def draw_gl_list + # @drawable_items_near_self + [self] + @ship.draw_gl_list + end + + def draw_gl + @ship.draw_gl + end - def update mouse_x = nil, mouse_y = nil, player = nil + def update mouse_x = nil, mouse_y = nil, player = nil, scroll_factor = 1 + @ship.update(mouse_x, mouse_y, player, scroll_factor) + # Update list of weapons for special cases like beans. Could iterate though an association in the future. + # @main_weapon.update(mouse_x, mouse_y, player) if @main_weapon + @cooldown_wait -= 1 if @cooldown_wait > 0 @secondary_cooldown_wait -= 1 if @secondary_cooldown_wait > 0 @grapple_hook_cooldown_wait -= 1 if @grapple_hook_cooldown_wait > 0 @time_alive += 1 if self.is_alive end def collect_pickups(pickups) - pickups.reject! do |pickup| - if Gosu.distance(@x, @y, pickup.x, pickup.y) < ((self.get_radius) + (pickup.get_radius)) * 1.2 && pickup.respond_to?(:collected_by_player) - pickup.collected_by_player(self) - if pickup.respond_to?(:get_points) - self.score += pickup.get_points - end - # stop that! - # @beep.play - true - else - false - end - end + @ship.collect_pickups(pickups) end - end \ No newline at end of file