Compare commits

...

2 Commits

Author SHA1 Message Date
9bbece1da4
看着还行 2024-05-22 23:16:06 +08:00
4321d0d9d3
我其实不太确定这玩意能不能用( 2024-05-22 23:11:44 +08:00
2 changed files with 81 additions and 1 deletions

View File

@ -206,6 +206,45 @@ impl PySR1PartData {
}
}
#[pyclass]
#[derive(Clone, Debug)]
#[pyo3(name = "SR1PartTypeArrayIterator_rs")]
pub struct PySR1PartDataIterator {
pub datas: Vec<(PySR1PartType, PySR1PartData)>,
pub index: usize,
}
#[pymethods]
impl PySR1PartDataIterator {
fn __next__(mut slf: PyRefMut<'_, Self>) -> Option<(PySR1PartType, PySR1PartData)> {
if slf.index < slf.datas.len() {
slf.index += 1;
Some(slf.datas[slf.index - 1].clone())
} else {
None
}
}
}
/// 用来存一堆 PartData 的数组
#[pyclass]
#[derive(Clone, Debug)]
#[pyo3(name = "SR1PartArray_rs")]
pub struct PySR1PartArray {
pub datas: Vec<(PySR1PartType, PySR1PartData)>,
pub part_list: SR1PartList,
}
#[pymethods]
impl PySR1PartArray {
fn __iter__(&self) -> PySR1PartDataIterator {
PySR1PartDataIterator {
datas: self.datas.clone(),
index: 0,
}
}
}
#[pyclass]
#[derive(Clone, Debug)]
#[pyo3(name = "SR1Ship_rs")]
@ -233,6 +272,48 @@ impl PySR1Ship {
}
}
fn parts(&self) -> PySR1PartArray {
let mut parts: Vec<(PySR1PartType, PySR1PartData)> = Vec::new();
for part_data in self.ship.parts.iter() {
if let Some(part_type) = self.part_list.get_part_type(&part_data.part_type_id) {
let part_type = PySR1PartType::new(part_type.clone());
let py_part_data = PySR1PartData::new(part_data.clone());
parts.push((part_type, py_part_data));
}
}
PySR1PartArray {
datas: parts,
part_list: self.part_list.clone(),
}
}
fn disconnected_parts(&self) -> Vec<PySR1PartArray> {
match self.ship.disconnected.as_ref() {
Some(parts) => {
if parts.is_empty() {
return Vec::new();
}
let mut result = Vec::with_capacity(parts.len());
for (part_group, _) in parts.iter() {
let mut part_list = Vec::with_capacity(part_group.len());
for part_data in part_group.iter() {
if let Some(part_type) = self.part_list.get_part_type(&part_data.part_type_id) {
let part_type = PySR1PartType::new(part_type.clone());
let py_part_data = PySR1PartData::new(part_data.clone());
part_list.push((part_type, py_part_data));
}
}
result.push(PySR1PartArray {
datas: part_list,
part_list: self.part_list.clone(),
});
}
result
}
None => Vec::new(),
}
}
#[getter]
fn get_img_pos(&self) -> (i64, i64, i64, i64) {
// -x, -y, +x, +y

View File

@ -881,7 +881,6 @@ impl SR1Ship {
fn write_data(data: &SR1Ship, save_status: &SaveStatus) -> String {
let mut writer: Writer<Cursor<Vec<u8>>> = Writer::new(Cursor::new(Vec::new()));
{
// ship attr
let mut ship_elem = BytesStart::new("Ship");