Skip to content
This repository has been archived by the owner on Apr 29, 2021. It is now read-only.

Commit

Permalink
Added an enemy that shoots
Browse files Browse the repository at this point in the history
  • Loading branch information
VictorKoenders committed Feb 25, 2017
1 parent 48f98e3 commit 1defd86
Show file tree
Hide file tree
Showing 134 changed files with 278 additions and 34 deletions.
9 changes: 7 additions & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@ install:

build_script:
- cargo build --release
- ps: Push-AppveyorArtifact target/release/janfeb.exe
- ps: Push-AppveyorArtifact .cargo/freetype/x86_64/freetype.dll
- mkdir output
- copy target\release\janfeb.exe output
- robocopy assets output/assets /s /e

test_script:
- cargo test --verbose --release -- --nocapture

artifacts:
- path:
name: janfeb.zip
Binary file added assets/M484BulletCollection1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/bullet.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed assets/spaceship.pdn
Binary file not shown.
Binary file removed assets/spaceship.png
Binary file not shown.
Binary file added assets/spaceships/aliens/alien1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/spaceships/aliens/alien10.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/spaceships/aliens/alien11.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/spaceships/aliens/alien12.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/spaceships/aliens/alien13.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/spaceships/aliens/alien14.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/spaceships/aliens/alien15.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/spaceships/aliens/alien2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/spaceships/aliens/alien3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/spaceships/aliens/alien4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/spaceships/aliens/alien5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/spaceships/aliens/alien6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/spaceships/aliens/alien7.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/spaceships/aliens/alien8.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/spaceships/aliens/alien9.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/spaceships/large/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/spaceships/large/10.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/spaceships/large/11.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/spaceships/large/12.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/spaceships/large/13.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/spaceships/large/14.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/spaceships/large/15.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/spaceships/large/16.png
Binary file added assets/spaceships/large/17.png
Binary file added assets/spaceships/large/18.png
Binary file added assets/spaceships/large/19.png
Binary file added assets/spaceships/large/2.png
Binary file added assets/spaceships/large/20.png
Binary file added assets/spaceships/large/21.png
Binary file added assets/spaceships/large/22.png
Binary file added assets/spaceships/large/23.png
Binary file added assets/spaceships/large/24.png
Binary file added assets/spaceships/large/25.png
Binary file added assets/spaceships/large/26.png
Binary file added assets/spaceships/large/27.png
Binary file added assets/spaceships/large/28.png
Binary file added assets/spaceships/large/29.png
Binary file added assets/spaceships/large/3.png
Binary file added assets/spaceships/large/30.png
Binary file added assets/spaceships/large/31.png
Binary file added assets/spaceships/large/32.png
Binary file added assets/spaceships/large/33.png
Binary file added assets/spaceships/large/34.png
Binary file added assets/spaceships/large/4.png
Binary file added assets/spaceships/large/5.png
Binary file added assets/spaceships/large/6.png
Binary file added assets/spaceships/large/7.png
Binary file added assets/spaceships/large/8.png
Binary file added assets/spaceships/large/9.png
Binary file added assets/spaceships/large/Thumbs.db
Binary file not shown.
Binary file added assets/spaceships/medium/1.png
Binary file added assets/spaceships/medium/11.png
Binary file added assets/spaceships/medium/13.png
Binary file added assets/spaceships/medium/15.png
Binary file added assets/spaceships/medium/17.png
Binary file added assets/spaceships/medium/19.png
Binary file added assets/spaceships/medium/2.png
Binary file added assets/spaceships/medium/21.png
Binary file added assets/spaceships/medium/22.png
Binary file added assets/spaceships/medium/24.png
Binary file added assets/spaceships/medium/27.png
Binary file added assets/spaceships/medium/29.png
Binary file added assets/spaceships/medium/3.png
Binary file added assets/spaceships/medium/30.png
Binary file added assets/spaceships/medium/31.png
Binary file added assets/spaceships/medium/35.png
Binary file added assets/spaceships/medium/36.png
Binary file added assets/spaceships/medium/37.png
Binary file added assets/spaceships/medium/39.png
Binary file added assets/spaceships/medium/4.png
Binary file added assets/spaceships/medium/40.png
Binary file added assets/spaceships/medium/41.png
Binary file added assets/spaceships/medium/42.png
Binary file added assets/spaceships/medium/43.png
Binary file added assets/spaceships/medium/5.png
Binary file added assets/spaceships/medium/6.png
Binary file added assets/spaceships/medium/Thumbs.db
Binary file not shown.
8 changes: 8 additions & 0 deletions assets/spaceships/readme.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
You are free to do anything with these graphics beside selling them. Using them for commercial projects is fine.

Note that I'm far from being an artist and these graphics are primarily meant as placeholders.

Not all spaceships are available in all sizes.

Dave Toulouse
www.over00.com
Binary file added assets/spaceships/small/0.png
Binary file added assets/spaceships/small/1.png
Binary file added assets/spaceships/small/10.png
Binary file added assets/spaceships/small/11.png
Binary file added assets/spaceships/small/13.png
Binary file added assets/spaceships/small/14.png
Binary file added assets/spaceships/small/15.png
Binary file added assets/spaceships/small/16.png
Binary file added assets/spaceships/small/17.png
Binary file added assets/spaceships/small/18.png
Binary file added assets/spaceships/small/19.png
Binary file added assets/spaceships/small/2.png
Binary file added assets/spaceships/small/20.png
Binary file added assets/spaceships/small/21.png
Binary file added assets/spaceships/small/22.png
Binary file added assets/spaceships/small/23.png
Binary file added assets/spaceships/small/24.png
Binary file added assets/spaceships/small/27.png
Binary file added assets/spaceships/small/28.png
Binary file added assets/spaceships/small/29.png
Binary file added assets/spaceships/small/3.png
Binary file added assets/spaceships/small/30.png
Binary file added assets/spaceships/small/31.png
Binary file added assets/spaceships/small/32.png
Binary file added assets/spaceships/small/33.png
Binary file added assets/spaceships/small/34.png
Binary file added assets/spaceships/small/35.png
Binary file added assets/spaceships/small/36.png
Binary file added assets/spaceships/small/37.png
Binary file added assets/spaceships/small/4.png
Binary file added assets/spaceships/small/5.png
Binary file added assets/spaceships/small/7.png
Binary file added assets/spaceships/small/8.png
Binary file added assets/spaceships/small/Thumbs.db
Binary file not shown.
Binary file added assets/spaceships/spacestations/1.png
Binary file added assets/spaceships/spacestations/2.png
12 changes: 10 additions & 2 deletions src/engine/engine_graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use glium::uniforms::UniformsStorage;
use glium::glutin::WindowBuilder;
use glium::index::PrimitiveType;
use std::collections::HashMap;
use std::fs::File;
use std::io::Read;


#[derive(Copy, Clone)]
Expand Down Expand Up @@ -76,8 +78,14 @@ impl<T: TGraphicIndex> EngineGraphics<T> {
})
}

pub fn load_graphic(&mut self, key: T, bytes: &[u8], width: f32, height: f32) -> Result<()> {
let param = DrawHelper::new(self, width, height, bytes)?;
pub fn load_graphic(&mut self, key: T, file: &str, width: f32, height: f32) -> Result<()> {
let bytes = {
let mut file = File::open(file)?;
let mut vec = Vec::new();
file.read_to_end(&mut vec)?;
vec
};
let param = DrawHelper::new(self, width, height, &bytes)?;
self.graphics.insert(key, param);
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion src/engine/entity/entity_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub trait EntityTrait<T: TGraphicIndex> {
fn draw(&self, _state: &EntityState, _graphics: &mut EngineGraphics<T>) -> Result<()> {
Ok(())
}
fn get_initial_state(&self, _engine: &Engine<T>) -> EntityState {
fn get_initial_state(&mut self, _engine: &Engine<T>) -> EntityState {
EntityState::default()
}
fn update(&mut self,
Expand Down
1 change: 1 addition & 0 deletions src/engine/entity/wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub struct EntityWrapper<T: TGraphicIndex> {

impl<T: TGraphicIndex> EntityWrapper<T> {
pub fn new(entity: Box<EntityTrait<T>>, engine: &Engine<T>) -> EntityWrapper<T> {
let mut entity = entity;
EntityWrapper {
state: entity.get_initial_state(engine),
name: entity.identifying_string(),
Expand Down
11 changes: 11 additions & 0 deletions src/engine/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use glium::texture::TextureCreationError;
use glium::glutin::CreationError;
use std::convert::From;
use image::ImageError;
use std::io::Error as ioError;

pub type Result<T> = ::std::result::Result<T, Error>;

Expand All @@ -28,6 +29,7 @@ pub enum ErrorEnum {
// NoInnerPixelSize,
// ComponentNotFound(u64),
ImageError(ImageError),
IOError(ioError),
}

// impl Error {
Expand Down Expand Up @@ -131,3 +133,12 @@ impl From<()> for Error {
}
}
}

impl From<ioError> for Error {
fn from(err: ioError) -> Self {
Error {
description: format!("IO error: {:?}", err),
error_type: ErrorEnum::IOError(err)
}
}
}
28 changes: 28 additions & 0 deletions src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,34 @@ pub struct Engine<T: TGraphicIndex> {
pub graphics: EngineGraphics<T>,
pub keyboard: KeyboardState,
pub running: bool,
pub render_hitboxes: bool,

pub last_update_time: u64,
pub entities: Vec<EntityWrapper<T>>,
pub rng: ::rand::ThreadRng,
}

impl<T: TGraphicIndex> Engine<T> {
#[cfg(not(debug_assertions))]
pub fn new(width: f32, height: f32) -> Result<Engine<T>> {
let engine = Engine {
graphics: EngineGraphics::<T>::new(width, height)?,
keyboard: KeyboardState::default(),
running: true,
render_hitboxes: false,
last_update_time: self::time::get(),
entities: Vec::new(),
rng: ::rand::thread_rng(),
};
Ok(engine)
}
#[cfg(debug_assertions)]
pub fn new(width: f32, height: f32) -> Result<Engine<T>> {
let engine = Engine {
graphics: EngineGraphics::<T>::new(width, height)?,
keyboard: KeyboardState::default(),
running: true,
render_hitboxes: true,
last_update_time: self::time::get(),
entities: Vec::new(),
rng: ::rand::thread_rng(),
Expand All @@ -61,6 +77,18 @@ impl<T: TGraphicIndex> Engine<T> {
frame.clear_color(0.0, 0.0, 1.0, 1.0);
}

if self.render_hitboxes {
for entity in &self.entities {
self.graphics.draw_rectangle(
entity.state.x - entity.state.hitbox.left,
entity.state.y - entity.state.hitbox.top,
entity.state.hitbox.left + entity.state.hitbox.right,
entity.state.hitbox.top + entity.state.hitbox.bottom,
(0.0, 0.0, 0.0, 0.0f32)
)?;
}
}

for entity in &self.entities {
entity.entity.draw(&entity.state, &mut self.graphics)?;
}
Expand Down
10 changes: 6 additions & 4 deletions src/entities/bullet.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use GraphicsEnum;
use engine::*;
use std::f32::consts::PI;

pub const WIDTH: f32 = 16.0;
pub const HEIGHT: f32 = 16.0;
Expand Down Expand Up @@ -34,7 +35,7 @@ impl EntityTrait<GraphicsEnum> for Bullet {
if self.is_player_bullet { vec![CollisionLayer::Enemy] }
else { vec![CollisionLayer::Player] }
}
fn get_initial_state(&self, _: &Engine<GraphicsEnum>) -> EntityState {
fn get_initial_state(&mut self, _: &Engine<GraphicsEnum>) -> EntityState {
let hitbox = Hitbox {
left: 6f32,
top: 6f32,
Expand All @@ -54,12 +55,12 @@ impl EntityTrait<GraphicsEnum> for Bullet {
state: &mut EntityState)
-> Vec<EntityEvent<GraphicsEnum>> {
if self.is_player_bullet {
state.x += 0.5f32 * game_state.delta_time;
state.x += 1f32 * game_state.delta_time;
if state.x - state.hitbox.left > game_state.screen_width {
state.active = false;
}
} else {
state.x -= 0.1f32 * game_state.delta_time;
state.x -= 0.5f32 * game_state.delta_time;

if state.x + state.hitbox.right < 0f32 {
state.active = false;
Expand All @@ -74,6 +75,7 @@ impl EntityTrait<GraphicsEnum> for Bullet {
}

fn draw(&self, state: &EntityState, graphics: &mut EngineGraphics<GraphicsEnum>) -> Result<()> {
graphics.draw(GraphicsEnum::Bullet, state.x, state.y, 0f32, 1f32)
let angle = if self.is_player_bullet { 0f32 } else { PI };
graphics.draw(GraphicsEnum::Bullet, state.x, state.y, angle, 1f32)
}
}
95 changes: 95 additions & 0 deletions src/entities/enemies/enemy_1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
use std::f32::consts::PI;
use entities::Bullet;
use GraphicsEnum;
use engine::*;

pub struct Enemy1 {
start_x: f32,
start_y: f32,
health: f32,
diagonal_direction: f32,
bullet_target_time: f32,
bullet_recharge_counter: f32,
}

const ONETHIRD: f32 = 1f32 / 3f32;
const TWOTHIRD: f32 = ONETHIRD * 2f32;

impl Enemy1 {
pub fn new(x: f32, y: f32, factor: f32) -> Enemy1 {
let bullet_target_time = 5000f32 / (factor / 2f32);
Enemy1 {
start_x: x,
start_y: y,
health: factor / 5f32,
diagonal_direction: 0f32,
bullet_target_time: bullet_target_time,
bullet_recharge_counter: bullet_target_time / 2f32,
}
}
}

impl EntityTrait<GraphicsEnum> for Enemy1 {
fn identifying_string(&self) -> String {
"Enemy type 1".to_owned()
}
fn collision_layers(&self) -> Option<CollisionLayer> {
Some(CollisionLayer::Enemy)
}
fn collides_with_layers(&self) -> Vec<CollisionLayer> {
vec![CollisionLayer::Player]
}
fn get_initial_state(&mut self, engine: &Engine<GraphicsEnum>) -> EntityState {
let hitbox = Hitbox {
left: 25f32,
top: 40f32,
right: 25f32,
bottom: 40f32,
};
self.diagonal_direction = if self.start_y > engine.graphics.height / 2f32 { -1f32 } else { 1f32 };
EntityState {
x: self.start_x,
y: self.start_y,
hitbox: hitbox,
..EntityState::default()
}
}
fn update(&mut self,
game_state: &mut GameState,
state: &mut EntityState)
-> Vec<EntityEvent<GraphicsEnum>> {


state.x -= 0.3f32 * game_state.delta_time;

if state.x > game_state.screen_width * ONETHIRD && state.x < game_state.screen_width * TWOTHIRD {
state.y += self.diagonal_direction * 0.1f32 * game_state.delta_time;
}

let mut result = Vec::new();

self.bullet_recharge_counter += game_state.delta_time;
if self.bullet_recharge_counter > self.bullet_target_time {
let bullet = Bullet::new(state.x, state.y, false);
self.bullet_recharge_counter -= self.bullet_target_time;
result.push(EntityEvent::SpawnEntity(Box::new(bullet)));
}
result
}
fn draw(&self, state: &EntityState, graphics: &mut EngineGraphics<GraphicsEnum>) -> Result<()> {
graphics.draw(GraphicsEnum::Player, state.x, state.y, PI / 2f32 * 3f32, 1f32)
}
fn collided(&mut self,
self_state: &mut EntityState,
_: &Box<EntityTrait<GraphicsEnum>>,
_: &mut EntityState)
-> Vec<EntityEvent<GraphicsEnum>> {
if self.health <= 1f32 {
self_state.active = false;
} else {
self.health -= 1f32;
}

Vec::new()
}
}
3 changes: 3 additions & 0 deletions src/entities/enemies/enemy_2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub struct Enemy2 {

}
3 changes: 3 additions & 0 deletions src/entities/enemies/enemy_3.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub struct Enemy3 {

}
7 changes: 7 additions & 0 deletions src/entities/enemies/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
mod enemy_1;
mod enemy_2;
mod enemy_3;

pub use self::enemy_1::Enemy1;
pub use self::enemy_2::Enemy2;
pub use self::enemy_3::Enemy3;
59 changes: 59 additions & 0 deletions src/entities/enemy_spawner.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use rand::{StdRng, SeedableRng, Rng};
use super::{Enemy1, Enemy2, Enemy3};
use GraphicsEnum;
use engine::*;

pub struct EnemySpawner {
pub time_counter: f32,
pub target_time_counter: f32,
pub enemy_amount_to_spawn: u32,
pub rng: StdRng,
}

impl EnemySpawner {
pub fn new() -> EnemySpawner {
let mut spawner = EnemySpawner {
time_counter: 0f32,
target_time_counter: 3000f32,
enemy_amount_to_spawn: 3,
rng: StdRng::from_seed(&[0usize]),
};
spawner
}

fn generate_time_target(&mut self) {
let factor = 5000f32 - ((self.enemy_amount_to_spawn - 3) * 100) as f32;
self.target_time_counter = factor + self.rng.next_f32() * factor;
}
}

impl EntityTrait<GraphicsEnum> for EnemySpawner {
fn identifying_string(&self) -> String {
"Enemy spawner".to_owned()
}
fn update(&mut self,
game_state: &mut GameState,
_: &mut EntityState)
-> Vec<EntityEvent<GraphicsEnum>> {
self.time_counter += game_state.delta_time;
let mut result = Vec::new();
while self.time_counter > self.target_time_counter {
self.time_counter -= self.target_time_counter;
let height = (game_state.screen_height) * self.rng.next_f32();
let mut x = game_state.screen_width;
let enemy_type = self.rng.next_u32() % 3 + 1;
println!("Spawning {} enemies at y {}!", self.enemy_amount_to_spawn, height);;
for i in 0..self.enemy_amount_to_spawn {
//match enemy_type {
//
//}
let enemy = Enemy1::new(x, height, self.enemy_amount_to_spawn as f32);
result.push(EntityEvent::SpawnEntity(Box::new(enemy)));
x += 50f32;
}
self.enemy_amount_to_spawn += 1;
self.generate_time_target();
}
result
}
}
8 changes: 6 additions & 2 deletions src/entities/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
pub mod bullet;
pub mod bullet_spawner;
pub mod player;
pub mod enemy_spawner;
pub mod you_lost;
pub mod enemies;
pub mod bullet;
pub mod player;

pub use self::enemies::{Enemy1, Enemy2, Enemy3};
pub use self::bullet_spawner::BulletSpawner;
pub use self::enemy_spawner::EnemySpawner;
pub use self::you_lost::YouLost;
pub use self::bullet::Bullet;
pub use self::player::Player;
Loading

0 comments on commit 1defd86

Please sign in to comment.