main
sandyx86 1 year ago
parent b86f332fc3
commit 8a0f249cc1

@ -3,17 +3,17 @@
#import <raylib.h> #import <raylib.h>
#import <yeslib.h> #import <yeslib.h>
#import "parser.h" #import "parser.h"
#import "Model.h" #import "PhysObject.h"
#import "Renderer.h"
@interface AssetLoader : YSObject { //need a way to differentiate different types of objects in asset file
YSArray *drawables;
@interface AssetLoader : YSArray {
} }
//@property (copy) NSMutableArray *drawables;
//-(id) init; //-(id) init;
-(void) draw;
-(void) loadAssetFile: (const char *) path; -(void) loadAssetFile: (const char *) path;
-(void) printAllAssets; -(void) sendObject: (id) obj to: (YSArray *) receiver;
@end @end

@ -3,25 +3,7 @@
@implementation AssetLoader @implementation AssetLoader
//@synthesize drawables; //@synthesize drawables;
-(id) init {
if ((self = [super init])) {
drawables = [[YSArray alloc] init];
}
return self;
}
-(void) draw {
int i;
for (i = 0; i < drawables->count; i++) {
//printf("i should be drawing: %p\n", drawables->array[i]);
[drawables->array[i] draw];
}
return;
}
//game specific parts of the parser //game specific parts of the parser
enum { enum {
MDL = 0, MDL = 0,
TEX = 1, TEX = 1,
@ -86,7 +68,7 @@ END:
char *s; char *s;
NSModel *m; PhysObject *m;
for (line = get_next_line(file, 0); line != NULL; line = get_next_line(file, 0)) { for (line = get_next_line(file, 0); line != NULL; line = get_next_line(file, 0)) {
//debug //debug
@ -102,9 +84,11 @@ END:
switch (r.type) { switch (r.type) {
case MDL: case MDL:
//suck cock //suck cock
m = [NSModel alloc]; m = [PhysObject alloc];
[m initWithModel: s]; [m init];
[m setModel: s];
break; break;
case TEX: case TEX:
[m setTexture: s]; [m setTexture: s];
break; break;
@ -112,25 +96,25 @@ END:
SKIP_PATH: SKIP_PATH:
case POS: case POS:
[m setPos: r.pos]; [m setPos: r.pos];
[drawables addObject: m]; [self addObject: m];
break; break;
} }
} }
#ifdef DEBUG #ifdef DEBUG
int i; int i;
for (i = 0; i < drawables->count; i++) { for (i = 0; i < count; i++) {
printf("%p\n", drawables->array[i]); printf("%p\n", array[i]);
printf("%f %f %f\n", [drawables->array[i] pos].x, [drawables->array[i] pos].y, [drawables->array[i] pos].z); printf("%f %f %f\n", [array[i] pos].x, [array[i] pos].y, [array[i] pos].z);
} }
printf("count: %d\n", drawables->count); printf("count: %d\n", count);
printf("capacity: %d\n", drawables->cap); printf("capacity: %d\n", cap);
#endif #endif
} }
-(void) printAllAssets { -(void) sendObject: (id) obj to: (YSArray *) receiver {
printf("capacity: %d\n", [drawables capacity]); [receiver addObject: obj];
} }
@end @end

@ -0,0 +1,25 @@
#ifndef GRAVITY_H
#define GRAVITY_H
//gravitator gravitates things
#import <raylib.h>
#import <yeslib.h>
#import "Activator.h"
#import "PhysObject.h"
//F = M*A
//objects should have mass and a velocity vector
//maybe change this to subclass of YSArray
@interface Gravitator : YSArray {
@public
Vector3 acceleration;
}
-(void) applyGravity;
@end
#endif

@ -0,0 +1,30 @@
#import "Gravitator.h"
static inline Vector3 multFloatVector3(float f, Vector3 v) {
return (Vector3){f * v.x, f * v.y, f * v.z};
}
@implementation Gravitator
//apply gravity to all objects in array
-(void) applyGravity {
int i = 0;
//receivers must have velocity vector
for (i = 0; i < count; i++) {
//cursed
Vector3 force = multFloatVector3( [(PhysObject *)array[i] mass], acceleration);
printf("%f\n", [(PhysObject *)array[i] mass]);
[(PhysObject *)array[i] setVelocity: Vector3Add(force, [(PhysObject *)array[i] velocity])];
[(PhysObject *)array[i] setPos: Vector3Add( [(PhysObject *)array[i] velocity], [(PhysObject *)array[i] pos] )];
//printf("%f %f %f\n", force.x, force.y, force.z );
}
}
@end

@ -4,15 +4,18 @@
#import <yesmath.h> #import <yesmath.h>
#import <yeslib.h> #import <yeslib.h>
@interface NSModel : YSObject { @interface YSModel : YSObject {
Model model; Model model;
Texture texture; Texture texture;
@public
Vector3 pos; Vector3 pos;
float scale; float scale;
} }
-(id) initWithModel: (const char *) path; -(id) initWithModel: (const char *) path;
-(id) initWithModel: (const char *) mdl_path andTexture: (const char *) tex_path; -(id) initWithModel: (const char *) mdl_path andTexture: (const char *) tex_path;
-(void) setModel: (const char *) mdl_path;
-(Model) model;
-(Vector3) pos; -(Vector3) pos;
-(void) setPos: (Vector3) p; -(void) setPos: (Vector3) p;
-(void) setTexture: (const char *) tex_path; -(void) setTexture: (const char *) tex_path;

@ -1,6 +1,6 @@
#import "Model.h" #import "Model.h"
@implementation NSModel @implementation YSModel
-(id) initWithModel: (const char *) path { -(id) initWithModel: (const char *) path {
if ( (self = [super init]) ) { if ( (self = [super init]) ) {
@ -20,6 +20,14 @@
return self; return self;
} }
-(void) setModel: (const char *) mdl_path {
model = LoadModel(mdl_path);
}
-(Model) model {
return model;
}
-(Vector3) pos { -(Vector3) pos {
return pos; return pos;
} }

@ -0,0 +1,20 @@
#ifndef PHYSOBJECT_H
#define PHYSOBJECT_H
#import "Model.h"
@interface PhysObject : YSModel {
@public
float mass;
Vector3 velocity;
}
-(id) init;
-(Vector3) velocity;
-(void) setVelocity: (Vector3) v;
-(float) mass;
-(void) setMass: (float) m;
@end
#endif

@ -0,0 +1,31 @@
#import "PhysObject.h"
@implementation PhysObject
-(id) init {
self = [super init];
if (self) {
mass = 1.0f;
}
return self;
}
-(Vector3) velocity {
return velocity;
}
-(void) setVelocity: (Vector3) v {
velocity = v;
}
-(float) mass {
return mass;
}
-(void) setMass: (float) m {
mass = m;
}
@end

@ -0,0 +1,20 @@
#ifndef RENDERER_H
#define RENDERER_H
#import <yeslib.h>
/*
the main loop may have two or more renderers,
one for the 3D section, and another for the
2D section.
*/
@interface Renderer : YSArray {
}
-(void) render;
@end
#endif

@ -0,0 +1,13 @@
#import "Renderer.h"
@implementation Renderer
-(void) render {
int i;
for (i = 0; i < count; i++) {
[array[i] draw];
}
}
@end

@ -7,6 +7,9 @@
//#import "SoundPlayer.h" //#import "SoundPlayer.h"
#import "InputHandler.h" #import "InputHandler.h"
#import "AssetLoader.h" #import "AssetLoader.h"
#import "Renderer.h"
#import "Gravitator.h"
#import "PhysObject.h"
#import "properties.h" #import "properties.h"
const int winWidth = 800; const int winWidth = 800;
@ -33,19 +36,30 @@ int main(int argc, const char *argv[]) {
//Enemy *enemy = [[Enemy alloc] init]; //Enemy *enemy = [[Enemy alloc] init];
InputHandler *input = [[InputHandler alloc] init]; InputHandler *input = [[InputHandler alloc] init];
AssetLoader *assetLoader = [[AssetLoader alloc] init]; AssetLoader *assetLoader = [[AssetLoader alloc] init];
Renderer *renderer = [[Renderer alloc] init];
Gravitator *gravitator = [[Gravitator alloc] init];
input->controllable = player; input->controllable = player;
gravitator->acceleration = (Vector3){0.0f, -1.0f, 0.0f};
//player.soundPlayer = sp; //player.soundPlayer = sp;
[camera attach: player]; [camera attach: player];
[assetLoader loadAssetFile: "assets.al"]; [assetLoader loadAssetFile: "assets.al"];
int i;
for (i = 0; i < assetLoader->count; i++) {
[assetLoader sendObject: assetLoader->array[i] to: renderer];
[assetLoader sendObject: assetLoader->array[i] to: gravitator];
}
while (!WindowShouldClose()) { while (!WindowShouldClose()) {
[camera update]; [camera update];
//hard coded input handler //hard coded input handler
[input handleInput]; [input handleInput];
[gravitator applyGravity];
BeginDrawing(); BeginDrawing();
ClearBackground(RAYWHITE); ClearBackground(RAYWHITE);
@ -55,7 +69,7 @@ int main(int argc, const char *argv[]) {
DrawCube((Vector3){ 16.0f, 2.5f, 0.0f }, 1.0f, 5.0f, 32.0f, LIME); // Draw a green wall DrawCube((Vector3){ 16.0f, 2.5f, 0.0f }, 1.0f, 5.0f, 32.0f, LIME); // Draw a green wall
DrawCube((Vector3){ 0.0f, 2.5f, 16.0f }, 32.0f, 5.0f, 1.0f, GOLD); DrawCube((Vector3){ 0.0f, 2.5f, 16.0f }, 32.0f, 5.0f, 1.0f, GOLD);
DrawCube((Vector3){ 0.0f, 2.5f, -16.0f }, 32.0f, 5.0f, 1.0f, RED); DrawCube((Vector3){ 0.0f, 2.5f, -16.0f }, 32.0f, 5.0f, 1.0f, RED);
[assetLoader draw]; [renderer render];
EndMode3D(); EndMode3D();

Loading…
Cancel
Save