Skip to content

Commit

Permalink
Add position system to deck spawning
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeDoctorDE committed Sep 16, 2024
1 parent a68578f commit 22ff5b3
Show file tree
Hide file tree
Showing 13 changed files with 113 additions and 105 deletions.
44 changes: 20 additions & 24 deletions api/lib/src/event/event.mapper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1976,7 +1976,7 @@ class ObjectsSpawnedMapper extends SubClassMapperBase<ObjectsSpawned> {
if (_instance == null) {
MapperContainer.globals.use(_instance = ObjectsSpawnedMapper._());
HybridWorldEventMapper.ensureInitialized().addSubMapper(_instance!);
GlobalVectorDefinitionMapper.ensureInitialized();
VectorDefinitionMapper.ensureInitialized();
GameObjectMapper.ensureInitialized();
}
return _instance!;
Expand All @@ -1985,16 +1985,16 @@ class ObjectsSpawnedMapper extends SubClassMapperBase<ObjectsSpawned> {
@override
final String id = 'ObjectsSpawned';

static GlobalVectorDefinition _$cell(ObjectsSpawned v) => v.cell;
static const Field<ObjectsSpawned, GlobalVectorDefinition> _f$cell =
Field('cell', _$cell);
static List<GameObject> _$objects(ObjectsSpawned v) => v.objects;
static const Field<ObjectsSpawned, List<GameObject>> _f$objects =
Field('objects', _$objects);
static String _$table(ObjectsSpawned v) => v.table;
static const Field<ObjectsSpawned, String> _f$table = Field('table', _$table);
static Map<VectorDefinition, List<GameObject>> _$objects(ObjectsSpawned v) =>
v.objects;
static const Field<ObjectsSpawned, Map<VectorDefinition, List<GameObject>>>
_f$objects = Field('objects', _$objects);

@override
final MappableFields<ObjectsSpawned> fields = const {
#cell: _f$cell,
#table: _f$table,
#objects: _f$objects,
};

Expand All @@ -2007,7 +2007,7 @@ class ObjectsSpawnedMapper extends SubClassMapperBase<ObjectsSpawned> {
HybridWorldEventMapper.ensureInitialized();

static ObjectsSpawned _instantiate(DecodingData data) {
return ObjectsSpawned(data.dec(_f$cell), data.dec(_f$objects));
return ObjectsSpawned(data.dec(_f$table), data.dec(_f$objects));
}

@override
Expand Down Expand Up @@ -2063,12 +2063,10 @@ extension ObjectsSpawnedValueCopy<$R, $Out>

abstract class ObjectsSpawnedCopyWith<$R, $In extends ObjectsSpawned, $Out>
implements HybridWorldEventCopyWith<$R, $In, $Out> {
GlobalVectorDefinitionCopyWith<$R, GlobalVectorDefinition,
GlobalVectorDefinition> get cell;
ListCopyWith<$R, GameObject, GameObjectCopyWith<$R, GameObject, GameObject>>
get objects;
MapCopyWith<$R, VectorDefinition, List<GameObject>,
ObjectCopyWith<$R, List<GameObject>, List<GameObject>>> get objects;
@override
$R call({GlobalVectorDefinition? cell, List<GameObject>? objects});
$R call({String? table, Map<VectorDefinition, List<GameObject>>? objects});
ObjectsSpawnedCopyWith<$R2, $In, $Out2> $chain<$R2, $Out2>(
Then<$Out2, $R2> t);
}
Expand All @@ -2082,22 +2080,20 @@ class _ObjectsSpawnedCopyWithImpl<$R, $Out>
late final ClassMapperBase<ObjectsSpawned> $mapper =
ObjectsSpawnedMapper.ensureInitialized();
@override
GlobalVectorDefinitionCopyWith<$R, GlobalVectorDefinition,
GlobalVectorDefinition>
get cell => $value.cell.copyWith.$chain((v) => call(cell: v));
@override
ListCopyWith<$R, GameObject, GameObjectCopyWith<$R, GameObject, GameObject>>
get objects => ListCopyWith($value.objects,
(v, t) => v.copyWith.$chain(t), (v) => call(objects: v));
MapCopyWith<$R, VectorDefinition, List<GameObject>,
ObjectCopyWith<$R, List<GameObject>, List<GameObject>>>
get objects => MapCopyWith($value.objects,
(v, t) => ObjectCopyWith(v, $identity, t), (v) => call(objects: v));
@override
$R call({GlobalVectorDefinition? cell, List<GameObject>? objects}) =>
$R call({String? table, Map<VectorDefinition, List<GameObject>>? objects}) =>
$apply(FieldCopyWithData({
if (cell != null) #cell: cell,
if (table != null) #table: table,
if (objects != null) #objects: objects
}));
@override
ObjectsSpawned $make(CopyWithData data) => ObjectsSpawned(
data.get(#cell, or: $value.cell), data.get(#objects, or: $value.objects));
data.get(#table, or: $value.table),
data.get(#objects, or: $value.objects));

@override
ObjectsSpawnedCopyWith<$R2, ObjectsSpawned, $Out2> $chain<$R2, $Out2>(
Expand Down
9 changes: 6 additions & 3 deletions api/lib/src/event/hybrid.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ final class BackgroundChanged extends HybridWorldEvent
@MappableClass()
final class ObjectsSpawned extends HybridWorldEvent
with ObjectsSpawnedMappable {
final GlobalVectorDefinition cell;
final List<GameObject> objects;
final String table;
final Map<VectorDefinition, List<GameObject>> objects;

ObjectsSpawned(this.cell, this.objects);
ObjectsSpawned(this.table, this.objects);
ObjectsSpawned.single(GlobalVectorDefinition cell, List<GameObject> objects)
: objects = {cell.position: objects},
table = cell.table;
}

@MappableClass()
Expand Down
14 changes: 7 additions & 7 deletions api/lib/src/event/process/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ bool isValidClientEvent(
0,
state
.getTableOrDefault(event.cell.table)
.getCell(event.cell.location)
.getCell(event.cell.position)
.objects
.length -
1) ??
true,
ShuffleCellRequest() => state
.getTableOrDefault(event.cell.table)
.cells
.containsKey(event.cell.location),
ObjectsSpawned() => event.objects.every((e) {
.containsKey(event.cell.position),
ObjectsSpawned() => event.objects.values.expand((e) => e).every((e) {
final figure =
assetManager.getPack(e.asset.namespace)?.getFigure(e.asset.id);
return figure != null &&
Expand All @@ -46,7 +46,7 @@ bool isValidClientEvent(
0,
state
.getTableOrDefault(event.cell.table)
.getCell(event.cell.location)
.getCell(event.cell.position)
.objects
.length -
1) ??
Expand All @@ -55,7 +55,7 @@ bool isValidClientEvent(
0,
state
.getTableOrDefault(event.cell.table)
.getCell(event.cell.location)
.getCell(event.cell.position)
.objects
.length -
1),
Expand Down Expand Up @@ -99,7 +99,7 @@ bool isValidClientEvent(
return (TeamLeft(channel, team), kAnyChannel);
case CellRollRequest():
final table = state.getTableOrDefault(event.cell.table);
var cell = table.getCell(event.cell.location);
var cell = table.getCell(event.cell.position);
final random = Random();
GameObject roll(GameObject object) {
final figure = assetManager
Expand All @@ -123,7 +123,7 @@ bool isValidClientEvent(
return (ObjectsChanged(event.cell, objects), kAnyChannel);
case ShuffleCellRequest():
final table = state.getTableOrDefault(event.cell.table);
final cell = table.cells[event.cell.location];
final cell = table.cells[event.cell.position];
if (cell == null) return null;
final positions = List<int>.generate(cell.objects.length, (i) => i)
..shuffle();
Expand Down
42 changes: 23 additions & 19 deletions api/lib/src/event/process/server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ bool isValidServerEvent(ServerWorldEvent event, WorldState state) =>
0,
state
.getTableOrDefault(event.cell.table)
.getCell(event.cell.location)
.getCell(event.cell.position)
.objects
.length -
1)),
Expand All @@ -30,7 +30,7 @@ bool isValidServerEvent(ServerWorldEvent event, WorldState state) =>
0,
state
.getTableOrDefault(event.cell.table)
.getCell(event.cell.location)
.getCell(event.cell.position)
.objects
.length -
1) ??
Expand All @@ -39,7 +39,7 @@ bool isValidServerEvent(ServerWorldEvent event, WorldState state) =>
0,
state
.getTableOrDefault(event.cell.table)
.getCell(event.cell.location)
.getCell(event.cell.position)
.objects
.length -
1),
Expand Down Expand Up @@ -98,31 +98,35 @@ WorldState? processServerEvent(
return state.copyWith(teamMembers: allMembers);
case ObjectsChanged():
return state.mapTableOrDefault(event.cell.table, (table) {
final cell = table.cells[event.cell.location] ?? TableCell();
final cell = table.cells[event.cell.position] ?? TableCell();
return table.copyWith.cells.replace(
event.cell.location, cell.copyWith(objects: event.objects));
event.cell.position, cell.copyWith(objects: event.objects));
});
case CellShuffled(positions: final positions):
return state.mapTableOrDefault(event.cell.table, (table) {
final cell = table.cells[event.cell.location] ?? TableCell();
final cell = table.cells[event.cell.position] ?? TableCell();
final objects = cell.objects;
final newObjects = List<GameObject>.from(objects);
for (var i = 0; i < positions.length; i++) {
newObjects[positions[i]] = objects[i];
}
return table.copyWith.cells.replace(
event.cell.location,
event.cell.position,
cell.copyWith(
objects: newObjects,
));
});
case BackgroundChanged():
return state.copyWith.table(background: event.background);
case ObjectsSpawned():
return state.mapTableOrDefault(event.cell.table, (table) {
final cell = table.getCell(event.cell.location);
return table.copyWith.cells.replace(event.cell.location,
cell.copyWith(objects: [...cell.objects, ...event.objects]));
return state.mapTableOrDefault(event.table, (table) {
var newTable = table;
for (final entry in event.objects.entries) {
final cell = newTable.cells[entry.key] ?? TableCell();
newTable = newTable.copyWith.cells
.replace(entry.key, cell.copyWith(objects: entry.value));
}
return newTable;
});
case ObjectsMoved():
return state.mapTableOrDefault(event.table, (table) {
Expand All @@ -148,19 +152,19 @@ WorldState? processServerEvent(
});
case CellHideChanged():
return state.mapTableOrDefault(event.cell.table, (table) {
final cell = table.cells[event.cell.location] ?? TableCell();
final cell = table.cells[event.cell.position] ?? TableCell();
final objectIndex = event.object;
if (objectIndex != null) {
final object = cell.objects[objectIndex];
return table.copyWith.cells.replace(
event.cell.location,
event.cell.position,
cell.copyWith.objects.replace(objectIndex,
object.copyWith(hidden: event.hide ?? !object.hidden)));
} else {
final hidden =
!(event.hide ?? cell.objects.firstOrNull?.hidden ?? false);
return table.copyWith.cells.replace(
event.cell.location,
event.cell.position,
cell.copyWith(
objects: cell.objects
.map((e) => e.copyWith(hidden: hidden))
Expand All @@ -170,28 +174,28 @@ WorldState? processServerEvent(
});
case CellItemsCleared():
return state.mapTableOrDefault(event.cell.table, (table) {
final cell = table.cells[event.cell.location] ?? TableCell();
final cell = table.cells[event.cell.position] ?? TableCell();
final objectIndex = event.object;
if (objectIndex != null) {
return table.copyWith.cells.replace(
event.cell.location, cell.copyWith.objects.removeAt(objectIndex));
event.cell.position, cell.copyWith.objects.removeAt(objectIndex));
} else {
return table.copyWith.cells.replace(
event.cell.location,
event.cell.position,
cell.copyWith(
objects: [],
));
}
});
case ObjectIndexChanged():
return state.mapTableOrDefault(event.cell.table, (table) {
final cell = table.cells[event.cell.location] ?? TableCell();
final cell = table.cells[event.cell.position] ?? TableCell();
final object = cell.objects[event.object];
final newObjects = List<GameObject>.from(cell.objects);
newObjects.removeAt(event.object);
newObjects.insert(event.index, object);
return table.copyWith.cells
.replace(event.cell.location, cell.copyWith(objects: newObjects));
.replace(event.cell.position, cell.copyWith(objects: newObjects));
});
case TeamChanged():
return state.copyWith.info.teams.put(event.name, event.team);
Expand Down
8 changes: 4 additions & 4 deletions api/lib/src/models/deck.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,22 @@ class DeckDefinition with DeckDefinitionMappable {
class FigureDeckDefinition with FigureDeckDefinitionMappable {
final String name;
final String? variation;
final VectorDefinition location;
final VectorDefinition position;

FigureDeckDefinition({
required this.name,
this.variation,
this.location = VectorDefinition.zero,
this.position = VectorDefinition.zero,
});
}

@MappableClass()
class BoardDeckDefinition with BoardDeckDefinitionMappable {
final String name;
final VectorDefinition location;
final VectorDefinition position;

BoardDeckDefinition({
required this.name,
this.location = VectorDefinition.zero,
this.position = VectorDefinition.zero,
});
}
Loading

0 comments on commit 22ff5b3

Please sign in to comment.