mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-02 20:13:50 +08:00
Adding more methods to Position and PositionComponent, and also more tests.
This commit is contained in:
@ -29,6 +29,26 @@ abstract class PositionComponent extends Component {
|
||||
double x = 0.0, y = 0.0, angle = 0.0;
|
||||
double width = 0.0, height = 0.0;
|
||||
|
||||
Position get position => new Position(x, y);
|
||||
void set position(Position position) {
|
||||
this.x = position.x;
|
||||
this.y = position.y;
|
||||
}
|
||||
|
||||
Position get size => new Position(width, height);
|
||||
void set size(Position size) {
|
||||
this.width = size.x;
|
||||
this.height = size.y;
|
||||
}
|
||||
|
||||
Rect get rect => new Rect.fromLTWH(x, y, width, height);
|
||||
void set rect(Rect rect) {
|
||||
this.x = rect.left;
|
||||
this.y = rect.top;
|
||||
this.width = rect.width;
|
||||
this.height = rect.height;
|
||||
}
|
||||
|
||||
double angleBetween(PositionComponent c) {
|
||||
return (atan2(c.x - this.x, this.y - c.y) - PI / 2) % (2 * PI);
|
||||
}
|
||||
@ -45,14 +65,6 @@ abstract class PositionComponent extends Component {
|
||||
canvas.rotate(angle);
|
||||
canvas.translate(-width/2, -height/2);
|
||||
}
|
||||
|
||||
Position toPosition() {
|
||||
return new Position(x, y);
|
||||
}
|
||||
|
||||
Rect toRect() {
|
||||
return new Rect.fromLTWH(x, y, width, height);
|
||||
}
|
||||
}
|
||||
|
||||
class SpriteComponent extends PositionComponent {
|
||||
|
||||
@ -41,6 +41,10 @@ class Position {
|
||||
return this;
|
||||
}
|
||||
|
||||
double dotProduct(Position p) {
|
||||
return this.x * p.x + this.y * p.y;
|
||||
}
|
||||
|
||||
double length() {
|
||||
return math.sqrt(math.pow(this.x, 2) + math.pow(this.y, 2));
|
||||
}
|
||||
@ -73,4 +77,8 @@ class Position {
|
||||
String toString() {
|
||||
return "($x, $y)";
|
||||
}
|
||||
|
||||
static ui.Rect rectFrom(Position topLeft, Position size) {
|
||||
return new ui.Rect.fromLTWH(topLeft.x, topLeft.y, size.x, size.y);
|
||||
}
|
||||
}
|
||||
|
||||
51
test/component_test.dart
Normal file
51
test/component_test.dart
Normal file
@ -0,0 +1,51 @@
|
||||
import 'package:test/test.dart';
|
||||
import 'dart:ui';
|
||||
|
||||
import 'package:flame/components/component.dart';
|
||||
import 'package:flame/position.dart';
|
||||
|
||||
void main() {
|
||||
group('component test', () {
|
||||
test('test get/set x/y or position', () {
|
||||
PositionComponent c = new SpriteComponent();
|
||||
c.x = 2.2;
|
||||
c.y = 3.4;
|
||||
expect(c.position.x, 2.2);
|
||||
expect(c.position.y, 3.4);
|
||||
|
||||
c.position = new Position(1.0, 0.0);
|
||||
expect(c.x, 1.0);
|
||||
expect(c.y, 0.0);
|
||||
});
|
||||
|
||||
test('test get/set widt/height or size', () {
|
||||
PositionComponent c = new SpriteComponent();
|
||||
c.width = 2.2;
|
||||
c.height = 3.4;
|
||||
expect(c.size.x, 2.2);
|
||||
expect(c.size.y, 3.4);
|
||||
|
||||
c.size = new Position(1.0, 0.0);
|
||||
expect(c.width, 1.0);
|
||||
expect(c.height, 0.0);
|
||||
});
|
||||
|
||||
test('test get/set rect', () {
|
||||
PositionComponent c = new SpriteComponent();
|
||||
c.x = 0.0;
|
||||
c.y = 1.0;
|
||||
c.width = 2.0;
|
||||
c.height = 2.0;
|
||||
expect(c.rect.left, 0.0);
|
||||
expect(c.rect.top, 1.0);
|
||||
expect(c.rect.width, 2.0);
|
||||
expect(c.rect.height, 2.0);
|
||||
|
||||
c.rect = new Rect.fromLTWH(10.0, 10.0, 1.0, 1.0);
|
||||
expect(c.x, 10.0);
|
||||
expect(c.y, 10.0);
|
||||
expect(c.width, 1.0);
|
||||
expect(c.height, 1.0);
|
||||
});
|
||||
});
|
||||
}
|
||||
@ -1,15 +1,40 @@
|
||||
import 'package:test/test.dart';
|
||||
import 'dart:math' as math;
|
||||
|
||||
import 'package:flame/position.dart';
|
||||
|
||||
void expectDouble(double d1, double d2) {
|
||||
expect((d1 - d2).abs() <= 0.0001, true);
|
||||
}
|
||||
|
||||
void main() {
|
||||
group('position test', () {
|
||||
test('test add', () {
|
||||
Position p = new Position(0.0, 5.0);
|
||||
Position p2 = p.add(new Position(5.0, 5.0));
|
||||
expect(p, p2);
|
||||
expect(p.x, 5.0);
|
||||
expect(p.y, 10.0);
|
||||
expectDouble(p.x, 5.0);
|
||||
expectDouble(p.y, 10.0);
|
||||
});
|
||||
|
||||
test('test clone', () {
|
||||
Position p = new Position(1.0, 0.0);
|
||||
Position clone = p.clone();
|
||||
|
||||
clone.times(2.0);
|
||||
expectDouble(p.x, 1.0);
|
||||
expectDouble(clone.x, 2.0);
|
||||
});
|
||||
|
||||
test('test rotate', () {
|
||||
Position p = new Position(1.0, 0.0).rotate(math.PI / 2);
|
||||
expectDouble(p.x, 0.0);
|
||||
expectDouble(p.y, 1.0);
|
||||
});
|
||||
|
||||
test('test length', () {
|
||||
Position p = new Position(3.0, 4.0);
|
||||
expectDouble(p.length(), 5.0);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user