Thing Reader #7

Merged
wesley merged 1 commits from thing-reader into main 2025-04-07 19:14:11 -04:00
8 changed files with 107 additions and 2 deletions

View File

@@ -12,4 +12,5 @@ pub enum LumpType {
Vertex,
Linedef,
Sidedef,
Thing,
}

View File

@@ -1,9 +1,11 @@
mod linedef;
mod lump;
mod sidedef;
mod thing;
mod vertex;
pub use linedef::LinedefLump;
pub use lump::{Lump, LumpType};
pub use sidedef::SidedefLump;
pub use thing::ThingLump;
pub use vertex::VertexLump;

19
src/lumps/thing.rs Normal file
View File

@@ -0,0 +1,19 @@
use crate::types::Thing;
pub struct ThingLump {
pub things: Vec<Thing>,
}
impl ThingLump {
pub fn get_num_things(&self) -> usize {
self.things.len()
}
pub fn get_all_things(&self) -> Vec<Thing> {
self.things.to_vec()
}
pub fn get_thing(&self, pos: usize) -> Thing {
self.things[pos].to_owned()
}
}

View File

@@ -193,3 +193,48 @@ pub fn get_num_sidedefs() {
assert_eq!(sidedef_lump.get_num_sidedefs(), 648);
}
#[test]
pub fn read_first_thing() {
use crate::types::Thing;
let wad_file = get_wad();
let thing_lump = wad_file.get_thing_lump(&wad_file.directory[7]);
let correct_thing = Thing {
x_position: 1056,
y_position: -3616,
angle: 90,
thing_type: 1,
flags: 7,
};
assert_eq!(thing_lump.get_thing(0), correct_thing);
}
#[test]
pub fn get_num_thing_lumps() {
use crate::lumps::{Lump, LumpType};
let mut thing_lumps: Vec<Lump> = Vec::new();
let wad_file = get_wad();
for entry in 0..wad_file.num_lumps as usize {
if let LumpType::Thing = wad_file.directory[entry].lump_type {
thing_lumps.push(wad_file.directory[entry].to_owned());
}
}
assert_eq!(thing_lumps.len(), 9);
}
#[test]
pub fn get_num_things() {
let wad_file = get_wad();
let thing_lump = wad_file.get_thing_lump(&wad_file.directory[7]);
assert_eq!(thing_lump.get_num_things(), 138);
}

View File

@@ -1,7 +1,9 @@
mod linedef;
mod sidedef;
mod thing;
mod vertex;
pub use linedef::Linedef;
pub use sidedef::Sidedef;
pub use thing::Thing;
pub use vertex::Vertex;

8
src/types/thing.rs Normal file
View File

@@ -0,0 +1,8 @@
#[derive(Clone, Debug, PartialEq)]
pub struct Thing {
pub x_position: i16,
pub y_position: i16,
pub angle: i16,
pub thing_type: i16,
pub flags: i16,
}

View File

@@ -45,6 +45,7 @@ impl WADFile {
"VERTEXES" => LumpType::Vertex,
"LINEDEFS" => LumpType::Linedef,
"SIDEDEFS" => LumpType::Sidedef,
"THINGS" => LumpType::Thing,
_ => LumpType::Unknown,
};
lump_dir.push(Lump {

View File

@@ -1,5 +1,5 @@
use crate::lumps::{LinedefLump, Lump, SidedefLump, VertexLump};
use crate::types::{Linedef, Sidedef, Vertex};
use crate::lumps::{LinedefLump, Lump, SidedefLump, ThingLump, VertexLump};
use crate::types::{Linedef, Sidedef, Thing, Vertex};
use crate::utils::read_ascii;
use crate::wad::WADFile;
use crate::wad::wadfile::read_i16_le;
@@ -85,4 +85,31 @@ impl WADFile {
SidedefLump { sidedefs }
}
pub fn get_thing_lump(&self, lump: &Lump) -> ThingLump {
let lump_offset = lump.offset as usize;
let lump_size = lump.size as usize;
let lump_data = &self.data[lump_offset..lump_offset + lump_size];
let lump_entries = lump_size / 10;
let mut things: Vec<Thing> = Vec::with_capacity(lump_entries);
for entry in 0..lump_entries {
let startpos = entry * 10;
let x_position = read_i16_le(&lump_data[startpos..startpos + 2]);
let y_position = read_i16_le(&lump_data[startpos + 2..startpos + 4]);
let angle = read_i16_le(&lump_data[startpos + 4..startpos + 6]);
let thing_type = read_i16_le(&lump_data[startpos + 6..startpos + 8]);
let flags = read_i16_le(&lump_data[startpos + 8..startpos + 10]);
things.push(Thing {
x_position,
y_position,
angle,
thing_type,
flags,
});
}
ThingLump { things }
}
}