Renamed code 'Monster' -> 'Actor'
This commit is contained in:
+5
-5
@@ -3,14 +3,14 @@ var frameCounter: Int = 0;
|
|||||||
var posCounter: Int = 0;
|
var posCounter: Int = 0;
|
||||||
|
|
||||||
fn onReady() {
|
fn onReady() {
|
||||||
loadMonsterSprite("monster", "assets/parvati.png", 32, 32);
|
loadSprite("zombie", "assets/parvati.png", 32, 32);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn onStep() {
|
fn onStep() {
|
||||||
frameCounter++;
|
frameCounter++;
|
||||||
|
|
||||||
if (frameCounter % 100 == 0) {
|
if (frameCounter % 100 == 0) {
|
||||||
spawnMonsterAt("monster", posCounter*50, posCounter*50);
|
spawnActorAt("zombie", posCounter*50, posCounter*50);
|
||||||
posCounter++;
|
posCounter++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -24,8 +24,8 @@ initScreen(1280, 720, "Hello raylib from Toy!");
|
|||||||
initLoop(onReady, onStep, onClose);
|
initLoop(onReady, onStep, onClose);
|
||||||
|
|
||||||
//test
|
//test
|
||||||
fn brains(monster: Opaque) {
|
fn brains(actor: Opaque) {
|
||||||
monster.setX(monster.x + 1);
|
actor.setX(actor.x + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
setMonsterStep(brains);
|
setActorStep(brains);
|
||||||
|
|||||||
+91
-86
@@ -12,41 +12,43 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
//sprites loaded from disk
|
//sprites loaded from disk
|
||||||
typedef struct MonsterSprite {
|
typedef struct SpriteData {
|
||||||
Texture2D texture;
|
Texture2D texture;
|
||||||
Rectangle rect;
|
Rectangle rect;
|
||||||
} MonsterSprite;
|
//TODO: animation
|
||||||
|
} SpriteData;
|
||||||
|
|
||||||
//Monsters loaded from scripts
|
//Actors loaded from scripts
|
||||||
typedef struct MonsterData {
|
typedef struct ActorData {
|
||||||
MonsterSprite* sprite;
|
SpriteData* sprite;
|
||||||
Vector2 position;
|
Vector2 position;
|
||||||
|
//TODO: animation
|
||||||
int health;
|
int health;
|
||||||
} MonsterData;
|
} ActorData;
|
||||||
|
|
||||||
//static storage
|
//static storage
|
||||||
static Toy_Table* spriteTable = NULL;
|
static Toy_Table* spriteTable = NULL;
|
||||||
static Toy_Array* monsterArray = NULL;
|
static Toy_Array* actorArray = NULL;
|
||||||
static Toy_Function* monsterStep = NULL;
|
static Toy_Function* actorStep = NULL;
|
||||||
|
|
||||||
//callbacks
|
//callbacks
|
||||||
static void loadMonsterSprite(Toy_VM* vm) {
|
static void loadSprite(Toy_VM* vm) {
|
||||||
//key, file, width, height -> null
|
//key, file, width, height -> null
|
||||||
|
|
||||||
if (!IsWindowReady()) {
|
if (!IsWindowReady()) {
|
||||||
fprintf(stderr, TOY_CC_ERROR "ERROR: Can't load monster sprites before the window has been initialized" TOY_CC_RESET "\n");
|
fprintf(stderr, TOY_CC_ERROR "ERROR: Can't load actor sprites before the window has been initialized" TOY_CC_RESET "\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
//check for initialization
|
//check for initialization
|
||||||
if (spriteTable == NULL || monsterArray == NULL) {
|
if (spriteTable == NULL || actorArray == NULL) {
|
||||||
fprintf(stderr, TOY_CC_ERROR "ERROR: Object pool for monster system hasn't been initialized" TOY_CC_RESET "\n");
|
fprintf(stderr, TOY_CC_ERROR "ERROR: Object pool for actor system hasn't been initialized" TOY_CC_RESET "\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
//check parameter count
|
//check parameter count
|
||||||
if (vm->stack->count < 4) {
|
if (vm->stack->count < 4) {
|
||||||
fprintf(stderr, TOY_CC_ERROR "ERROR: Not enough parameters found in 'loadMonsterSprite'" TOY_CC_RESET "\n");
|
fprintf(stderr, TOY_CC_ERROR "ERROR: Not enough parameters found in 'loadSprite'" TOY_CC_RESET "\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -57,13 +59,13 @@ static void loadMonsterSprite(Toy_VM* vm) {
|
|||||||
|
|
||||||
//check types
|
//check types
|
||||||
if (!TOY_VALUE_IS_STRING(file) || !TOY_VALUE_IS_INTEGER(width) || !TOY_VALUE_IS_INTEGER(height)) {
|
if (!TOY_VALUE_IS_STRING(file) || !TOY_VALUE_IS_INTEGER(width) || !TOY_VALUE_IS_INTEGER(height)) {
|
||||||
fprintf(stderr, TOY_CC_ERROR "ERROR: Bad parameter types found in 'loadMonsterSprite'" TOY_CC_RESET "\n");
|
fprintf(stderr, TOY_CC_ERROR "ERROR: Bad parameter types found in 'loadSprite'" TOY_CC_RESET "\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
//check for overwriting the key
|
//check for overwriting the key
|
||||||
if ( TOY_VALUE_IS_NULL(Toy_lookupTable(&spriteTable, key)) != true ) {
|
if ( TOY_VALUE_IS_NULL(Toy_lookupTable(&spriteTable, key)) != true ) {
|
||||||
fprintf(stderr, TOY_CC_ERROR "ERROR: Can't overwrite existing monster sprite key" TOY_CC_RESET "\n");
|
fprintf(stderr, TOY_CC_ERROR "ERROR: Can't overwrite existing actor sprite key" TOY_CC_RESET "\n");
|
||||||
Toy_freeValue(key);
|
Toy_freeValue(key);
|
||||||
Toy_freeValue(file);
|
Toy_freeValue(file);
|
||||||
Toy_freeValue(width);
|
Toy_freeValue(width);
|
||||||
@@ -72,7 +74,7 @@ static void loadMonsterSprite(Toy_VM* vm) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//create the sprite stored in the bucket
|
//create the sprite stored in the bucket
|
||||||
MonsterSprite* sprite = (MonsterSprite*)Toy_partitionBucket(&(vm->memoryBucket), sizeof(MonsterSprite));
|
SpriteData* sprite = (SpriteData*)Toy_partitionBucket(&(vm->memoryBucket), sizeof(SpriteData));
|
||||||
sprite->rect = (Rectangle){ 0, 0, TOY_VALUE_AS_INTEGER(width), TOY_VALUE_AS_INTEGER(height) };
|
sprite->rect = (Rectangle){ 0, 0, TOY_VALUE_AS_INTEGER(width), TOY_VALUE_AS_INTEGER(height) };
|
||||||
|
|
||||||
//load the texture from a file
|
//load the texture from a file
|
||||||
@@ -90,18 +92,18 @@ static void loadMonsterSprite(Toy_VM* vm) {
|
|||||||
Toy_insertTable(&spriteTable, key, TOY_OPAQUE_FROM_POINTER(sprite));
|
Toy_insertTable(&spriteTable, key, TOY_OPAQUE_FROM_POINTER(sprite));
|
||||||
}
|
}
|
||||||
|
|
||||||
static void spawnMonsterAt(Toy_VM* vm) {
|
static void spawnActorAt(Toy_VM* vm) {
|
||||||
//sprite, x, y -> void
|
//sprite, x, y -> void
|
||||||
|
|
||||||
//check for initialization
|
//check for initialization
|
||||||
if (spriteTable == NULL || monsterArray == NULL) {
|
if (spriteTable == NULL || actorArray == NULL) {
|
||||||
fprintf(stderr, TOY_CC_ERROR "ERROR: Object pool for monster system hasn't been initialized" TOY_CC_RESET "\n");
|
fprintf(stderr, TOY_CC_ERROR "ERROR: Object pool for actor system hasn't been initialized" TOY_CC_RESET "\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
//check parameter count
|
//check parameter count
|
||||||
if (vm->stack->count < 3) {
|
if (vm->stack->count < 3) {
|
||||||
fprintf(stderr, TOY_CC_ERROR "ERROR: Not enough parameters found in 'spawnMonsterAt'" TOY_CC_RESET "\n");
|
fprintf(stderr, TOY_CC_ERROR "ERROR: Not enough parameters found in 'spawnActorAt'" TOY_CC_RESET "\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -111,7 +113,7 @@ static void spawnMonsterAt(Toy_VM* vm) {
|
|||||||
|
|
||||||
//check types
|
//check types
|
||||||
if (!TOY_VALUE_IS_INTEGER(xpos) || !TOY_VALUE_IS_INTEGER(ypos)) {
|
if (!TOY_VALUE_IS_INTEGER(xpos) || !TOY_VALUE_IS_INTEGER(ypos)) {
|
||||||
fprintf(stderr, TOY_CC_ERROR "ERROR: Bad parameter types found in 'spawnMonsterAt'" TOY_CC_RESET "\n");
|
fprintf(stderr, TOY_CC_ERROR "ERROR: Bad parameter types found in 'spawnActorAt'" TOY_CC_RESET "\n");
|
||||||
Toy_freeValue(key);
|
Toy_freeValue(key);
|
||||||
Toy_freeValue(xpos);
|
Toy_freeValue(xpos);
|
||||||
Toy_freeValue(ypos);
|
Toy_freeValue(ypos);
|
||||||
@@ -123,7 +125,7 @@ static void spawnMonsterAt(Toy_VM* vm) {
|
|||||||
if (TOY_VALUE_IS_NULL(spriteValue)) {
|
if (TOY_VALUE_IS_NULL(spriteValue)) {
|
||||||
Toy_String* string = Toy_stringifyValue(&(vm->memoryBucket), key);
|
Toy_String* string = Toy_stringifyValue(&(vm->memoryBucket), key);
|
||||||
char* cstr = Toy_getStringRaw(string);
|
char* cstr = Toy_getStringRaw(string);
|
||||||
fprintf(stderr, TOY_CC_ERROR "ERROR: Can't spawn a monster with a non-existant sprite '%s'" TOY_CC_RESET "\n", cstr);
|
fprintf(stderr, TOY_CC_ERROR "ERROR: Can't spawn a actor with a non-existant sprite '%s'" TOY_CC_RESET "\n", cstr);
|
||||||
free(cstr);
|
free(cstr);
|
||||||
Toy_freeString(string);
|
Toy_freeString(string);
|
||||||
Toy_freeValue(key);
|
Toy_freeValue(key);
|
||||||
@@ -133,52 +135,52 @@ static void spawnMonsterAt(Toy_VM* vm) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//expand the array if needed
|
//expand the array if needed
|
||||||
if (monsterArray->count == monsterArray->capacity) {
|
if (actorArray->count == actorArray->capacity) {
|
||||||
monsterArray = Toy_resizeArray(monsterArray, monsterArray->capacity * TOY_ARRAY_EXPANSION_RATE);
|
actorArray = Toy_resizeArray(actorArray, actorArray->capacity * TOY_ARRAY_EXPANSION_RATE);
|
||||||
//set the new entries to null values
|
//set the new entries to null values
|
||||||
for (unsigned int i = monsterArray->count; i < monsterArray->capacity; i++) {
|
for (unsigned int i = actorArray->count; i < actorArray->capacity; i++) {
|
||||||
monsterArray->data[i] = TOY_VALUE_FROM_NULL();
|
actorArray->data[i] = TOY_VALUE_FROM_NULL();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//find an existing spot for the new monster, overwriting a dead one
|
//find an existing spot for the new actor, overwriting a dead one
|
||||||
MonsterData* newMonsterPtr = NULL;
|
ActorData* newActorPtr = NULL;
|
||||||
for (unsigned int i = 0; i < monsterArray->count; i++) {
|
for (unsigned int i = 0; i < actorArray->count; i++) {
|
||||||
MonsterData* mData = (MonsterData*)TOY_VALUE_AS_OPAQUE(monsterArray->data[i]);
|
ActorData* mData = (ActorData*)TOY_VALUE_AS_OPAQUE(actorArray->data[i]);
|
||||||
if (mData->health <= 0) { //if this monster is dead, steal the slot
|
if (mData->health <= 0) { //if this actor is dead, steal the slot
|
||||||
newMonsterPtr = mData;
|
newActorPtr = mData;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//if no dead monsters were found, make a new slot
|
//if no dead actors were found, make a new slot
|
||||||
if (newMonsterPtr == NULL) {
|
if (newActorPtr == NULL) {
|
||||||
newMonsterPtr = (MonsterData*)Toy_partitionBucket(&(vm->memoryBucket), sizeof(MonsterData));
|
newActorPtr = (ActorData*)Toy_partitionBucket(&(vm->memoryBucket), sizeof(ActorData));
|
||||||
monsterArray->data[monsterArray->count++] = TOY_OPAQUE_FROM_POINTER(newMonsterPtr);
|
actorArray->data[actorArray->count++] = TOY_OPAQUE_FROM_POINTER(newActorPtr);
|
||||||
}
|
}
|
||||||
|
|
||||||
//finally, store the new monster's data
|
//finally, store the new actor's data
|
||||||
(*newMonsterPtr) = (MonsterData){
|
(*newActorPtr) = (ActorData){
|
||||||
.sprite = (MonsterSprite*)(TOY_VALUE_AS_OPAQUE(spriteValue)),
|
.sprite = (SpriteData*)(TOY_VALUE_AS_OPAQUE(spriteValue)),
|
||||||
.position = { TOY_VALUE_AS_INTEGER(xpos), TOY_VALUE_AS_INTEGER(ypos) },
|
.position = { TOY_VALUE_AS_INTEGER(xpos), TOY_VALUE_AS_INTEGER(ypos) },
|
||||||
.health = 10,
|
.health = 10,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
static void setMonsterStep(Toy_VM* vm) {
|
static void setActorStep(Toy_VM* vm) {
|
||||||
Toy_Value value = Toy_popStack(&vm->stack);
|
Toy_Value value = Toy_popStack(&vm->stack);
|
||||||
|
|
||||||
if (!TOY_VALUE_IS_FUNCTION(value) && !TOY_VALUE_IS_NULL(value)) {
|
if (!TOY_VALUE_IS_FUNCTION(value) && !TOY_VALUE_IS_NULL(value)) {
|
||||||
fprintf(stderr, TOY_CC_ERROR "ERROR: Bad argument type found in 'setMonsterStep', exiting" TOY_CC_RESET "\n");
|
fprintf(stderr, TOY_CC_ERROR "ERROR: Bad argument type found in 'setActorStep', exiting" TOY_CC_RESET "\n");
|
||||||
exit(-1);
|
exit(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (TOY_VALUE_IS_FUNCTION(value)) {
|
if (TOY_VALUE_IS_FUNCTION(value)) {
|
||||||
if (TOY_VALUE_AS_FUNCTION(value)->type != TOY_FUNCTION_CUSTOM) {
|
if (TOY_VALUE_AS_FUNCTION(value)->type != TOY_FUNCTION_CUSTOM) {
|
||||||
fprintf(stderr, TOY_CC_ERROR "ERROR: Bad function found in 'setMonsterStep', exiting (only allows custom functions or null)" TOY_CC_RESET "\n");
|
fprintf(stderr, TOY_CC_ERROR "ERROR: Bad function found in 'setActorStep', exiting (only allows custom functions or null)" TOY_CC_RESET "\n");
|
||||||
exit(-1);
|
exit(-1);
|
||||||
}
|
}
|
||||||
monsterStep = TOY_VALUE_AS_FUNCTION(value);
|
actorStep = TOY_VALUE_AS_FUNCTION(value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -189,15 +191,17 @@ typedef struct CallbackPairs {
|
|||||||
} CallbackPairs;
|
} CallbackPairs;
|
||||||
|
|
||||||
static CallbackPairs callbackPairs[] = {
|
static CallbackPairs callbackPairs[] = {
|
||||||
{"loadMonsterSprite", loadMonsterSprite},
|
{"loadSprite", loadSprite},
|
||||||
{"spawnMonsterAt", spawnMonsterAt},
|
// {"unloadSprite", unloadSprite},
|
||||||
{"setMonsterStep", setMonsterStep},
|
{"spawnActorAt", spawnActorAt},
|
||||||
|
// {"despawnActor", despawnActor},
|
||||||
|
{"setActorStep", setActorStep},
|
||||||
|
|
||||||
{NULL, NULL},
|
{NULL, NULL},
|
||||||
};
|
};
|
||||||
|
|
||||||
//exposed
|
//exposed
|
||||||
void initMonsterAPI(Toy_VM* vm) {
|
void initActorAPI(Toy_VM* vm) {
|
||||||
if (vm == NULL || vm->scope == NULL || vm->memoryBucket == NULL) {
|
if (vm == NULL || vm->scope == NULL || vm->memoryBucket == NULL) {
|
||||||
fprintf(stderr, TOY_CC_ERROR "ERROR: Can't initialize standard library, exiting\n" TOY_CC_RESET);
|
fprintf(stderr, TOY_CC_ERROR "ERROR: Can't initialize standard library, exiting\n" TOY_CC_RESET);
|
||||||
exit(-1);
|
exit(-1);
|
||||||
@@ -215,33 +219,33 @@ void initMonsterAPI(Toy_VM* vm) {
|
|||||||
|
|
||||||
//make the local storage (these use malloc(), don't they?)
|
//make the local storage (these use malloc(), don't they?)
|
||||||
spriteTable = Toy_allocateTable(TOY_TABLE_INITIAL_CAPACITY);
|
spriteTable = Toy_allocateTable(TOY_TABLE_INITIAL_CAPACITY);
|
||||||
monsterArray = Toy_resizeArray(NULL, TOY_ARRAY_INITIAL_CAPACITY);
|
actorArray = Toy_resizeArray(NULL, TOY_ARRAY_INITIAL_CAPACITY);
|
||||||
}
|
}
|
||||||
|
|
||||||
void freeMonsterAPI(Toy_VM* vm) {
|
void freeActorAPI(Toy_VM* vm) {
|
||||||
(void)vm;
|
(void)vm;
|
||||||
|
|
||||||
//free the GL textures
|
//free the GL textures
|
||||||
for (unsigned int i = 0; i < spriteTable->capacity; i++) {
|
for (unsigned int i = 0; i < spriteTable->capacity; i++) {
|
||||||
if (TOY_VALUE_IS_OPAQUE(spriteTable->data[i].value)) {
|
if (TOY_VALUE_IS_OPAQUE(spriteTable->data[i].value)) {
|
||||||
UnloadTexture(((MonsterSprite*)TOY_VALUE_AS_OPAQUE(spriteTable->data[i].value))->texture);
|
UnloadTexture(((SpriteData*)TOY_VALUE_AS_OPAQUE(spriteTable->data[i].value))->texture);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Toy_freeTable(spriteTable);
|
Toy_freeTable(spriteTable);
|
||||||
spriteTable = NULL;
|
spriteTable = NULL;
|
||||||
|
|
||||||
monsterArray = Toy_resizeArray(monsterArray, 0);
|
actorArray = Toy_resizeArray(actorArray, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void processMonsterStep(Toy_VM* vm) {
|
void processActorStep(Toy_VM* vm) {
|
||||||
//check for initialization
|
//check for initialization
|
||||||
if (spriteTable == NULL || monsterArray == NULL) {
|
if (spriteTable == NULL || actorArray == NULL) {
|
||||||
fprintf(stderr, TOY_CC_ERROR "ERROR: Object pool for monster system hasn't been initialized" TOY_CC_RESET "\n");
|
fprintf(stderr, TOY_CC_ERROR "ERROR: Object pool for actor system hasn't been initialized" TOY_CC_RESET "\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (monsterStep == NULL) {
|
if (actorStep == NULL) {
|
||||||
return; //no-op
|
return; //no-op
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -250,7 +254,7 @@ void processMonsterStep(Toy_VM* vm) {
|
|||||||
//bind a sub-vm
|
//bind a sub-vm
|
||||||
Toy_VM subVM;
|
Toy_VM subVM;
|
||||||
Toy_inheritVM(vm, &subVM);
|
Toy_inheritVM(vm, &subVM);
|
||||||
Toy_bindVM(&subVM, monsterStep->bytecode.code, monsterStep->bytecode.parentScope);
|
Toy_bindVM(&subVM, actorStep->bytecode.code, actorStep->bytecode.parentScope);
|
||||||
|
|
||||||
//paramAddr is relative to the data section, and is followed by the param type
|
//paramAddr is relative to the data section, and is followed by the param type
|
||||||
unsigned int paramAddr = ((unsigned int*)(subVM.code + subVM.paramAddr))[0];
|
unsigned int paramAddr = ((unsigned int*)(subVM.code + subVM.paramAddr))[0];
|
||||||
@@ -260,13 +264,13 @@ void processMonsterStep(Toy_VM* vm) {
|
|||||||
const char* cstr = ((char*)(subVM.code + subVM.dataAddr)) + paramAddr;
|
const char* cstr = ((char*)(subVM.code + subVM.dataAddr)) + paramAddr;
|
||||||
Toy_String* name = Toy_toStringLength(&subVM.memoryBucket, cstr, strlen(cstr));
|
Toy_String* name = Toy_toStringLength(&subVM.memoryBucket, cstr, strlen(cstr));
|
||||||
|
|
||||||
//load each valid monster and process them one at a time
|
//load each valid actor and process them one at a time
|
||||||
for (unsigned int i = 0; i < monsterArray->count; i++) {
|
for (unsigned int i = 0; i < actorArray->count; i++) {
|
||||||
MonsterData* monster = (MonsterData*)TOY_VALUE_AS_OPAQUE(monsterArray->data[i]);
|
ActorData* actor = (ActorData*)TOY_VALUE_AS_OPAQUE(actorArray->data[i]);
|
||||||
if (monster->health > 0) {
|
if (actor->health > 0) {
|
||||||
subVM.scope = Toy_pushScope(&subVM.memoryBucket, subVM.scope);
|
subVM.scope = Toy_pushScope(&subVM.memoryBucket, subVM.scope);
|
||||||
|
|
||||||
Toy_declareScope(subVM.scope, name, paramType, Toy_copyValue(&subVM.memoryBucket, monsterArray->data[i]), true);
|
Toy_declareScope(subVM.scope, name, paramType, Toy_copyValue(&subVM.memoryBucket, actorArray->data[i]), true);
|
||||||
Toy_runVM(&subVM);
|
Toy_runVM(&subVM);
|
||||||
|
|
||||||
subVM.scope = Toy_popScope(subVM.scope);
|
subVM.scope = Toy_popScope(subVM.scope);
|
||||||
@@ -276,84 +280,85 @@ void processMonsterStep(Toy_VM* vm) {
|
|||||||
Toy_freeVM(&subVM);
|
Toy_freeVM(&subVM);
|
||||||
}
|
}
|
||||||
|
|
||||||
void drawMonsters(Toy_VM* vm) {
|
void drawActors(Toy_VM* vm) {
|
||||||
(void)vm;
|
(void)vm;
|
||||||
|
|
||||||
//check for initialization
|
//check for initialization
|
||||||
if (spriteTable == NULL || monsterArray == NULL) {
|
if (spriteTable == NULL || actorArray == NULL) {
|
||||||
fprintf(stderr, TOY_CC_ERROR "ERROR: Object pool for monster system hasn't been initialized" TOY_CC_RESET "\n");
|
fprintf(stderr, TOY_CC_ERROR "ERROR: Object pool for actor system hasn't been initialized" TOY_CC_RESET "\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (unsigned int i = 0; i < monsterArray->count; i++) {
|
for (unsigned int i = 0; i < actorArray->count; i++) {
|
||||||
MonsterData* monster = (MonsterData*)TOY_VALUE_AS_OPAQUE(monsterArray->data[i]);
|
ActorData* actor = (ActorData*)TOY_VALUE_AS_OPAQUE(actorArray->data[i]);
|
||||||
|
|
||||||
if (monster->health > 0) {
|
if (actor->health > 0) {
|
||||||
DrawTextureRec(monster->sprite->texture, monster->sprite->rect, monster->position, WHITE);
|
DrawTextureRec(actor->sprite->texture, actor->sprite->rect, actor->position, WHITE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void attr_monsterSetX(Toy_VM* vm) {
|
static void attr_actorSetX(Toy_VM* vm) {
|
||||||
Toy_Value compound = Toy_popStack(&vm->stack);
|
Toy_Value compound = Toy_popStack(&vm->stack);
|
||||||
Toy_Value x = Toy_popStack(&vm->stack);
|
Toy_Value x = Toy_popStack(&vm->stack);
|
||||||
|
|
||||||
if (!TOY_VALUE_IS_INTEGER(x)) {
|
if (!TOY_VALUE_IS_INTEGER(x)) {
|
||||||
fprintf(stderr, TOY_CC_ERROR "ERROR: Bad argument type in MonsterData.setX() (expected 'Int', found '%s')" TOY_CC_RESET "\n", Toy_getValueTypeAsCString(x.type));
|
fprintf(stderr, TOY_CC_ERROR "ERROR: Bad argument type in ActorData.setX() (expected 'Int', found '%s')" TOY_CC_RESET "\n", Toy_getValueTypeAsCString(x.type));
|
||||||
}
|
}
|
||||||
|
|
||||||
MonsterData* monster = (MonsterData*)TOY_VALUE_AS_OPAQUE(compound);
|
ActorData* actor = (ActorData*)TOY_VALUE_AS_OPAQUE(compound);
|
||||||
|
|
||||||
monster->position.x = TOY_VALUE_AS_INTEGER(x);
|
actor->position.x = TOY_VALUE_AS_INTEGER(x);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void attr_monsterSetY(Toy_VM* vm) {
|
static void attr_actorSetY(Toy_VM* vm) {
|
||||||
Toy_Value compound = Toy_popStack(&vm->stack);
|
Toy_Value compound = Toy_popStack(&vm->stack);
|
||||||
Toy_Value y = Toy_popStack(&vm->stack);
|
Toy_Value y = Toy_popStack(&vm->stack);
|
||||||
|
|
||||||
if (!TOY_VALUE_IS_INTEGER(y)) {
|
if (!TOY_VALUE_IS_INTEGER(y)) {
|
||||||
fprintf(stderr, TOY_CC_ERROR "ERROR: Bad argument type in MonsterData.setY() (expected 'Int', found '%s')" TOY_CC_RESET "\n", Toy_getValueTypeAsCString(y.type));
|
fprintf(stderr, TOY_CC_ERROR "ERROR: Bad argument type in ActorData.setY() (expected 'Int', found '%s')" TOY_CC_RESET "\n", Toy_getValueTypeAsCString(y.type));
|
||||||
}
|
}
|
||||||
|
|
||||||
MonsterData* monster = (MonsterData*)TOY_VALUE_AS_OPAQUE(compound);
|
ActorData* actor = (ActorData*)TOY_VALUE_AS_OPAQUE(compound);
|
||||||
|
|
||||||
monster->position.y = TOY_VALUE_AS_INTEGER(y);
|
actor->position.y = TOY_VALUE_AS_INTEGER(y);
|
||||||
}
|
}
|
||||||
|
|
||||||
Toy_Value handleMonsterAttributes(Toy_VM* vm, Toy_Value compound, Toy_Value attribute) {
|
//opaque handler
|
||||||
|
Toy_Value handleActorAttributes(Toy_VM* vm, Toy_Value compound, Toy_Value attribute) {
|
||||||
//check for initialization
|
//check for initialization
|
||||||
if (spriteTable == NULL || monsterArray == NULL) {
|
if (spriteTable == NULL || actorArray == NULL) {
|
||||||
fprintf(stderr, TOY_CC_ERROR "ERROR: Object pool for monster system hasn't been initialized" TOY_CC_RESET "\n");
|
fprintf(stderr, TOY_CC_ERROR "ERROR: Object pool for actor system hasn't been initialized" TOY_CC_RESET "\n");
|
||||||
return TOY_VALUE_FROM_NULL();
|
return TOY_VALUE_FROM_NULL();
|
||||||
}
|
}
|
||||||
|
|
||||||
//check for correct types
|
//check for correct types
|
||||||
if (!TOY_VALUE_IS_OPAQUE(compound) || !TOY_VALUE_IS_STRING(attribute) || TOY_VALUE_AS_STRING(attribute)->info.type != TOY_STRING_LEAF) {
|
if (!TOY_VALUE_IS_OPAQUE(compound) || !TOY_VALUE_IS_STRING(attribute) || TOY_VALUE_AS_STRING(attribute)->info.type != TOY_STRING_LEAF) {
|
||||||
fprintf(stderr, TOY_CC_ERROR "ERROR: Bad parameters found in 'handleMonsterAttributes'" TOY_CC_RESET "\n");
|
fprintf(stderr, TOY_CC_ERROR "ERROR: Bad parameters found in 'handleActorAttributes'" TOY_CC_RESET "\n");
|
||||||
return TOY_VALUE_FROM_NULL(); //do not free the params here
|
return TOY_VALUE_FROM_NULL(); //do not free the params here
|
||||||
}
|
}
|
||||||
|
|
||||||
MonsterData* monster = (MonsterData*)TOY_VALUE_AS_OPAQUE(compound);
|
ActorData* actor = (ActorData*)TOY_VALUE_AS_OPAQUE(compound);
|
||||||
|
|
||||||
|
|
||||||
if (TOY_VALUE_AS_STRING(attribute)->info.length == 1 && strncmp(TOY_VALUE_AS_STRING(attribute)->leaf.data, "x", 1) == 0) {
|
if (TOY_VALUE_AS_STRING(attribute)->info.length == 1 && strncmp(TOY_VALUE_AS_STRING(attribute)->leaf.data, "x", 1) == 0) {
|
||||||
return TOY_VALUE_FROM_INTEGER(monster->position.x);
|
return TOY_VALUE_FROM_INTEGER(actor->position.x);
|
||||||
}
|
}
|
||||||
else if (TOY_VALUE_AS_STRING(attribute)->info.length == 1 && strncmp(TOY_VALUE_AS_STRING(attribute)->leaf.data, "y", 1) == 0) {
|
else if (TOY_VALUE_AS_STRING(attribute)->info.length == 1 && strncmp(TOY_VALUE_AS_STRING(attribute)->leaf.data, "y", 1) == 0) {
|
||||||
return TOY_VALUE_FROM_INTEGER(monster->position.y);
|
return TOY_VALUE_FROM_INTEGER(actor->position.y);
|
||||||
}
|
}
|
||||||
else if (TOY_VALUE_AS_STRING(attribute)->info.length == 4 && strncmp(TOY_VALUE_AS_STRING(attribute)->leaf.data, "setX", 4) == 0) {
|
else if (TOY_VALUE_AS_STRING(attribute)->info.length == 4 && strncmp(TOY_VALUE_AS_STRING(attribute)->leaf.data, "setX", 4) == 0) {
|
||||||
Toy_Function* fn = Toy_createFunctionFromCallback(&vm->memoryBucket, attr_monsterSetX);
|
Toy_Function* fn = Toy_createFunctionFromCallback(&vm->memoryBucket, attr_actorSetX);
|
||||||
return TOY_VALUE_FROM_FUNCTION(fn);
|
return TOY_VALUE_FROM_FUNCTION(fn);
|
||||||
}
|
}
|
||||||
else if (TOY_VALUE_AS_STRING(attribute)->info.length == 4 && strncmp(TOY_VALUE_AS_STRING(attribute)->leaf.data, "setY", 4) == 0) {
|
else if (TOY_VALUE_AS_STRING(attribute)->info.length == 4 && strncmp(TOY_VALUE_AS_STRING(attribute)->leaf.data, "setY", 4) == 0) {
|
||||||
Toy_Function* fn = Toy_createFunctionFromCallback(&vm->memoryBucket, attr_monsterSetY);
|
Toy_Function* fn = Toy_createFunctionFromCallback(&vm->memoryBucket, attr_actorSetY);
|
||||||
return TOY_VALUE_FROM_FUNCTION(fn);
|
return TOY_VALUE_FROM_FUNCTION(fn);
|
||||||
}
|
}
|
||||||
|
|
||||||
else {
|
else {
|
||||||
char buffer[256];
|
char buffer[256];
|
||||||
snprintf(buffer, 256, "Unknown attribute '%s' of type MonsterData (an Opaque)", TOY_VALUE_AS_STRING(attribute)->leaf.data);
|
snprintf(buffer, 256, "Unknown attribute '%s' of type ActorData (an Opaque)", TOY_VALUE_AS_STRING(attribute)->leaf.data);
|
||||||
Toy_error(buffer);
|
Toy_error(buffer);
|
||||||
return TOY_VALUE_FROM_NULL();
|
return TOY_VALUE_FROM_NULL();
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-5
@@ -3,10 +3,10 @@
|
|||||||
#include "toy_vm.h"
|
#include "toy_vm.h"
|
||||||
|
|
||||||
//object pool system
|
//object pool system
|
||||||
void initMonsterAPI(Toy_VM* vm);
|
void initActorAPI(Toy_VM* vm);
|
||||||
void freeMonsterAPI(Toy_VM* vm);
|
void freeActorAPI(Toy_VM* vm);
|
||||||
|
|
||||||
void processMonsterStep(Toy_VM* vm);
|
void processActorStep(Toy_VM* vm);
|
||||||
void drawMonsters(Toy_VM* vm);
|
void drawActors(Toy_VM* vm);
|
||||||
|
|
||||||
Toy_Value handleMonsterAttributes(Toy_VM* vm, Toy_Value compound, Toy_Value attribute);
|
Toy_Value handleActorAttributes(Toy_VM* vm, Toy_Value compound, Toy_Value attribute);
|
||||||
+14
-44
@@ -167,27 +167,6 @@ void initGameAPI(Toy_VM* vm) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//player data
|
|
||||||
typedef struct PlayerData {
|
|
||||||
Texture2D texture;
|
|
||||||
Rectangle rect;
|
|
||||||
Vector2 position;
|
|
||||||
//TODO: hitbox, hurtbox, motion, etc.
|
|
||||||
} PlayerData;
|
|
||||||
|
|
||||||
PlayerData loadPlayerData(const char* fileName, Rectangle rect) {
|
|
||||||
PlayerData player = {0};
|
|
||||||
if (IsWindowReady()) {
|
|
||||||
player.texture = LoadTexture(fileName);
|
|
||||||
player.rect = rect;
|
|
||||||
}
|
|
||||||
return player;
|
|
||||||
}
|
|
||||||
|
|
||||||
void unloadPlayerData(PlayerData player) {
|
|
||||||
UnloadTexture(player.texture);
|
|
||||||
}
|
|
||||||
|
|
||||||
//main file
|
//main file
|
||||||
int main() {
|
int main() {
|
||||||
//load the entry point
|
//load the entry point
|
||||||
@@ -207,15 +186,12 @@ int main() {
|
|||||||
Toy_bindVM(&vm, entryCode, NULL);
|
Toy_bindVM(&vm, entryCode, NULL);
|
||||||
|
|
||||||
initGameAPI(&vm);
|
initGameAPI(&vm);
|
||||||
initMonsterAPI(&vm);
|
initActorAPI(&vm);
|
||||||
Toy_setOpaqueAttributeHandler(handleMonsterAttributes);
|
Toy_setOpaqueAttributeHandler(handleActorAttributes);
|
||||||
|
|
||||||
Toy_runVM(&vm);
|
Toy_runVM(&vm);
|
||||||
Toy_resetVM(&vm, false, false); //leave in a valid, but unset state
|
Toy_resetVM(&vm, false, false); //leave in a valid, but unset state
|
||||||
|
|
||||||
//load a sprite
|
|
||||||
PlayerData player = loadPlayerData("assets/parvati.png", (Rectangle){0,0,32,32});
|
|
||||||
|
|
||||||
//setup and run the given loop functions
|
//setup and run the given loop functions
|
||||||
if (onReady != NULL) {
|
if (onReady != NULL) {
|
||||||
Toy_bindVM(&vm, onReady->bytecode.code, onReady->bytecode.parentScope);
|
Toy_bindVM(&vm, onReady->bytecode.code, onReady->bytecode.parentScope);
|
||||||
@@ -229,14 +205,14 @@ int main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
while (!WindowShouldClose()) {
|
while (!WindowShouldClose()) {
|
||||||
//input
|
//TODO: player input
|
||||||
if (IsKeyDown(KEY_UP)) player.position.y -= 5.0f;
|
// if (IsKeyDown(KEY_UP)) player.position.y -= 5.0f;
|
||||||
if (IsKeyDown(KEY_DOWN)) player.position.y += 5.0f;
|
// if (IsKeyDown(KEY_DOWN)) player.position.y += 5.0f;
|
||||||
if (IsKeyDown(KEY_LEFT)) player.position.x -= 5.0f;
|
// if (IsKeyDown(KEY_LEFT)) player.position.x -= 5.0f;
|
||||||
if (IsKeyDown(KEY_RIGHT)) player.position.x += 5.0f;
|
// if (IsKeyDown(KEY_RIGHT)) player.position.x += 5.0f;
|
||||||
|
|
||||||
//process the monsters (if possible)
|
//process the actors (if possible)
|
||||||
processMonsterStep(&vm);
|
processActorStep(&vm);
|
||||||
|
|
||||||
//run the onStep function
|
//run the onStep function
|
||||||
Toy_runVM(&vm); //no check needed, empty VMs are skipped
|
Toy_runVM(&vm); //no check needed, empty VMs are skipped
|
||||||
@@ -244,12 +220,7 @@ int main() {
|
|||||||
//drawing
|
//drawing
|
||||||
BeginDrawing();
|
BeginDrawing();
|
||||||
ClearBackground(RAYWHITE);
|
ClearBackground(RAYWHITE);
|
||||||
|
drawActors(&vm);
|
||||||
//draw the player
|
|
||||||
DrawTextureRec(player.texture, player.rect, player.position, WHITE);
|
|
||||||
|
|
||||||
drawMonsters(&vm);
|
|
||||||
|
|
||||||
DrawFPS(0,0);
|
DrawFPS(0,0);
|
||||||
EndDrawing();
|
EndDrawing();
|
||||||
}
|
}
|
||||||
@@ -266,15 +237,14 @@ int main() {
|
|||||||
Toy_resetVM(&vm, false, false);
|
Toy_resetVM(&vm, false, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
freeMonsterAPI(&vm);
|
freeActorAPI(&vm);
|
||||||
unloadPlayerData(player);
|
|
||||||
|
Toy_freeVM(&vm);
|
||||||
|
free(entryCode);
|
||||||
|
|
||||||
if (IsWindowReady()) {
|
if (IsWindowReady()) {
|
||||||
CloseWindow();
|
CloseWindow();
|
||||||
}
|
}
|
||||||
|
|
||||||
Toy_freeVM(&vm);
|
|
||||||
free(entryCode);
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user