Files
Luan Nico 06f5a4b4a8 chore: Update cicd.yml file on flame_3d to match main, rollback color changes (#3378)
Update cicd.yml file on flame_3d to match main.

Since we finally updated main, we should require no difference
whatsoever on this file anymore.

This will require downgrading all the color changes, which is fine. This
should never have been a part of flame_3d to begin with.

Files were "untouched" by checking out the exact version as they are in
main right now (will need to rebase flame_3d to main later). I had to
add a couple more files because the files on main had dependencies on
changes that are not yet rebased on flame_3d. These extra diffs should
disappear when I do the final rebase.
2024-12-11 14:26:29 -05:00

138 lines
3.3 KiB
Dart

import 'dart:math';
import 'package:flame/collisions.dart';
import 'package:flame/components.dart';
import 'package:flame/game.dart';
import 'package:flame/geometry.dart';
import 'package:flame/palette.dart';
import 'package:flutter/material.dart';
class RaycastExample extends FlameGame with HasCollisionDetection {
static const description = '''
In this example the raycast functionality is showcased. The circle moves around
and casts 10 rays and checks how far the nearest hitboxes are and naively moves
around trying not to hit them.
''';
Ray2? ray;
Ray2? reflection;
Vector2 origin = Vector2(250, 100);
Paint paint = Paint()..color = Colors.amber.withOpacity(0.6);
final speed = 100;
final inertia = 3.0;
final safetyDistance = 50;
final direction = Vector2(0, 1);
final velocity = Vector2.zero();
final random = Random();
static const numberOfRays = 10;
final List<Ray2> rays = [];
final List<RaycastResult<ShapeHitbox>> results = [];
late Path path;
@override
Future<void> onLoad() async {
final paint = BasicPalette.gray.paint()
..style = PaintingStyle.stroke
..strokeWidth = 2.0;
add(ScreenHitbox());
add(
CircleComponent(
position: Vector2(100, 100),
radius: 50,
paint: paint,
children: [CircleHitbox()],
),
);
add(
CircleComponent(
position: Vector2(150, 500),
radius: 50,
paint: paint,
children: [CircleHitbox()],
),
);
add(
RectangleComponent(
position: Vector2.all(300),
size: Vector2.all(100),
paint: paint,
children: [RectangleHitbox()],
),
);
add(
RectangleComponent(
position: Vector2.all(500),
size: Vector2(100, 200),
paint: paint,
children: [RectangleHitbox()],
),
);
add(
RectangleComponent(
position: Vector2(550, 200),
size: Vector2(200, 150),
paint: paint,
children: [RectangleHitbox()],
),
);
}
final _velocityModifier = Vector2.zero();
@override
void update(double dt) {
super.update(dt);
collisionDetection.raycastAll(
origin,
numberOfRays: numberOfRays,
rays: rays,
out: results,
);
velocity.scale(inertia);
for (final result in results) {
_velocityModifier
..setFrom(result.intersectionPoint!)
..sub(origin)
..normalize();
if (result.distance! < safetyDistance) {
_velocityModifier.negate();
} else if (random.nextDouble() < 0.2) {
velocity.add(_velocityModifier);
}
velocity.add(_velocityModifier);
}
velocity
..normalize()
..scale(speed * dt);
origin.add(velocity);
}
@override
void render(Canvas canvas) {
super.render(canvas);
renderResult(canvas, origin, results, paint);
}
void renderResult(
Canvas canvas,
Vector2 origin,
List<RaycastResult<ShapeHitbox>> results,
Paint paint,
) {
final originOffset = origin.toOffset();
for (final result in results) {
if (!result.isActive) {
continue;
}
final intersectionPoint = result.intersectionPoint!.toOffset();
canvas.drawLine(
originOffset,
intersectionPoint,
paint,
);
}
canvas.drawCircle(originOffset, 5, paint);
}
}