mirror of
				https://github.com/flame-engine/flame.git
				synced 2025-11-04 04:47:13 +08:00 
			
		
		
		
	move prepare canvas to position component
This commit is contained in:
		@ -12,9 +12,7 @@ abstract class Component {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
abstract class PositionComponent extends Component {
 | 
					abstract class PositionComponent extends Component {
 | 
				
			||||||
  double x = 0.0,
 | 
					  double x = 0.0, y = 0.0, angle = 0.0;
 | 
				
			||||||
      y = 0.0,
 | 
					 | 
				
			||||||
      angle = 0.0;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
  double angleBetween(PositionComponent c) {
 | 
					  double angleBetween(PositionComponent c) {
 | 
				
			||||||
    return (atan2(c.x - this.x, this.y - c.y) - PI / 2) % (2 * PI);
 | 
					    return (atan2(c.x - this.x, this.y - c.y) - PI / 2) % (2 * PI);
 | 
				
			||||||
@ -23,14 +21,18 @@ abstract class PositionComponent extends Component {
 | 
				
			|||||||
  double distance(PositionComponent c) {
 | 
					  double distance(PositionComponent c) {
 | 
				
			||||||
    return sqrt(pow(this.y - c.y, 2) + pow(this.x - c.x, 2));
 | 
					    return sqrt(pow(this.y - c.y, 2) + pow(this.x - c.x, 2));
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  void prepareCanvas(Canvas canvas) {
 | 
				
			||||||
 | 
					    canvas.translate(x, y);
 | 
				
			||||||
 | 
					    canvas.rotate(angle); // TODO: rotate around center
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
abstract class SpriteComponent extends PositionComponent {
 | 
					abstract class SpriteComponent extends PositionComponent {
 | 
				
			||||||
  double width, height;
 | 
					  double width, height;
 | 
				
			||||||
  Image image;
 | 
					  Image image;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  final Paint paint = new Paint()
 | 
					  final Paint paint = new Paint()..color = new Color(0xffffffff);
 | 
				
			||||||
    ..color = new Color(0xffffffff);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
  SpriteComponent.square(double size, String imagePath)
 | 
					  SpriteComponent.square(double size, String imagePath)
 | 
				
			||||||
      : this.rectangle(size, size, imagePath);
 | 
					      : this.rectangle(size, size, imagePath);
 | 
				
			||||||
@ -43,16 +45,11 @@ abstract class SpriteComponent extends PositionComponent {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  render(Canvas canvas) {
 | 
					  render(Canvas canvas) {
 | 
				
			||||||
    if (image != null) {
 | 
					    if (image != null) {
 | 
				
			||||||
      _prepareCanvas(canvas);
 | 
					      prepareCanvas(canvas);
 | 
				
			||||||
      _drawImage(canvas);
 | 
					      _drawImage(canvas);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  void _prepareCanvas(Canvas canvas) {
 | 
					 | 
				
			||||||
    canvas.translate(x, y);
 | 
					 | 
				
			||||||
    canvas.rotate(angle); // TODO: rotate around center
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  void _drawImage(Canvas canvas) {
 | 
					  void _drawImage(Canvas canvas) {
 | 
				
			||||||
    final Rect outputRect = new Rect.fromLTWH(0.0, 0.0, width, height);
 | 
					    final Rect outputRect = new Rect.fromLTWH(0.0, 0.0, width, height);
 | 
				
			||||||
    final Size imageSize =
 | 
					    final Size imageSize =
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user