Skip to content

Commit

Permalink
Replaces ids with generational indexes inside ActorData and fixes exa…
Browse files Browse the repository at this point in the history
…mple to compile
  • Loading branch information
DarinM223 committed Sep 22, 2018
1 parent edc5345 commit 3c51845
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 75 deletions.
59 changes: 33 additions & 26 deletions examples/mario/src/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ use actors::coin::Coin;
use actors::koopa::Koopa;
use actors::player::Player;
use mold2d;
use mold2d::{ActorManager, CollisionSide, Context, MessageHandler, PositionChange, Viewport};
use mold2d::{
ActorIndex, ActorManager, ActorPosition, ActorToken, CollisionSide, Context, MessageHandler,
PositionChange, Viewport,
};
use sdl2::render::Renderer;

/// Actions for an actor to process
Expand All @@ -26,12 +29,12 @@ pub enum ActorAction {
/// Actor messages
#[derive(Clone, Debug, PartialEq)]
pub enum ActorMessage {
AddActor(char, (i32, i32)),
RemoveActor(i32),
AddActor(ActorToken, ActorPosition),
RemoveActor(ActorIndex),
SetViewport(i32, i32),
ActorAction {
send_id: i32,
recv_id: i32,
send_id: ActorIndex,
recv_id: ActorIndex,
action: ActorAction,
},
MultipleMessages(Vec<Box<ActorMessage>>),
Expand All @@ -56,26 +59,26 @@ pub type ActorData = mold2d::ActorData<ActorType>;

#[inline]
pub fn actor_from_token(
token: char,
id: i32,
position: (i32, i32),
ActorToken(token): ActorToken,
index: ActorIndex,
position: ActorPosition,
renderer: &mut Renderer,
) -> Box<Actor> {
match token {
'P' => Box::new(Player::new(id, position, renderer, 30.)),
'C' => Box::new(Coin::new(id, position, renderer, 20.)),
'K' => Box::new(Koopa::new(id, position, renderer, 30.)),
'S' => Box::new(StartBlock::new(id, position, renderer, 1.)),
'=' => Box::new(GroundBlockTop::new(id, position, renderer, 1.)),
'-' => Box::new(GroundBlockMid::new(id, position, renderer, 1.)),
'_' => Box::new(StoneBlock::new(id, position, renderer, 1.)),
'P' => Box::new(Player::new(index, position, renderer, 30.)),
'C' => Box::new(Coin::new(index, position, renderer, 20.)),
'K' => Box::new(Koopa::new(index, position, renderer, 30.)),
'S' => Box::new(StartBlock::new(index, position, renderer, 1.)),
'=' => Box::new(GroundBlockTop::new(index, position, renderer, 1.)),
'-' => Box::new(GroundBlockMid::new(index, position, renderer, 1.)),
'_' => Box::new(StoneBlock::new(index, position, renderer, 1.)),
_ => panic!("Actor not implemented for token!"),
}
}

#[inline]
pub fn handle_message(
curr_actor_id: i32,
curr_actor_id: ActorIndex,
actors: &mut ActorManager<Actor>,
viewport: &mut Viewport,
context: &mut Context,
Expand Down Expand Up @@ -107,6 +110,10 @@ pub fn handle_message(
#[inline]
pub fn resolve_collision(actor: &mut Actor, other: &ActorData, direction: CollisionSide) {
let data = actor.data();
let invalid_index = ActorIndex {
id: 0,
generation: 0,
};
if data.resolves_collisions {
while actor.collides_with(other) == Some(direction) {
let change = match direction {
Expand All @@ -117,17 +124,17 @@ pub fn resolve_collision(actor: &mut Actor, other: &ActorData, direction: Collis
};

actor.handle_message(&ActorMessage::ActorAction {
send_id: -1,
recv_id: -1,
send_id: invalid_index,
recv_id: invalid_index,
action: ActorAction::ChangePosition(change),
});
}

if direction == CollisionSide::Bottom {
let down_change = PositionChange::new().down(1);
actor.handle_message(&ActorMessage::ActorAction {
send_id: -1,
recv_id: -1,
send_id: invalid_index,
recv_id: invalid_index,
action: ActorAction::ChangePosition(down_change),
});
}
Expand All @@ -150,17 +157,17 @@ pub fn handle_collision(

if direction != 0 {
let response = ActorMessage::ActorAction {
send_id: other.id,
recv_id: actor.id,
send_id: other.index,
recv_id: actor.index,
action: ActorAction::Collision(other.actor_type, CollisionSide::from(direction)),
};
let other_msg = ActorMessage::ActorAction {
send_id: actor.id,
recv_id: other.id,
send_id: actor.index,
recv_id: other.index,
action: ActorAction::Collision(actor.actor_type, CollisionSide::from(rev_dir)),
};

(handler)(actor.id, actors, viewport, context, &response);
(handler)(actor.id, actors, viewport, context, &other_msg);
(handler)(actor.index, actors, viewport, context, &response);
(handler)(actor.index, actors, viewport, context, &other_msg);
}
}
21 changes: 13 additions & 8 deletions examples/mario/src/actors/coin.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use actions::{ActorAction, ActorData, ActorMessage, ActorType};
use mold2d::{
Actor, AnimatedSprite, BoundingBox, Collision, CollisionSide, Context, PositionChange,
Renderable, SpriteRectangle, Spritesheet, SpritesheetConfig, Viewport,
Actor, ActorIndex, ActorPosition, AnimatedSprite, BoundingBox, Collision, CollisionSide,
Context, PositionChange, Renderable, SpriteRectangle, Spritesheet, SpritesheetConfig, Viewport,
};
use sdl2::rect::Rect;
use sdl2::render::Renderer;
Expand All @@ -10,13 +10,18 @@ use std::error::Error;
const COIN_VALUE: i32 = 5;

pub struct Coin {
id: i32,
index: ActorIndex,
rect: SpriteRectangle,
animation: AnimatedSprite,
}

impl Coin {
pub fn new(id: i32, position: (i32, i32), renderer: &mut Renderer, fps: f64) -> Coin {
pub fn new(
index: ActorIndex,
position: ActorPosition,
renderer: &mut Renderer,
fps: f64,
) -> Coin {
let anim = Spritesheet::new(
SpritesheetConfig {
width: 32,
Expand All @@ -30,7 +35,7 @@ impl Coin {
let anims = anim.range(0, 8);

Coin {
id,
index,
rect: SpriteRectangle::new(position.0, position.1, 32, 32),
animation: AnimatedSprite::with_fps(anims, fps),
}
Expand All @@ -49,15 +54,15 @@ impl Actor for Coin {
// Update score and remove coin
ActorMessage::MultipleMessages(vec![
Box::new(ActorMessage::UpdateScore(COIN_VALUE)),
Box::new(ActorMessage::RemoveActor(self.data().id)),
Box::new(ActorMessage::RemoveActor(self.data().index)),
])
}
// Action when an enemy is thrown or kicked into item
ActorAction::DamageActor(_) => {
// Update score and remove coin
ActorMessage::MultipleMessages(vec![
Box::new(ActorMessage::UpdateScore(COIN_VALUE)),
Box::new(ActorMessage::RemoveActor(self.data().id)),
Box::new(ActorMessage::RemoveActor(self.data().index)),
])
}
_ => ActorMessage::None,
Expand Down Expand Up @@ -92,7 +97,7 @@ impl Actor for Coin {

fn data(&mut self) -> ActorData {
ActorData {
id: self.id,
index: self.index,
state: 0,
damage: 0,
collision_filter: 0b1111,
Expand Down
25 changes: 15 additions & 10 deletions examples/mario/src/actors/koopa.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use actions::{ActorAction, ActorData, ActorMessage, ActorType};
use mold2d::{
Actor, Animations, BoundingBox, CollisionSide, Context, Direction, PositionChange,
SpriteRectangle, Spritesheet, SpritesheetConfig, Vector2D, Viewport,
Actor, ActorIndex, ActorPosition, Animations, BoundingBox, CollisionSide, Context, Direction,
PositionChange, SpriteRectangle, Spritesheet, SpritesheetConfig, Vector2D, Viewport,
};
use sdl2::render::Renderer;
use std::error::Error;
Expand All @@ -27,7 +27,7 @@ pub const KOOPA_WIDTH: u32 = 30;
pub const KOOPA_HEIGHT: u32 = 60;

pub struct Koopa {
id: i32,
index: ActorIndex,
curr_state: KoopaState,
size: KoopaSize,
direction: Direction,
Expand All @@ -39,7 +39,12 @@ pub struct Koopa {
}

impl Koopa {
pub fn new(id: i32, position: (i32, i32), renderer: &mut Renderer, fps: f64) -> Koopa {
pub fn new(
index: ActorIndex,
position: ActorPosition,
renderer: &mut Renderer,
fps: f64,
) -> Koopa {
use self::KoopaSize::*;
use self::KoopaState::*;
use mold2d::sprite::Direction::*;
Expand Down Expand Up @@ -89,7 +94,7 @@ impl Koopa {
anims.add((Walking, Shell, Right), sanim.range(4, 5), cbbox);

Koopa {
id,
index,
curr_state: KoopaState::Walking,
size: KoopaSize::Upright,
direction: Direction::Left,
Expand Down Expand Up @@ -121,11 +126,11 @@ impl Actor for Koopa {
self.anims.map_bbox_mut(|bbox| bbox.apply_change(&change));
ActorMessage::None
}
DamageActor(_) => ActorMessage::RemoveActor(self.id),
DamageActor(_) => ActorMessage::RemoveActor(self.index),
CanBounce => {
// Respond with yes if size is upright
ActorMessage::ActorAction {
send_id: self.id,
send_id: self.index,
recv_id: send_id,
action: ActorAction::Bounce(
self.size == KoopaSize::Upright || self.curr_speed.x == 0.,
Expand Down Expand Up @@ -162,7 +167,7 @@ impl Actor for Koopa {
}
Collision(actor_type, side) if side & 0b1101 != 0 => {
let damage_message = ActorMessage::ActorAction {
send_id: self.id,
send_id: self.index,
recv_id: send_id,
action: ActorAction::DamageActor(0),
};
Expand All @@ -172,7 +177,7 @@ impl Actor for Koopa {
ActorType::Item => {
// Attempt to pick up item if kicked
ActorMessage::ActorAction {
send_id: self.id,
send_id: self.index,
recv_id: send_id,
action: ActorAction::DamageActor(0),
}
Expand Down Expand Up @@ -255,7 +260,7 @@ impl Actor for Koopa {

fn data(&mut self) -> ActorData {
ActorData {
id: self.id,
index: self.index,
state: self.curr_state as u32,
damage: 5,
resolves_collisions: true,
Expand Down
20 changes: 13 additions & 7 deletions examples/mario/src/actors/player.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use actions::{ActorAction, ActorData, ActorMessage, ActorType};
use mold2d::{
Actor, Animations, BoundingBox, CollisionSide, Context, Direction, Polygon, PositionChange,
Segment, SpriteRectangle, Spritesheet, SpritesheetConfig, Vector2D, Viewport,
Actor, ActorIndex, ActorPosition, Animations, BoundingBox, CollisionSide, Context, Direction,
Polygon, PositionChange, Segment, SpriteRectangle, Spritesheet, SpritesheetConfig, Vector2D,
Viewport,
};
use sdl2::pixels::Color;
use sdl2::render::Renderer;
Expand Down Expand Up @@ -30,7 +31,7 @@ pub enum PlayerSize {
}

pub struct Player {
id: i32,
index: ActorIndex,
curr_state: PlayerState,
direction: Direction,
size: PlayerSize,
Expand All @@ -44,7 +45,12 @@ pub struct Player {
}

impl Player {
pub fn new(id: i32, position: (i32, i32), renderer: &mut Renderer, fps: f64) -> Player {
pub fn new(
index: ActorIndex,
position: ActorPosition,
renderer: &mut Renderer,
fps: f64,
) -> Player {
use self::PlayerSize::*;
use self::PlayerState::*;
use mold2d::sprite::Direction::*;
Expand Down Expand Up @@ -105,7 +111,7 @@ impl Player {
anims.add((Crouching, Walking, Right), banim.range(10, 11), cbbox);

Player {
id,
index,
curr_state: PlayerState::Jumping,
direction: Direction::Right,
size: PlayerSize::Big,
Expand Down Expand Up @@ -168,7 +174,7 @@ impl Actor for Player {
ActorAction::Collision(ActorType::Enemy, CollisionSide::Bottom) => {
// Ask actor if it can bounce on it
ActorMessage::ActorAction {
send_id: self.id,
send_id: self.index,
recv_id: send_id,
action: ActorAction::CanBounce,
}
Expand Down Expand Up @@ -300,7 +306,7 @@ impl Actor for Player {

fn data(&mut self) -> ActorData {
ActorData {
id: self.id,
index: self.index,
state: self.curr_state as u32,
damage: 0,
resolves_collisions: true,
Expand Down
12 changes: 6 additions & 6 deletions examples/mario/src/views/game_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl View for GameView {
context.renderer.clear();

// render contained actors
for actor in self.actors.actors.values_mut() {
for actor in self.actors.values_mut() {
if self.viewport.rect_in_viewport(&actor.data().rect) {
actor.render(context, &mut self.viewport, elapsed)?;
}
Expand Down Expand Up @@ -101,13 +101,13 @@ impl View for GameView {
let window_rect = Rect::new(0, 0, context.window.width, context.window.height);
let viewport_clone = self.viewport.clone();
let mut quadtree = Quadtree::new(window_rect, &viewport_clone);
let mut keys = Vec::with_capacity(self.actors.actors.len());
let mut keys = Vec::with_capacity(self.actors.len());

for (key, actor) in &mut self.actors.actors {
for (key, actor) in &mut self.actors.iter_mut() {
let data = actor.data();

if self.viewport.rect_in_viewport(&data.rect) {
keys.push(key.clone());
keys.push(key);
quadtree.insert(data);
}
}
Expand All @@ -121,8 +121,8 @@ impl View for GameView {
// update the actor
let pos_change = actor.update(context, elapsed);
actor.handle_message(&ActorMessage::ActorAction {
send_id: data.id,
recv_id: data.id,
send_id: data.index,
recv_id: data.index,
action: ActorAction::ChangePosition(pos_change),
});

Expand Down
Loading

0 comments on commit 3c51845

Please sign in to comment.