Skip to content

Commit

Permalink
add method vertices_scaled
Browse files Browse the repository at this point in the history
  • Loading branch information
whatthedev-eth committed Jul 21, 2023
1 parent 3fa743d commit d9fe26b
Showing 1 changed file with 89 additions and 1 deletion.
90 changes: 89 additions & 1 deletion src/enemy.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ struct Position {

trait PositionTrait {
// Returns the vertices of the enemy at given position
fn vertices(self: @Position) -> Span<Vec2>;
fn vertices(self: @Position) -> Span<Vec2>; // Position has unscaled members
fn vertices_scaled(self: @Position) -> Span<Vec2>; // Position has scaled members
}

impl PostionImpl of PositionTrait {
Expand Down Expand Up @@ -58,6 +59,34 @@ impl PostionImpl of PositionTrait {
vertices.append(Vec2 { x: FixedTrait::new_unscaled(*self.x + CAR_WIDTH, false), y: y1 });
vertices.span()
}

fn vertices_scaled(self: @Position) -> Span<Vec2> {
let mut vertices = ArrayTrait::new();
vertices
.append(
Vec2 {
x: FixedTrait::new(*self.x + CAR_WIDTH_SCALED, false),
y: FixedTrait::new(*self.y + CAR_HEIGHT_SCALED, false)
}
);

let x1 = if *self.x < CAR_WIDTH_SCALED {
FixedTrait::new(0, false)
} else {
FixedTrait::new(*self.x - CAR_WIDTH_SCALED, false)
};

let y1 = if *self.y < CAR_HEIGHT_SCALED {
FixedTrait::new(0, false)
} else {
FixedTrait::new(*self.y - CAR_HEIGHT_SCALED, false)
};

vertices.append(Vec2 { x: x1, y: FixedTrait::new(*self.y + CAR_HEIGHT_SCALED, false) });
vertices.append(Vec2 { x: x1, y: y1 });
vertices.append(Vec2 { x: FixedTrait::new(*self.x + CAR_WIDTH_SCALED, false), y: y1 });
vertices.span()
}
}

#[system]
Expand Down Expand Up @@ -498,4 +527,63 @@ mod tests {
Option::None(())
);
}

#[test]
#[available_gas(2000000)]
fn test_vertices_scaled() {
let position = Position { x: HUNDRED, y: HUNDRED };
let vertices = position.vertices_scaled();

assert_precise(
*(vertices.at(0).x),
(HUNDRED + CAR_WIDTH_SCALED).into(),
'invalid vertex_0',
Option::None(())
);
assert_precise(
*(vertices.at(0).y),
(HUNDRED + CAR_HEIGHT_SCALED).into(),
'invalid vertex_0',
Option::None(())
);

assert_precise(
*(vertices.at(1).x),
(HUNDRED - CAR_WIDTH_SCALED).into(),
'invalid vertex_1',
Option::None(())
);
assert_precise(
*(vertices.at(1).y),
(HUNDRED + CAR_HEIGHT_SCALED).into(),
'invalid vertex_1',
Option::None(())
);

assert_precise(
*(vertices.at(2).x),
(HUNDRED - CAR_WIDTH_SCALED).into(),
'invalid vertex_x2',
Option::None(())
);
assert_precise(
*(vertices.at(2).y),
(HUNDRED - CAR_HEIGHT_SCALED).into(),
'invalid vertex_y2',
Option::None(())
);

assert_precise(
*(vertices.at(3).x),
(HUNDRED + CAR_WIDTH_SCALED).into(),
'invalid vertex_3',
Option::None(())
);
assert_precise(
*(vertices.at(3).y),
(HUNDRED - CAR_HEIGHT_SCALED).into(),
'invalid vertex_3',
Option::None(())
);
}
}

0 comments on commit d9fe26b

Please sign in to comment.