From 39896ac26fb6e17e9c2e968a0abf9b491580f598 Mon Sep 17 00:00:00 2001 From: sandyx86 Date: Mon, 12 Aug 2024 11:11:46 -0500 Subject: [PATCH] dialogue scrolling --- src/AssetLoader.m | 11 ++++-- src/Camera.h | 2 +- src/DialogueBox.h | 62 +++++++++++++++++++++++++++++++++ src/DialogueBox.m | 89 +++++++++++++++++++++++++++++++++++++++++++++++ src/MapLoader.h | 33 ++++++++++++++++++ src/MapLoader.m | 35 +++++++++++++++++++ src/Player.h | 2 +- src/Player.m | 2 +- src/main.m | 12 ++----- 9 files changed, 233 insertions(+), 15 deletions(-) create mode 100644 src/DialogueBox.h create mode 100644 src/DialogueBox.m create mode 100644 src/MapLoader.h create mode 100644 src/MapLoader.m diff --git a/src/AssetLoader.m b/src/AssetLoader.m index 806d709..5e88728 100644 --- a/src/AssetLoader.m +++ b/src/AssetLoader.m @@ -21,7 +21,7 @@ Vector3 parse_pos(char *buffer) { vec.x = strtof( buffer, NULL); vec.y = strtof( get_next_token(buffer, ", ", 0), NULL); vec.z = strtof( get_next_token(buffer, ", ", 0), NULL); - printf("got vector: %f %f %f\n", vec.x, vec.y, vec.z); + //printf("got vector: %f %f %f\n", vec.x, vec.y, vec.z); return vec; } @@ -50,15 +50,19 @@ Resource parse_line(char *buffer) { goto GET_POS; } + goto END; //debug + GET_PATH: r.path = get_next_token(buffer, ": ", 0); + printf("got path: %s\n", r.path); goto END; GET_POS: r.pos = parse_pos( get_next_token(buffer, ": ", 0) ); + printf("got pos: %f %f %f\n", r.pos.x, r.pos.y, r.pos.z); END: - get_next_token(NULL, NULL, 1); + get_next_token(NULL, NULL, 1); return r; } @@ -73,7 +77,8 @@ END: for (line = get_next_line(file, 0); line != NULL; line = get_next_line(file, 0)) { //debug - Resource r = parse_line(line); + Resource r = {0}; + r = parse_line(line); if (r.path == NULL) { goto SKIP_PATH; diff --git a/src/Camera.h b/src/Camera.h index 102045b..2227234 100644 --- a/src/Camera.h +++ b/src/Camera.h @@ -31,7 +31,7 @@ -(void) showTarget; -(void) drawCrosshair; -(void) drawWeapon; --(bool) raycast; +-(bool) raycast: (BoundingBox) bbox; -(Camera3D *) addressOfCamera; @end diff --git a/src/DialogueBox.h b/src/DialogueBox.h new file mode 100644 index 0000000..3d8b557 --- /dev/null +++ b/src/DialogueBox.h @@ -0,0 +1,62 @@ +#ifndef DBOX_H +#define DBOX_H + +//a dialogue box for dialogue +#import +#import +#import +#import + + +/* + functionality of dbox: + it will display a rectangle or place to hold text + it will gradually print text within the box + it will pause when a certain delimiter is reached + or other method of pausing the text + it will continue when it is send a continue message + + optional but requirement for quality: + change text speed upon reaching delimiter + or other method, probably something in a script +*/ + +static int thiccness; + +@interface DialogueBox : YSObject { + Vector2 pos; + Rectangle rect; + Color inner_color; + Color outer_color; + Color text_color; + int speed; //speed of text drawing + int left; //ticks left before next char draw + + char *full_text; //a buffer to copy text into + + //display two lines of text + char *line_top; + char *line_bottom; //dont need +} + +/* + method of gradually printing text: + copy the string and slide a null byte through it + + method of controlling speed: + count ticks until 0 + + slide in animation: + box moves towards target pos then stops +*/ + +-(id) initWithText: (char *) text; ++(id) newWithText: (char *) text; +//-(void) setText; +-(void) setSpeed: (int) s; //set speed of text +-(void) tick; +-(void) advance; //advance the text +-(void) draw; //draw the rect + +@end +#endif \ No newline at end of file diff --git a/src/DialogueBox.m b/src/DialogueBox.m new file mode 100644 index 0000000..70364c7 --- /dev/null +++ b/src/DialogueBox.m @@ -0,0 +1,89 @@ +#import "DialogueBox.h" + +@implementation DialogueBox + +Rectangle make_border(Rectangle r) { + return (Rectangle) { + .x = r.x - thiccness, + .y = r.y - thiccness, + .width = r.width + thiccness + thiccness, + .height = r.height + thiccness + thiccness, + }; +} + +-(id) initWithText: (char *) text { + self = [super init]; + + if (self) { + full_text = text; + pos = (Vector2){0, 0}; + rect = (Rectangle){50, 450, 700, 100}; + inner_color = WHITE; + outer_color = BLACK; + text_color = BLACK; + thiccness = 2; + line_top = malloc(100); + speed = 10; + //line_bottom = malloc(100); + memcpy(line_top, full_text, 100); //segfault? + } + + return self; +} + ++(id) newWithText: (char *) text { + return [[DialogueBox alloc] initWithText: text]; +} + +-(void) setSpeed: (int) s { + speed = s; +} + +-(void) tick { + left = speed; + + if (left-- == 0) { + left = speed; + } +} + +-(void) advance { +} + +void swap_char(char *str, int len) { + //might need to add reset function + //or not use static variables. + static int idx; + static char chr; + + idx = idx % len; + + //put back char + if (idx) { + str[idx - 1] = chr; + } + + chr = str[idx]; //save char + + str[idx++] = 0; //zero char +} + +-(void) draw { + DrawRectangleRec(make_border(rect), outer_color); + DrawRectangleRec(rect, inner_color); + static char c; + static int idx; + + if (left == 0) { + swap_char(line_top, 100); + idx = (idx + 1) % 100; + //idx = (idx + 1) % len(current_text) + } + + //line_top[idx] = full_text[idx]; + + + DrawText(line_top, rect.x + 1, rect.y, 20, text_color); +} + +@end \ No newline at end of file diff --git a/src/MapLoader.h b/src/MapLoader.h new file mode 100644 index 0000000..f546029 --- /dev/null +++ b/src/MapLoader.h @@ -0,0 +1,33 @@ +#ifndef MAPLOADER_H +#define MAPLOADER_H + +#import +#import +#import "parser.h" +//import all the stuff + +/* + this might do things like + load the map + set the current map + unload some other map +*/ + +/* + I may need a way to keep things persistent + across maps. + + either by giving certain objects a persist flag + or by putting them in a different array, or + by copying the handler they're in. +*/ + +@interface MapLoader : YSObject { + +} + +-(void) loadMap: (const char *) path; +-(void) sendObject: (id) obj to: (YSArray *) receiver; + +@end +#endif \ No newline at end of file diff --git a/src/MapLoader.m b/src/MapLoader.m new file mode 100644 index 0000000..dfcfe4d --- /dev/null +++ b/src/MapLoader.m @@ -0,0 +1,35 @@ +#include "MapLoader.h" + + +enum { + NONE = 0, + MAP = 1, + BLOCK, + TILE, + NPC, + PLAYER_SPAWN, + LIGHT, + +}; + +@implementation MapLoader + + +-(void) loadMap: (const char *) path { + char *file_buffer = load_file(path); + char *line; + + for(line = get_next_line(file_buffer, 0); line != NULL; line = get_next_line(file_buffer, 0)) { + //depending on what keyword it reads + //it makes that type of object + + id parse_line(char *file_buffer); + + + } +} +-(void) sendObject: (id) obj to: (YSArray *) receiver { + +} + +@end \ No newline at end of file diff --git a/src/Player.h b/src/Player.h index 3254caa..203c546 100644 --- a/src/Player.h +++ b/src/Player.h @@ -24,7 +24,7 @@ -(id) init; -(void) showPos; -(void) attack; --(bool) activate; +-(void) activate; -(void) moveForward; -(void) moveBack; -(void) moveLeft; diff --git a/src/Player.m b/src/Player.m index e6eb97b..6c14a37 100644 --- a/src/Player.m +++ b/src/Player.m @@ -28,7 +28,7 @@ } } --(bool) activate { +-(void) activate { int i; for (i = 0; i < activators->count; i++) { if ( [attachedCamera raycast: [activators->array[i] bbox]] ) { diff --git a/src/main.m b/src/main.m index 5c71090..f707d63 100644 --- a/src/main.m +++ b/src/main.m @@ -11,6 +11,7 @@ #import "Gravitator.h" #import "PhysObject.h" #import "NPC.h" +#import "DialogueBox.h" #import "properties.h" @@ -19,12 +20,9 @@ const int winHeight = 600; int main(int argc, const char *argv[]) { - //NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; //insert custom properties loader here - YSConstantString *cs = @"yes"; - InitWindow(default_properties.width, default_properties.height, "Game :3"); SetTargetFPS(60); @@ -34,14 +32,12 @@ int main(int argc, const char *argv[]) { [camera init]; Player *player = [[Player alloc] init]; - //SoundPlayer *sp = [[SoundPlayer alloc] init]; - //Enemy *enemy = [[Enemy alloc] init]; InputHandler *input = [[InputHandler alloc] init]; AssetLoader *assetLoader = [[AssetLoader alloc] init]; Renderer *renderer = [[Renderer alloc] init]; Gravitator *gravitator = [[Gravitator alloc] init]; NPC *npc = [NPC new]; - + DialogueBox *dbox = [DialogueBox newWithText: "Cock and ball torture, from wikipedia, the free encyclopedia,\nat wikipedia.org"]; input->controllable = player; @@ -69,13 +65,10 @@ int main(int argc, const char *argv[]) { while (!WindowShouldClose()) { [camera update]; - //hard coded input handler [input handleInput]; [gravitator applyGravity]; - - BeginDrawing(); ClearBackground(RAYWHITE); BeginMode3D(camera->camera); @@ -92,6 +85,7 @@ int main(int argc, const char *argv[]) { [camera showTarget]; [player showPos]; [camera drawCrosshair]; + [dbox draw]; //[camera raycast]; //[enemy Render: [camera camera]]; //DrawTexture(tex, default_properties.width - tex.width, default_properties.height - tex.height, WHITE);