aboutsummaryrefslogtreecommitdiff
path: root/pixel-client/src/game.rs
diff options
context:
space:
mode:
Diffstat (limited to 'pixel-client/src/game.rs')
-rw-r--r--pixel-client/src/game.rs56
1 files changed, 48 insertions, 8 deletions
diff --git a/pixel-client/src/game.rs b/pixel-client/src/game.rs
index 7d8e466a..170e6a6e 100644
--- a/pixel-client/src/game.rs
+++ b/pixel-client/src/game.rs
@@ -16,7 +16,7 @@
*/
use crate::{
- helper::Vec2InterpolateExt,
+ helper::InterpolateExt,
render::{
misc::MiscTextures,
sprite::{Sprite, SpriteDraw},
@@ -42,6 +42,7 @@ pub struct Game {
tilemap: Tilemap,
collision_map: HashSet<IVec2>,
players: HashMap<PlayerID, Player>,
+ items_removed: Vec<Item>,
my_id: PlayerID,
camera_center: Vec2,
@@ -74,7 +75,9 @@ pub struct Player {
pub struct Item {
position: Vec2,
+ parent_position: Vec2,
kind: ItemIndex,
+ alive: f32,
progress: Option<(f32, bool)>,
}
@@ -90,6 +93,7 @@ impl Game {
movement_send_cooldown: 0.,
misc_textures: MiscTextures::init(layout),
item_sprites: Vec::new(),
+ items_removed: Vec::new(),
interacting: false,
score: Score::default(),
camera_center: Vec2::ZERO,
@@ -187,11 +191,25 @@ impl Game {
}
}
}
- PacketC::MoveItem { from, to } => *self.get_item(to) = self.get_item(from).take(),
+ PacketC::MoveItem { from, to } => {
+ let mut item = self.get_item(from).take();
+ if let Some(item) = &mut item {
+ item.parent_position = self.get_location_position(to);
+ }
+ *self.get_item(to) = item;
+ }
PacketC::SetItem { location, item } => {
- *self.get_item(location) = item.map(|kind| Item {
+ let position = self.get_location_position(location);
+ let slot = match location {
+ ItemLocation::Tile(pos) => &mut self.tiles.get_mut(&pos).unwrap().item,
+ ItemLocation::Player(pid) => &mut self.players.get_mut(&pid).unwrap().item,
+ };
+ self.items_removed.extend(slot.take());
+ *slot = item.map(|kind| Item {
kind,
- position: Vec2::ZERO,
+ parent_position: position,
+ alive: 0.,
+ position,
progress: None,
})
}
@@ -239,6 +257,12 @@ impl Game {
ItemLocation::Player(pid) => &mut self.players.get_mut(&pid).unwrap().item,
}
}
+ pub fn get_location_position(&self, location: ItemLocation) -> Vec2 {
+ match location {
+ ItemLocation::Tile(pos) => pos.as_vec2() + 0.5,
+ ItemLocation::Player(p) => self.players[&p].movement.position,
+ }
+ }
pub fn tick(&mut self, dt: f32, keyboard: &KeyboardState, packet_out: &mut VecDeque<PacketS>) {
let mut direction = IVec2::new(
@@ -289,14 +313,19 @@ impl Game {
for (_pid, player) in &mut self.players {
if let Some(item) = &mut player.item {
- item.position = player.movement.position
+ item.parent_position = player.movement.position;
+ item.tick(1., dt);
}
}
- for (pos, tile) in &mut self.tiles {
+ for (_pos, tile) in &mut self.tiles {
if let Some(item) = &mut tile.item {
- item.position = pos.as_vec2() + 0.5
+ item.tick(1., dt)
}
}
+ self.items_removed.retain_mut(|i| {
+ i.tick(0., dt);
+ i.alive > 0.01
+ })
}
pub fn draw(&self, ctx: &mut SpriteRenderer) {
@@ -315,12 +344,23 @@ impl Game {
item.draw(ctx, &self.item_sprites, &self.misc_textures)
}
}
+ for item in &self.items_removed {
+ item.draw(ctx, &self.item_sprites, &self.misc_textures)
+ }
}
}
impl Item {
+ pub fn tick(&mut self, alive: f32, dt: f32) {
+ self.position.exp_to(self.parent_position, dt * 20.);
+ self.alive.exp_to(alive, dt * 20.)
+ }
pub fn draw(&self, ctx: &mut SpriteRenderer, item_sprites: &[Sprite], misc: &MiscTextures) {
- ctx.draw_world(item_sprites[self.kind.0].at(self.position));
+ ctx.draw_world(
+ item_sprites[self.kind.0]
+ .at(self.position)
+ .alpha(self.alive),
+ );
if let Some((progress, warn)) = self.progress {
let (bg, fg) = if warn {
([100, 0, 0, 200], [255, 0, 0, 200])