Renamed code 'Monster' -> 'Actor'
This commit is contained in:
+5
-5
@@ -3,14 +3,14 @@ var frameCounter: Int = 0;
|
||||
var posCounter: Int = 0;
|
||||
|
||||
fn onReady() {
|
||||
loadMonsterSprite("monster", "assets/parvati.png", 32, 32);
|
||||
loadSprite("zombie", "assets/parvati.png", 32, 32);
|
||||
}
|
||||
|
||||
fn onStep() {
|
||||
frameCounter++;
|
||||
|
||||
if (frameCounter % 100 == 0) {
|
||||
spawnMonsterAt("monster", posCounter*50, posCounter*50);
|
||||
spawnActorAt("zombie", posCounter*50, posCounter*50);
|
||||
posCounter++;
|
||||
}
|
||||
}
|
||||
@@ -24,8 +24,8 @@ initScreen(1280, 720, "Hello raylib from Toy!");
|
||||
initLoop(onReady, onStep, onClose);
|
||||
|
||||
//test
|
||||
fn brains(monster: Opaque) {
|
||||
monster.setX(monster.x + 1);
|
||||
fn brains(actor: Opaque) {
|
||||
actor.setX(actor.x + 1);
|
||||
}
|
||||
|
||||
setMonsterStep(brains);
|
||||
setActorStep(brains);
|
||||
|
||||
+91
-86
@@ -12,41 +12,43 @@
|
||||
#include <string.h>
|
||||
|
||||
//sprites loaded from disk
|
||||
typedef struct MonsterSprite {
|
||||
typedef struct SpriteData {
|
||||
Texture2D texture;
|
||||
Rectangle rect;
|
||||
} MonsterSprite;
|
||||
//TODO: animation
|
||||
} SpriteData;
|
||||
|
||||
//Monsters loaded from scripts
|
||||
typedef struct MonsterData {
|
||||
MonsterSprite* sprite;
|
||||
//Actors loaded from scripts
|
||||
typedef struct ActorData {
|
||||
SpriteData* sprite;
|
||||
Vector2 position;
|
||||
//TODO: animation
|
||||
int health;
|
||||
} MonsterData;
|
||||
} ActorData;
|
||||
|
||||
//static storage
|
||||
static Toy_Table* spriteTable = NULL;
|
||||
static Toy_Array* monsterArray = NULL;
|
||||
static Toy_Function* monsterStep = NULL;
|
||||
static Toy_Array* actorArray = NULL;
|
||||
static Toy_Function* actorStep = NULL;
|
||||
|
||||
//callbacks
|
||||
static void loadMonsterSprite(Toy_VM* vm) {
|
||||
static void loadSprite(Toy_VM* vm) {
|
||||
//key, file, width, height -> null
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
//check for initialization
|
||||
if (spriteTable == NULL || monsterArray == NULL) {
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: Object pool for monster system hasn't been initialized" TOY_CC_RESET "\n");
|
||||
if (spriteTable == NULL || actorArray == NULL) {
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: Object pool for actor system hasn't been initialized" TOY_CC_RESET "\n");
|
||||
return;
|
||||
}
|
||||
|
||||
//check parameter count
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -57,13 +59,13 @@ static void loadMonsterSprite(Toy_VM* vm) {
|
||||
|
||||
//check types
|
||||
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;
|
||||
}
|
||||
|
||||
//check for overwriting the key
|
||||
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(file);
|
||||
Toy_freeValue(width);
|
||||
@@ -72,7 +74,7 @@ static void loadMonsterSprite(Toy_VM* vm) {
|
||||
}
|
||||
|
||||
//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) };
|
||||
|
||||
//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));
|
||||
}
|
||||
|
||||
static void spawnMonsterAt(Toy_VM* vm) {
|
||||
static void spawnActorAt(Toy_VM* vm) {
|
||||
//sprite, x, y -> void
|
||||
|
||||
//check for initialization
|
||||
if (spriteTable == NULL || monsterArray == NULL) {
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: Object pool for monster system hasn't been initialized" TOY_CC_RESET "\n");
|
||||
if (spriteTable == NULL || actorArray == NULL) {
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: Object pool for actor system hasn't been initialized" TOY_CC_RESET "\n");
|
||||
return;
|
||||
}
|
||||
|
||||
//check parameter count
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -111,7 +113,7 @@ static void spawnMonsterAt(Toy_VM* vm) {
|
||||
|
||||
//check types
|
||||
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(xpos);
|
||||
Toy_freeValue(ypos);
|
||||
@@ -123,7 +125,7 @@ static void spawnMonsterAt(Toy_VM* vm) {
|
||||
if (TOY_VALUE_IS_NULL(spriteValue)) {
|
||||
Toy_String* string = Toy_stringifyValue(&(vm->memoryBucket), key);
|
||||
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);
|
||||
Toy_freeString(string);
|
||||
Toy_freeValue(key);
|
||||
@@ -133,52 +135,52 @@ static void spawnMonsterAt(Toy_VM* vm) {
|
||||
}
|
||||
|
||||
//expand the array if needed
|
||||
if (monsterArray->count == monsterArray->capacity) {
|
||||
monsterArray = Toy_resizeArray(monsterArray, monsterArray->capacity * TOY_ARRAY_EXPANSION_RATE);
|
||||
if (actorArray->count == actorArray->capacity) {
|
||||
actorArray = Toy_resizeArray(actorArray, actorArray->capacity * TOY_ARRAY_EXPANSION_RATE);
|
||||
//set the new entries to null values
|
||||
for (unsigned int i = monsterArray->count; i < monsterArray->capacity; i++) {
|
||||
monsterArray->data[i] = TOY_VALUE_FROM_NULL();
|
||||
for (unsigned int i = actorArray->count; i < actorArray->capacity; i++) {
|
||||
actorArray->data[i] = TOY_VALUE_FROM_NULL();
|
||||
}
|
||||
}
|
||||
|
||||
//find an existing spot for the new monster, overwriting a dead one
|
||||
MonsterData* newMonsterPtr = NULL;
|
||||
for (unsigned int i = 0; i < monsterArray->count; i++) {
|
||||
MonsterData* mData = (MonsterData*)TOY_VALUE_AS_OPAQUE(monsterArray->data[i]);
|
||||
if (mData->health <= 0) { //if this monster is dead, steal the slot
|
||||
newMonsterPtr = mData;
|
||||
//find an existing spot for the new actor, overwriting a dead one
|
||||
ActorData* newActorPtr = NULL;
|
||||
for (unsigned int i = 0; i < actorArray->count; i++) {
|
||||
ActorData* mData = (ActorData*)TOY_VALUE_AS_OPAQUE(actorArray->data[i]);
|
||||
if (mData->health <= 0) { //if this actor is dead, steal the slot
|
||||
newActorPtr = mData;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
//if no dead monsters were found, make a new slot
|
||||
if (newMonsterPtr == NULL) {
|
||||
newMonsterPtr = (MonsterData*)Toy_partitionBucket(&(vm->memoryBucket), sizeof(MonsterData));
|
||||
monsterArray->data[monsterArray->count++] = TOY_OPAQUE_FROM_POINTER(newMonsterPtr);
|
||||
//if no dead actors were found, make a new slot
|
||||
if (newActorPtr == NULL) {
|
||||
newActorPtr = (ActorData*)Toy_partitionBucket(&(vm->memoryBucket), sizeof(ActorData));
|
||||
actorArray->data[actorArray->count++] = TOY_OPAQUE_FROM_POINTER(newActorPtr);
|
||||
}
|
||||
|
||||
//finally, store the new monster's data
|
||||
(*newMonsterPtr) = (MonsterData){
|
||||
.sprite = (MonsterSprite*)(TOY_VALUE_AS_OPAQUE(spriteValue)),
|
||||
//finally, store the new actor's data
|
||||
(*newActorPtr) = (ActorData){
|
||||
.sprite = (SpriteData*)(TOY_VALUE_AS_OPAQUE(spriteValue)),
|
||||
.position = { TOY_VALUE_AS_INTEGER(xpos), TOY_VALUE_AS_INTEGER(ypos) },
|
||||
.health = 10,
|
||||
};
|
||||
}
|
||||
|
||||
static void setMonsterStep(Toy_VM* vm) {
|
||||
static void setActorStep(Toy_VM* vm) {
|
||||
Toy_Value value = Toy_popStack(&vm->stack);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
if (TOY_VALUE_IS_FUNCTION(value)) {
|
||||
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);
|
||||
}
|
||||
monsterStep = TOY_VALUE_AS_FUNCTION(value);
|
||||
actorStep = TOY_VALUE_AS_FUNCTION(value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -189,15 +191,17 @@ typedef struct CallbackPairs {
|
||||
} CallbackPairs;
|
||||
|
||||
static CallbackPairs callbackPairs[] = {
|
||||
{"loadMonsterSprite", loadMonsterSprite},
|
||||
{"spawnMonsterAt", spawnMonsterAt},
|
||||
{"setMonsterStep", setMonsterStep},
|
||||
{"loadSprite", loadSprite},
|
||||
// {"unloadSprite", unloadSprite},
|
||||
{"spawnActorAt", spawnActorAt},
|
||||
// {"despawnActor", despawnActor},
|
||||
{"setActorStep", setActorStep},
|
||||
|
||||
{NULL, NULL},
|
||||
};
|
||||
|
||||
//exposed
|
||||
void initMonsterAPI(Toy_VM* vm) {
|
||||
void initActorAPI(Toy_VM* vm) {
|
||||
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);
|
||||
exit(-1);
|
||||
@@ -215,33 +219,33 @@ void initMonsterAPI(Toy_VM* vm) {
|
||||
|
||||
//make the local storage (these use malloc(), don't they?)
|
||||
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;
|
||||
|
||||
//free the GL textures
|
||||
for (unsigned int i = 0; i < spriteTable->capacity; i++) {
|
||||
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);
|
||||
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
|
||||
if (spriteTable == NULL || monsterArray == NULL) {
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: Object pool for monster system hasn't been initialized" TOY_CC_RESET "\n");
|
||||
if (spriteTable == NULL || actorArray == NULL) {
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: Object pool for actor system hasn't been initialized" TOY_CC_RESET "\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if (monsterStep == NULL) {
|
||||
if (actorStep == NULL) {
|
||||
return; //no-op
|
||||
}
|
||||
|
||||
@@ -250,7 +254,7 @@ void processMonsterStep(Toy_VM* vm) {
|
||||
//bind a sub-vm
|
||||
Toy_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
|
||||
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;
|
||||
Toy_String* name = Toy_toStringLength(&subVM.memoryBucket, cstr, strlen(cstr));
|
||||
|
||||
//load each valid monster and process them one at a time
|
||||
for (unsigned int i = 0; i < monsterArray->count; i++) {
|
||||
MonsterData* monster = (MonsterData*)TOY_VALUE_AS_OPAQUE(monsterArray->data[i]);
|
||||
if (monster->health > 0) {
|
||||
//load each valid actor and process them one at a time
|
||||
for (unsigned int i = 0; i < actorArray->count; i++) {
|
||||
ActorData* actor = (ActorData*)TOY_VALUE_AS_OPAQUE(actorArray->data[i]);
|
||||
if (actor->health > 0) {
|
||||
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);
|
||||
|
||||
subVM.scope = Toy_popScope(subVM.scope);
|
||||
@@ -276,84 +280,85 @@ void processMonsterStep(Toy_VM* vm) {
|
||||
Toy_freeVM(&subVM);
|
||||
}
|
||||
|
||||
void drawMonsters(Toy_VM* vm) {
|
||||
void drawActors(Toy_VM* vm) {
|
||||
(void)vm;
|
||||
|
||||
//check for initialization
|
||||
if (spriteTable == NULL || monsterArray == NULL) {
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: Object pool for monster system hasn't been initialized" TOY_CC_RESET "\n");
|
||||
if (spriteTable == NULL || actorArray == NULL) {
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: Object pool for actor system hasn't been initialized" TOY_CC_RESET "\n");
|
||||
return;
|
||||
}
|
||||
|
||||
for (unsigned int i = 0; i < monsterArray->count; i++) {
|
||||
MonsterData* monster = (MonsterData*)TOY_VALUE_AS_OPAQUE(monsterArray->data[i]);
|
||||
for (unsigned int i = 0; i < actorArray->count; i++) {
|
||||
ActorData* actor = (ActorData*)TOY_VALUE_AS_OPAQUE(actorArray->data[i]);
|
||||
|
||||
if (monster->health > 0) {
|
||||
DrawTextureRec(monster->sprite->texture, monster->sprite->rect, monster->position, WHITE);
|
||||
if (actor->health > 0) {
|
||||
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 x = Toy_popStack(&vm->stack);
|
||||
|
||||
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 y = Toy_popStack(&vm->stack);
|
||||
|
||||
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
|
||||
if (spriteTable == NULL || monsterArray == NULL) {
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: Object pool for monster system hasn't been initialized" TOY_CC_RESET "\n");
|
||||
if (spriteTable == NULL || actorArray == NULL) {
|
||||
fprintf(stderr, TOY_CC_ERROR "ERROR: Object pool for actor system hasn't been initialized" TOY_CC_RESET "\n");
|
||||
return TOY_VALUE_FROM_NULL();
|
||||
}
|
||||
|
||||
//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) {
|
||||
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
|
||||
}
|
||||
|
||||
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) {
|
||||
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) {
|
||||
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) {
|
||||
Toy_Function* fn = Toy_createFunctionFromCallback(&vm->memoryBucket, attr_monsterSetX);
|
||||
Toy_Function* fn = Toy_createFunctionFromCallback(&vm->memoryBucket, attr_actorSetX);
|
||||
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) {
|
||||
Toy_Function* fn = Toy_createFunctionFromCallback(&vm->memoryBucket, attr_monsterSetY);
|
||||
Toy_Function* fn = Toy_createFunctionFromCallback(&vm->memoryBucket, attr_actorSetY);
|
||||
return TOY_VALUE_FROM_FUNCTION(fn);
|
||||
}
|
||||
|
||||
else {
|
||||
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);
|
||||
return TOY_VALUE_FROM_NULL();
|
||||
}
|
||||
|
||||
+5
-5
@@ -3,10 +3,10 @@
|
||||
#include "toy_vm.h"
|
||||
|
||||
//object pool system
|
||||
void initMonsterAPI(Toy_VM* vm);
|
||||
void freeMonsterAPI(Toy_VM* vm);
|
||||
void initActorAPI(Toy_VM* vm);
|
||||
void freeActorAPI(Toy_VM* vm);
|
||||
|
||||
void processMonsterStep(Toy_VM* vm);
|
||||
void drawMonsters(Toy_VM* vm);
|
||||
void processActorStep(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
|
||||
int main() {
|
||||
//load the entry point
|
||||
@@ -207,15 +186,12 @@ int main() {
|
||||
Toy_bindVM(&vm, entryCode, NULL);
|
||||
|
||||
initGameAPI(&vm);
|
||||
initMonsterAPI(&vm);
|
||||
Toy_setOpaqueAttributeHandler(handleMonsterAttributes);
|
||||
initActorAPI(&vm);
|
||||
Toy_setOpaqueAttributeHandler(handleActorAttributes);
|
||||
|
||||
Toy_runVM(&vm);
|
||||
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
|
||||
if (onReady != NULL) {
|
||||
Toy_bindVM(&vm, onReady->bytecode.code, onReady->bytecode.parentScope);
|
||||
@@ -229,14 +205,14 @@ int main() {
|
||||
}
|
||||
|
||||
while (!WindowShouldClose()) {
|
||||
//input
|
||||
if (IsKeyDown(KEY_UP)) 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_RIGHT)) player.position.x += 5.0f;
|
||||
//TODO: player input
|
||||
// if (IsKeyDown(KEY_UP)) 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_RIGHT)) player.position.x += 5.0f;
|
||||
|
||||
//process the monsters (if possible)
|
||||
processMonsterStep(&vm);
|
||||
//process the actors (if possible)
|
||||
processActorStep(&vm);
|
||||
|
||||
//run the onStep function
|
||||
Toy_runVM(&vm); //no check needed, empty VMs are skipped
|
||||
@@ -244,12 +220,7 @@ int main() {
|
||||
//drawing
|
||||
BeginDrawing();
|
||||
ClearBackground(RAYWHITE);
|
||||
|
||||
//draw the player
|
||||
DrawTextureRec(player.texture, player.rect, player.position, WHITE);
|
||||
|
||||
drawMonsters(&vm);
|
||||
|
||||
drawActors(&vm);
|
||||
DrawFPS(0,0);
|
||||
EndDrawing();
|
||||
}
|
||||
@@ -266,15 +237,14 @@ int main() {
|
||||
Toy_resetVM(&vm, false, false);
|
||||
}
|
||||
|
||||
freeMonsterAPI(&vm);
|
||||
unloadPlayerData(player);
|
||||
freeActorAPI(&vm);
|
||||
|
||||
Toy_freeVM(&vm);
|
||||
free(entryCode);
|
||||
|
||||
if (IsWindowReady()) {
|
||||
CloseWindow();
|
||||
}
|
||||
|
||||
Toy_freeVM(&vm);
|
||||
free(entryCode);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user