Copy project
This commit is contained in:
parent
ce7a467af7
commit
26a0fd884f
173 changed files with 157458 additions and 1 deletions
93
ad_astra/AdAstra/src/mediator/mediator.c
Normal file
93
ad_astra/AdAstra/src/mediator/mediator.c
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
#include "mediator.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "../hardware_access/hardware_access.h"
|
||||
#include "../objects/object_container/object_container.h"
|
||||
#include "../objects/event_generator/event_generator.h"
|
||||
#include "../objects/commands/commands.h"
|
||||
#include "../objects/ai/ai.h"
|
||||
#include "../driver/display/display.h"
|
||||
#include "../driver/infra/infra.h"
|
||||
#include "../driver/sleep/sleep.h"
|
||||
#include "../driver/redundant_storage/redundant_storage.h"
|
||||
|
||||
|
||||
#define TARGET_FRAME_DURATION 20 // ms
|
||||
#define DEATH_SCREEN_LENGTH 50 // frames
|
||||
#define SAVE_INTERVAL 50 // every x frames
|
||||
|
||||
static struct {
|
||||
uint8_t contrast;
|
||||
uint8_t framesSinceLastSave;
|
||||
uint8_t deathDownCounter;
|
||||
} state = {
|
||||
.contrast = 255
|
||||
};
|
||||
|
||||
static inline void saveGame() {
|
||||
if (++state.framesSinceLastSave > SAVE_INTERVAL && startSchedulingObjectsForSaving()) {
|
||||
scheduleNextObjectForSave(&state.contrast, sizeof(state.contrast));
|
||||
saveObjects();
|
||||
saveScheduledObjects();
|
||||
state.framesSinceLastSave = 0;
|
||||
}
|
||||
}
|
||||
|
||||
static bool frameFunction(uint8_t previousFrameTime) {
|
||||
disableWritingEEPROM();
|
||||
|
||||
if (spaceshipObject->as.spaceship.healthLoss >= MAX_HEALTH) {
|
||||
setDisplayContrast(state.contrast * (state.deathDownCounter / DEATH_SCREEN_LENGTH));
|
||||
if (state.deathDownCounter-- == 0) {
|
||||
invalidateEEPROM();
|
||||
return false;
|
||||
};
|
||||
}
|
||||
|
||||
handleCommands();
|
||||
handleAI();
|
||||
generateEvents();
|
||||
tickObjects(previousFrameTime);
|
||||
drawFrame();
|
||||
enableWritingEEPROM();
|
||||
saveGame();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static inline void createObjects() {
|
||||
state.deathDownCounter = DEATH_SCREEN_LENGTH;
|
||||
|
||||
if (isValid()) {
|
||||
loadNextObject(&state.contrast, sizeof(state.contrast));
|
||||
}
|
||||
setDisplayContrast(state.contrast);
|
||||
|
||||
initializeBackground();
|
||||
initializeObjectContainer();
|
||||
}
|
||||
|
||||
void setupConnections() {
|
||||
initializeHardwareAccess();
|
||||
initializeRedundantStorage();
|
||||
initializeInfra(addCommand);
|
||||
initializeDisplay(drawObjects);
|
||||
}
|
||||
|
||||
void startGame() {
|
||||
while (true) {
|
||||
createObjects();
|
||||
startFrameLoop(frameFunction, TARGET_FRAME_DURATION);
|
||||
}
|
||||
}
|
||||
|
||||
void changeDisplayContrast(int8_t by) {
|
||||
if (by < 0) {
|
||||
state.contrast = (state.contrast < -by) ? 0 : (state.contrast + by);
|
||||
} else {
|
||||
state.contrast = (state.contrast > 255 - by) ? 255 : (state.contrast + by);
|
||||
}
|
||||
|
||||
setDisplayContrast(state.contrast);
|
||||
}
|
||||
11
ad_astra/AdAstra/src/mediator/mediator.h
Normal file
11
ad_astra/AdAstra/src/mediator/mediator.h
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
#ifndef MEDIATOR_H
|
||||
#define MEDIATOR_H
|
||||
|
||||
#include <avr/io.h>
|
||||
|
||||
|
||||
void setupConnections();
|
||||
void startGame();
|
||||
void changeDisplayContrast(int8_t by);
|
||||
|
||||
#endif
|
||||
Loading…
Add table
Add a link
Reference in a new issue