flame_geom 0.2.3

  • Readme
  • Changelog
  • Example
  • Installing
  • 28

flame_geom #

Pub Test Discord

Complement Flame with APIs for handling shapes and geometric operations.

Overview #

This package makes heavy use of Flame's Position class and includes several geometric structures:

  • LineSegment: represents a finite line segment between two points in a 2D Euclidian space (or sometimes the whole line).
  • Circle: represents a circle (or sometimes a circumference) in 2D Euclidian space.
  • Rectangle: represents an axis-aligned rectangle in a 2D Euclidian space.
  • Overlapable: generic interface for shapes that can overlap one another.

If working with integer precision, it might also be useful to take a look at some integer versions of these classes:

  • IntPosition: similar to Position but for integers.
  • IntRect: similar to Rectangle but for integers.
  • IntBounds: similar to Overlapple but for integers.

Example #

The example folder contains a playground for you to render a Flame game using shapes computed by Flame Geom.

[0.2.3] #

  • Update to work with newer flame version

[0.2.2] #

  • Fix linting issues

[0.2.1] #

  • Better code formatting

[0.2.0] #

  • Improves overlapables to include more objects
  • Adds Renderable interface and an easy to use Flame component

[0.1.0] #

  • Adds better example and more tests

[0.0.2] #

  • Adds linter, tests, formatting and other fixes

[0.0.1] #

  • First release

example/lib/main.dart

import 'dart:ui';

import 'package:flame/game.dart';
import 'package:flame/position.dart';
import 'package:flame_geom/circle.dart';
import 'package:flame_geom/bounds.dart';
import 'package:flame_geom/shape_component.dart';
import 'package:flame_geom/rectangle.dart';
import 'package:flutter/material.dart' show Colors;
import 'package:flutter/widgets.dart' show runApp;

void main() => runApp(MyGame().widget);

class MyGame extends BaseGame {
  static Paint p1 = Paint()
    ..color = const Color(0xFFFF00FF)
    ..style = PaintingStyle.stroke;
  static Paint p2 = Paint()
    ..color = const Color(0xFFFFFF00)
    ..style = PaintingStyle.stroke;
  static Paint p3 = Paint()
    ..color = const Color(0xFF00FFFF)
    ..style = PaintingStyle.stroke
    ..strokeWidth = 4.0;

  int setup = 0;

  MyGame() {
    setup0();
  }

  void setup0() {
    final unitCircle = Circle(Position(10, 30), 100.0);
    final rectangle = Rectangle.fromLTWH(-100, -100, 200, 200);
    final points = rectangle
        .sides()
        .expand((e) => unitCircle.intersectLineSegment(e))
        .toList();

    components.clear();
    add(ShapeComponent.fromRenderable(p1, unitCircle));
    add(ShapeComponent.fromRenderable(p2, rectangle));
    add(ShapeComponent.fromPositions(p3, points));
  }

  void setup1() {
    final bounds = Bounds([
      Circle(Position(10, 30), 100.0),
      Circle(Position(40, -10), 70.0),
      Circle(Position(-20, 100), 22.0),
    ]);
    final rectangle = Rectangle.fromLTWH(-120, -150, 270, 120);

    final overlap = bounds.overlaps(rectangle);
    final paint = Paint()..color = overlap ? Colors.green : Colors.red;

    components.clear();
    add(ShapeComponent.fromRenderable(p1, bounds));
    add(ShapeComponent.fromRenderable(p2, rectangle));
    add(ShapeComponent.fromRenderable(paint, Rectangle.fromLTWH(0, 0, 10, 10)));
  }

  void setup2() {
    final bounds = Bounds([
      Circle(Position(190, 30), 100.0),
      Circle(Position(200, -10), 70.0),
      Circle(Position(120, 100), 22.0),
    ]);
    final rectangle = Rectangle.fromLTWH(-230, -150, 270, 120);

    final overlap = bounds.overlaps(rectangle);
    final paint = Paint()..color = overlap ? Colors.green : Colors.red;

    components.clear();
    add(ShapeComponent.fromRenderable(p1, bounds));
    add(ShapeComponent.fromRenderable(p2, rectangle));
    add(ShapeComponent.fromRenderable(paint, Rectangle.fromLTWH(0, 0, 10, 10)));
  }

  @override
  void render(Canvas canvas) {
    canvas.translate(size.width / 2, size.height / 2);
    super.render(canvas);
  }

  @override
  void onTap(int pointerId) {
    setup = (setup + 1) % 3;
    if (setup == 0) {
      setup0();
    } else if (setup == 1) {
      setup1();
    } else if (setup == 2) {
      setup2();
    }
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  flame_geom: ^0.2.3

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:flame_geom/bounds.dart';
import 'package:flame_geom/circle.dart';
import 'package:flame_geom/int_bounds.dart';
import 'package:flame_geom/int_position.dart';
import 'package:flame_geom/int_rect.dart';
import 'package:flame_geom/line_segment.dart';
import 'package:flame_geom/overlapable.dart';
import 'package:flame_geom/rectangle.dart';
import 'package:flame_geom/renderable.dart';
import 'package:flame_geom/renderer.dart';
import 'package:flame_geom/shape_component.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
28
Health:
Code health derived from static analysis. [more]
0
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
70
Overall:
Weighted score of the above. [more]
28
Learn more about scoring.

We analyzed this package on Jul 9, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.14
  • Flutter: 1.17.5

Analysis issues and suggestions

Fix dependencies in pubspec.yaml.

Running flutter pub upgrade failed with the following output:

ERR: The current Flutter SDK version is 1.17.5.
 
 Because flame ^0.23.0 depends on flutter_svg >=0.18.0 which requires Flutter SDK version >=1.18.0-6.0.pre <2.0.0, flame ^0.23.0 is forbidden.
 So, because flame_geom depends on flame ^0.23.0, version solving failed.

Maintenance issues and suggestions

No valid SDK. (-20 points)

The analysis could not detect a valid SDK that can use this package.

Make sure dartdoc successfully runs on your package's source files. (-10 points)

Dependencies were not resolved.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0