triangle fish

This commit is contained in:
Evan Pratten 2021-04-24 11:17:34 -04:00
parent 556a9696cc
commit a0735b774b
8 changed files with 149 additions and 7 deletions

View File

@ -2,5 +2,11 @@
"end_position": {
"x": 10000.0,
"y": 10000.0
}
},
"fish": [
{
"x": 100.0,
"y": 100.0
}
]
}

98
src/entities/fish.rs Normal file
View File

@ -0,0 +1,98 @@
use raylib::prelude::*;
use crate::{gamecore::GameCore, lib::utils::triangles::rotate_vector, player::Player};
const FISH_FOLLOW_PLAYER_DISTANCE: f32 = 80.0;
const FISH_FOLLOW_PLAYER_SPEED: f32 = 2.0;
const FISH_FOLLOW_PLAYER_SPEED_FAST: f32 = 6.0;
#[derive(Debug, Clone)]
pub struct FishEntity {
position: Vector2,
direction: Vector2,
following_player: bool,
size: Vector2
}
impl FishEntity {
pub fn new(position: Vector2) -> Self {
Self {
position: position,
direction: Vector2::zero(),
following_player: true,
size: Vector2 {
x: 5.0,
y: 8.0
}
}
}
pub fn new_from_positions(positions: &Vec<Vector2>) -> Vec<Self> {
let mut output = Vec::new();
for position in positions {
output.push(FishEntity::new(*position));
}
return output;
}
pub fn handle_follow_player(&mut self, player: &Player) {
// Distance and direction to player
let dist_to_player = player.position - self.position;
let dist_to_player_lin = self.position.distance_to(player.position);
let mut direction_to_player = dist_to_player;
direction_to_player.normalize();
// Fish movement
let movement;
// If the fish is double its follow distance from the player
if dist_to_player_lin.abs() > (FISH_FOLLOW_PLAYER_DISTANCE * 2.0) {
movement = direction_to_player * FISH_FOLLOW_PLAYER_SPEED_FAST;
} else {
// Move slowly in the direction of the player unless too close
if dist_to_player_lin.abs() > FISH_FOLLOW_PLAYER_DISTANCE {
movement = direction_to_player * FISH_FOLLOW_PLAYER_SPEED;
} else {
movement = Vector2::zero();
}
}
// Move the fish
self.direction = direction_to_player;
self.position += movement;
}
pub fn handle_free_movement(&mut self) {}
pub fn update_position(&mut self, player: &Player, dt: f64) {
if self.following_player {
self.handle_follow_player(player);
} else {
self.handle_free_movement();
}
}
pub fn render(&self, context_2d: &mut RaylibMode2D<RaylibDrawHandle>) {
// Direction
let direction = Vector2::zero().angle_to(self.direction.normalized()) + (90.0 as f32).to_radians();
// Get the corners of the fish
let fish_front = rotate_vector(Vector2 {
x: 0.0,
y: (self.size.y / 2.0) * -1.0,
}, direction);
let fish_bl = rotate_vector(Vector2 {
x: (self.size.x / 2.0) * -1.0,
y: (self.size.y / 2.0),
}, direction);
let fish_br = rotate_vector(Vector2 {
x: (self.size.x / 2.0),
y: (self.size.y / 2.0),
}, direction);
// Draw the fish as a triangle with rotation
context_2d.draw_triangle(self.position + fish_front, self.position + fish_bl, self.position + fish_br, Color::BLACK);
}
}

1
src/entities/mod.rs Normal file
View File

@ -0,0 +1 @@
pub mod fish;

View File

@ -1 +1,2 @@
pub mod profiler;
pub mod triangles;

View File

@ -0,0 +1,14 @@
use raylib::math::Vector2;
pub fn rotate_vector(vector: Vector2, angle_rad: f32) -> Vector2{
// let dist = (vector.x * vector.x) + (vector.y * vector.y);
// let angle = (vector.x.abs() / vector.y.abs()).atan();
// let angle = angle + angle_rad;
return Vector2 {
x: (vector.x * angle_rad.cos()) - (vector.y * angle_rad.sin()),
y: (vector.y * angle_rad.cos()) + (vector.x * angle_rad.sin()),
};
}

View File

@ -1,5 +1,5 @@
mod playerlogic;
mod hud;
mod playerlogic;
use raylib::prelude::*;
@ -33,8 +33,6 @@ impl InGameScreen {
) {
context_2d.draw_circle(0, 0, 10.0, Color::BLACK);
}
}
impl Screen for InGameScreen {
@ -45,6 +43,9 @@ impl Screen for InGameScreen {
audio_system: &mut AudioPlayer,
game_core: &mut GameCore,
) -> Option<GameState> {
// Calculate DT
let dt = draw_handle.get_time() - game_core.last_frame_time;
// Clear frame
draw_handle.clear_background(Color::BLUE);
@ -71,6 +72,13 @@ impl Screen for InGameScreen {
// Render the world
self.render_world(&mut context_2d, game_core);
// Render entities
let mut fish = &mut game_core.world.fish;
for fish in fish.iter_mut() {
fish.update_position(&game_core.player, dt);
fish.render(&mut context_2d);
}
// Render Player
playerlogic::render_player(&mut context_2d, game_core);
}

View File

@ -5,6 +5,7 @@ mod resources;
mod player;
mod world;
mod pallette;
mod entities;
use gamecore::{GameCore, GameState};
use lib::{utils::profiler::GameProfiler, wrappers::audio::player::AudioPlayer};

View File

@ -5,9 +5,17 @@ use serde::{Deserialize, Serialize};
use std::io::Read;
use failure::Error;
use crate::entities::fish::FishEntity;
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct World {
pub end_position: Vector2
pub end_position: Vector2,
#[serde(rename = "fish")]
pub fish_positions: Vec<Vector2>,
#[serde(skip)]
pub fish: Vec<FishEntity>
}
impl World {
@ -17,6 +25,11 @@ impl World {
let reader = BufReader::new(file);
// Deserialize
Ok(serde_json::from_reader(reader)?)
let mut result: World = serde_json::from_reader(reader)?;
// Init all fish
result.fish = FishEntity::new_from_positions(&result.fish_positions);
Ok(result)
}
}