-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminimal_game.dart
137 lines (118 loc) · 3.33 KB
/
minimal_game.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import 'dart:async';
import 'dart:math';
import 'package:flame/collisions.dart';
import 'package:flame/components.dart';
import 'package:flame/extensions.dart';
import 'package:flame/game.dart';
import 'package:flame_spatial_grid/flame_spatial_grid.dart';
import 'package:flutter/material.dart' hide Image, Draggable;
class MinimalGame extends FlameGame with HasSpatialGridFramework {
MinimalGame();
@override
FutureOr<void> onLoad() async {
final player = Player(position: Vector2(160, 190), isPrimary: true);
await initializeSpatialGrid(
cellSize: 50,
// debug: true,
activeRadius: const Size(2, 2),
unloadRadius: const Size(2, 2),
trackWindowSize: false,
trackedComponent: player,
cellBuilderNoMap: onBuildNewCell,
suspendedCellLifetime: const Duration(seconds: 30),
);
add(player);
for (var i = 0; i < 100; i++) {
add(Player(position: Vector2(i * 10.0, 20)));
}
add(FpsTextComponent());
return super.onLoad();
}
Future<void> onBuildNewCell(
Cell cell,
Component rootComponent,
Iterable<Rect> mapRectOnCell,
) async {
final random = Random();
final doCreation = random.nextBool();
if (doCreation) {
add(Player(position: cell.center)..currentCell = cell);
}
}
}
class Player extends PositionComponent
with HasGridSupport, HasPaint, CollisionCallbacks {
Player({super.position, bool? isPrimary}) : super(size: Vector2(10, 10)) {
_isPrimary = isPrimary ?? false;
paint.color = _isPrimary ? Colors.indigoAccent : Colors.brown;
_rect = Rect.fromLTWH(0, 0, size.x, size.y);
if (!_isPrimary) {
// debugMode = true;
}
boundingBox.collisionType =
boundingBox.defaultCollisionType = CollisionType.active;
boundingBox.parentSpeedGetter = () => _dtSpeed;
}
late final Rect _rect;
late final bool _isPrimary;
var _dtSpeed = 0.0;
@override
void render(Canvas canvas) {
canvas.drawRect(_rect, paint);
}
final speed = 80.0;
final vector = Vector2.zero();
double dtElapsed = 0;
final dtMax = 400;
@override
void update(double dt) {
dtElapsed++;
if (dtElapsed >= dtMax || _outOfBounds()) {
vector.setZero();
dtElapsed = 0;
}
if (vector.isZero()) {
_createNewVector();
}
final newSpeed = speed * dt;
if (newSpeed - _dtSpeed > 0.1) {
boundingBox.onParentSpeedChange();
}
_dtSpeed = newSpeed;
final newStep = vector * _dtSpeed;
if (!vector.isZero()) {
position.add(newStep);
}
super.update(dt);
}
void _createNewVector() {
final rand = Random();
var xSign = rand.nextBool() ? -1 : 1;
var ySign = rand.nextBool() ? -1 : 1;
if (position.x >= 900) {
xSign = -1;
} else if (position.x <= 0) {
xSign = 1;
}
if (position.y >= 500) {
ySign = -1;
} else if (position.y <= 0) {
ySign = 1;
}
final xValue = rand.nextDouble();
final yValue = rand.nextDouble();
vector.setValues(xValue * xSign, yValue * ySign);
}
bool _outOfBounds() =>
position.x <= 0 ||
position.y <= 0 ||
position.x >= 900 ||
position.y >= 500;
@override
void onCollision(Set<Vector2> intersectionPoints, PositionComponent other) {
if (other is Player) {
vector.setZero();
}
super.onCollision(intersectionPoints, other);
}
}