int ui_drag(int id, rect_t r, int *dx, int *dy) { static int dragid; /* drag flag */ if (dragid == id || rect_hit(r)) { uistate.hotitem = id; if (uistate.activeitem == 0 && uistate.mousedown) uistate.activeitem = id; } if (uistate.hotitem == id) { if (uistate.activeitem == id) { int x = uistate.mousex - r.w / 2; int y = uistate.mousey - r.h / 2; rect_t atmouse = ui_mkrect(x, y, r.w, r.h); rect_draw(atmouse, uistyle.active); dragid = id; uistate.dragid = id; } else rect_draw(r, uistyle.hot); } else rect_draw(r, uistyle.away); if (uistate.mousedown == 0 && uistate.hotitem == id && uistate.activeitem == id) { *dx = uistate.mousex; *dy = uistate.mousey; dragid = 0; return uistate.dragid; } return 0; }
int ui_textfield(int id, rect_t r, char *buf, size_t bufsz) { int len = strlen(buf); int changed = 0; rect_t kbdrect = ui_mkrect(r.x-3, r.y-3, r.w+6, r.h+6); if (rect_hit(r)) { uistate.hotitem = id; if (uistate.activeitem == 0 && uistate.mousedown) uistate.activeitem = id; } if (uistate.kbditem == 0) uistate.kbditem = id; if (uistate.kbditem == id) rect_draw(kbdrect, uistyle.kbdfocus); if (uistate.activeitem == id || uistate.hotitem == id) rect_draw(r, uistyle.active); else rect_draw(r, uistyle.away); buf[len] = '|'; rect_drawtext(r, uistyle.away, buf); buf[len] = 0; if (uistate.kbditem == id) { switch (uistate.keyentered) { case ALLEGRO_KEY_TAB: uistate.kbditem = 0; uistate.keyentered = 0; if (uistate.keymod & ALLEGRO_KEYMOD_SHIFT) uistate.kbditem = uistate.lastwidget; break; case ALLEGRO_KEY_BACKSPACE: if (len <= 0) break; len--; buf[len] = 0; changed = 1; break; case ALLEGRO_KEY_ENTER: changed = 2; default : break; } if (uistate.keychar >= 32 && uistate.keychar < 128 && len < bufsz) { buf[len] = uistate.keychar; len++; buf[len] = 0; changed = 1; } } if (uistate.mousedown == 0 && uistate.hotitem == id && uistate.activeitem == id) uistate.kbditem = id; uistate.lastwidget = id; return changed; }
/* * draws the slider sldr on the touch screen */ void slider_draw(slider *sldr) { MYstrcpy(sldr->printLabel,sldr->label.label); strAppendInt(sldr->printLabel,*(sldr->level)); rect_draw(&sldr->bgnd); rect_draw(&sldr->track); rect_draw(&sldr->slide); rect_draw(&sldr->bottomEdge); text_drawVal(&sldr->label, sldr->printLabel); }
int ui_drop(int id, rect_t r, int dx, int dy) { int dragid; if (rect_hit(r) && uistate.dragid) rect_draw(r, uistyle.active); else rect_draw(r, uistyle.away); if (!rect_hitxy(r, dx, dy)) return 0; dragid = uistate.dragid; uistate.dragid = 0; return dragid; }
void xyGrid_draw(xyGrid *grid) { MYstrcpy(grid->xPrintLabel,grid->xlabel.label); MYstrcpy(grid->yPrintLabel,grid->ylabel.label); strAppendInt(grid->xPrintLabel,*grid->xlevel); strAppendInt(grid->yPrintLabel,*grid->ylevel); rect_draw(&grid->bgnd); circle_draw(&grid->dot); rect_draw(&grid->leftEdge); rect_draw(&grid->rightEdge); rect_draw(&grid->bottomEdge); text_drawVal(&grid->xlabel, grid->xPrintLabel); text_drawVal(&grid->ylabel, grid->yPrintLabel); }
DrawTextResult text_draw(i32 x, i32 y, char *text, u8 cf, u8 cb) { ASSERT(PROGRAM->font); V4i r = v4i_make_size(x, y, PROGRAM->font->cw, PROGRAM->font->ch); if ((x + PROGRAM->tx) < PROGRAM->bitmap_rect.max_x && (y + PROGRAM->ty) < PROGRAM->bitmap_rect.max_y) { // rect_tr(&r, PROGRAM->tx, PROGRAM->ty); V4i rect; ImageSet *font = PROGRAM->font; char *anchor = text; for (;;) { text = find_next(anchor, '\n'); if (cb) { rect.min_x = x - font->padding.min_x; rect.min_y = y - font->padding.min_y; rect.max_x = x + (text - anchor) * font->cw + font->padding.max_x; rect.max_y = y + font->ch + font->padding.max_y; rect_draw(rect, cb); } i32 xp = x; while (anchor != text) { if (*anchor != ' ') { image_set_draw(xp, y, PROGRAM->font, *anchor, DrawSpriteMode_Mask, cf); } anchor++; xp += PROGRAM->font->cw; } if (*text == 0) { break; } text++; anchor = text; rect_tr(&r, 0, PROGRAM->font->ch); } } DrawTextResult res = { r.max_x, r.min_y }; return res; }
void draw_message_box(char *line1, char *line2, char *line3) { i32 i, w = CANVAS_WIDTH, h = CANVAS_HEIGHT; draw_sprite(0, h-1-2.0f*TILE_SIZE, 0, 4, 0); draw_sprite(0, h-1-1.0f*TILE_SIZE, 0, 5, 0); draw_sprite(w-1-TILE_SIZE, h-1-2.0f*TILE_SIZE, 0, 4, DrawFlags_FlipH); draw_sprite(w-1-TILE_SIZE, h-1-1.0f*TILE_SIZE, 0, 5, DrawFlags_FlipH); for (i = TILE_SIZE; i < w-1-TILE_SIZE; i += TILE_SIZE) { draw_sprite(i, h-1-2.0f*TILE_SIZE, 1, 4, 0); draw_sprite(i, h-1-1.0f*TILE_SIZE, 1, 5, 0); } rect_draw(rect_make_size(5, h-1-27, w-1-10, 22), PC_WHITE); if (line1) text_draw(8, 116+0*font.char_height, line1, PC_BLACK); if (line2) text_draw(8, 116+1*font.char_height, line2, PC_BLACK); if (line3) text_draw(8, 116+2*font.char_height, line3, PC_BLACK); }
void rect_drawtext(rect_t r, ALLEGRO_COLOR c, const char *s) { int i, len; int sx, fw = uistyle.fontw; int sy, fh = uistyle.fonth; if (s == NULL) return; len = strlen(s); /* make text and rect with the same center. */ /* Sbegin = Rcenter - Ssize */ sx = (r.x + r.w/2) - (fw * len)/2; sy = (r.y + r.h/2) - (fh)/2; rect_draw(r, c); glBindTexture(GL_TEXTURE_2D, al_get_opengl_texture(uistyle.font12)); for (i=0; i<len; i++) drawchar(sx+i*fw, sy, s[i]); }
void DrawBorder( CRect& rect, HDC hdc, CBitmap& bmpLeft, CBitmap& bmpRight, CBitmap& bmpTop, CBitmap& bmpBottom, HBITMAP hbmpBg ) { BITMAP infoLeft, infoRight, infoTop, infoBottom; CRect rect_draw(rect); if ( bmpLeft.IsNull() || bmpRight.IsNull() || bmpTop.IsNull() || bmpBottom.IsNull() ) { return; } bmpLeft.GetBitmap( infoLeft ); rect_draw.right = rect_draw.left + infoLeft.bmWidth; DrawBmp( hdc, rect_draw, bmpLeft ); rect_draw.right = rect.right; bmpRight.GetBitmap( infoRight ); rect_draw.left = rect_draw.right - infoRight.bmWidth; DrawBmp( hdc, rect_draw, bmpRight ); rect_draw.top = rect.top; rect_draw.left = rect.left + infoLeft.bmWidth; rect_draw.right = rect.right - infoRight.bmWidth; bmpTop.GetBitmap( infoTop ); rect_draw.bottom = rect.top + infoTop.bmHeight; DrawBmp( hdc, rect_draw, bmpTop ); rect_draw.bottom = rect.bottom; bmpBottom.GetBitmap( infoBottom ); rect_draw.top = rect_draw.bottom - infoBottom.bmHeight; DrawBmp( hdc, rect_draw, bmpBottom ); if ( hbmpBg ) { rect_draw.bottom = rect.bottom - infoBottom.bmHeight; rect_draw.top = rect.top + infoTop.bmHeight; DrawBmp( hdc, rect_draw, hbmpBg ); } }
void step(void) { if (key_pressed(KEY_ESCAPE)) { CORE->running = 0; return; } if (key_pressed(KEY_1)) set_palette_set(PS_WHITE); else if (key_pressed(KEY_2)) set_palette_set(PS_GREEN); else if (key_pressed(KEY_3)) set_palette_set(PS_BEIGE); else if (key_pressed(KEY_4)) set_palette_set(PS_BLUE); else if (key_pressed(KEY_5)) set_palette_set(PS_RED); else if (key_pressed(KEY_6)) set_palette_set(PS_PASTEL_MIX); else if (key_pressed(KEY_7)) set_palette_set(PS_RED_GREEN); switch (game.mode) { case GAME_MODE_TITLE: { if (game.intro_cooldown --> 0) { canvas_clear(PC_TRANSPARENT); bitmap_draw(0, 0, 0, 0, &title_screen, NULL, 0, 0); } else { game.mode = GAME_MODE_MAP; } } break; case GAME_MODE_MAP: { Map *map = &game.maps[game.curr_map_index]; i32 i, j, x, y; gbVec2 old_player_pos = game.player.pos; b32 render_doctor_text = false; canvas_clear(PC_TRANSPARENT); game.player.vel = gb_vec2_zero(); if (key_down(KEY_UP)) { game.player.vel.y -= 1.0f; } if (key_down(KEY_DOWN)) { game.player.vel.y += 1.0f; } if (key_down(KEY_LEFT)) { game.player.vel.x -= 1.0f; } if (key_down(KEY_RIGHT)) { game.player.vel.x += 1.0f; } gb_vec2_norm0(&game.player.vel, game.player.vel); gb_vec2_muleq(&game.player.vel, game.player.in_grass ? 2.5f : 3.5f); gb_vec2_addeq(&game.player.pos, game.player.vel); game.player.in_grass = false; game.player.is_moving = (gb_vec2_mag(game.player.vel) > 0); if (game.player.vel.x < 0) game.player.orientation = 2; if (game.player.vel.x > 0) game.player.orientation = 3; if (game.player.vel.y > 0) game.player.orientation = 0; if (game.player.vel.y < 0) game.player.orientation = 1; game.player.pos.x = gb_clamp(game.player.pos.x, 0, (map->width-1) *TILE_SIZE); game.player.pos.y = gb_clamp(game.player.pos.y, 0, (map->height-1)*TILE_SIZE); game.grass_event_cooldown--; if (game.grass_event_cooldown < 0) game.grass_event_cooldown = 0; draw_tooltip = false; gb_zero_array(tooltip_messages, gb_count_of(tooltip_messages)); { // Handle Interactions Rect player_rect = rect_make_size(game.player.pos.x+2, game.player.pos.y+12, 12, 4); for (j = 0; j < map->height; j++) { y = j*TILE_SIZE; for (i = 0; i < map->width; i++) { Rect tile_rect; char t = map->tiles[j][i]; x = i*TILE_SIZE; tile_rect = rect_make_size(x, y, TILE_SIZE, TILE_SIZE); if (gb_char_is_digit(t)) { if (rect_collides(player_rect, tile_rect, NULL)) { change_map_to(t-'0'); return; } } if ((t != ' ' && t != '.' && t != 'g')) { Rect inter; b32 do_collision = true; if (t == 'L') { // NOTE(bill): Lab Chemicals Top tile_rect = rect_make_size(x, y+7, TILE_SIZE, TILE_SIZE-7); } if (t == 'Q' && game.has_chosen_transmog) { do_collision = false; } if (do_collision && rect_collides(player_rect, tile_rect, &inter)) { game.player.pos = old_player_pos; game.player.is_moving = false; } } if (t == 'g' && game.player.is_moving && rect_collides(player_rect, tile_rect, NULL)) { i32 random = gb_random_range_int(0, 100); game.player.in_grass = true; if ((random < 7) && (game.grass_event_cooldown <= 0)) { game.grass_event_cooldown = 50; #if 1 change_to_battle_mode(BATTLE_TYPE_GRASS); #endif return; } } if (t == 'D') { f32 dx = game.player.pos.x - (x + TILE_SIZE/2); f32 dy = game.player.pos.y - (y + TILE_SIZE/2); if (dx*dx+dy*dy < square(2*TILE_SIZE)) render_doctor_text = true; } if (t == 'Y') { set_tooltip(x, y, 1.5f, "Hello! I'm your worst nightmare!", "My name is Yugi the Boss.", "Want to battle [z]?"); if (key_pressed(KEY_Z)) { change_to_battle_mode(BATTLE_TYPE_BOSS); return; } } if (t == 'Q' && !game.has_chosen_transmog) { set_tooltip(x, y, 1.5f, "It's dangerous to go alone", "in there! You might get hurt.", "Go see The Doctor in The Lab."); } if (t == 'L' || t == 'l') { set_tooltip(x, y, 1.0f, "It appears to be weird chemicals.", NULL, NULL); } if (t == 'm' || t == 'c') { set_tooltip(x, y, 1.0f, "State of the art computers", "The new Pear Bartlett!", NULL); } if (t == 'P') { set_tooltip(x, y, 0.6f, "Such a lovely picture!", NULL, NULL); } if (t == 'B' || t == 'b') { set_tooltip(x, y, 1.2f, "It is not time to sleep.", NULL, NULL); } if (t == 'W') { set_tooltip(x, y, 0.6f, "It's a beautiful view outside.", "Why not go outside and explore?", NULL); } } } } move_game_camera(map); // Render 1x1 Sprites for (j = 0; j < map->height; j++) { y = j*TILE_SIZE; for (i = 0; i < map->width; i++) { char t = map->tiles[j][i]; x = i*TILE_SIZE; switch (t) { case ' ': case '.': case 'g': case 'Q': case 'D': case 'Y': { i32 sx = 0; i32 sy = map->outside; if (map->outside) { sx = (i+j)%2; } draw_sprite(x, y, sx, sy, 0); if (t == 'g') draw_sprite(x, y, 2, 1, 0); // grass if (t == 'Q' && !game.has_chosen_transmog) draw_sprite(x, y, 0, 3, 0); // Person in the way if (t == 'D') draw_sprite(x, y, 1, 3, 0); // Dr. if (t == 'Y') draw_sprite(x, y, 2, 3, 0); // Yugi } break; case 'x': draw_sprite(x, y, 1, 0, 0); break; // Wall case 'P': draw_sprite(x, y, 2, 0, 0); break; // Hanging Picture case 'W': draw_sprite(x, y, 3, 0, 0); break; // Window case 'T': draw_sprite(x, y, 3, 1, 0); break; // Thick Grass case 't': draw_sprite(x, y, 4, 1, 0); break; // Tree trunk case 'B': draw_sprite(x, y, 5, 0, 0); break; // Bed Top case 'b': draw_sprite(x, y, 5, 1, 0); break; // Bed bottom case 'M': draw_sprite(x, y, 6, 0, 0); break; // Computer monitor case 'm': draw_sprite(x, y, 6, 1, 0); break; // Computer keyboard case 'C': draw_sprite(x, y, 7, 0, 0); break; // Computer case case 'c': draw_sprite(x, y, 7, 1, 0); break; // Computer mouse case 'L': draw_sprite(x, y, 8, 0, 0); break; // Lap Chemicals top case 'l': draw_sprite(x, y, 8, 1, 0); break; // Lap Chemicals bottom default: { if (gb_char_is_digit(t)) draw_sprite(x, y, 4, 0, 0); } break; } } } // Render NxM Sprites // TODO(bill): Be more efficient for (j = 0; j < map->height; j++) { y = j*TILE_SIZE; for (i = 0; i < map->width; i++) { char t = map->tiles[j][i]; x = i*TILE_SIZE; switch (t) { case 'h': draw_sprite_size(x, y, 12, 2, 4, 4, 0); break; case 'd': draw_sprite_size(x, y, 8, 2, 4, 4, 0); break; } } } { // Render player and particles i32 sx = 0; u32 flags = 0; // 0 - down // 1 - up // 2 - left // 3 - right (-ve left) if (game.player.orientation == 0) sx = 0; if (game.player.orientation == 1) sx = 1; if (game.player.orientation == 2) sx = 2; if (game.player.orientation == 3) { sx = 2; flags |= DrawFlags_FlipH; } if (game.player.is_moving) { u32 flip = 10.0f*CORE->perf_frame.stamp; b32 use_flip = true; if (game.player.orientation == 0) { if (use_flip) { sx = 3; flags |= DrawFlags_FlipH * (flip%2); } } else if (game.player.orientation == 1) { if (use_flip) { sx = 4; flags |= DrawFlags_FlipH * (flip%2); } } else { sx += 3 * (flip%2); } } draw_sprite(game.player.pos.x, game.player.pos.y, sx, 2, flags); } { // Render Text CORE->translate_x = 0; CORE->translate_y = 0; if (game.show_map_name_ticks-- > 0) { if (game.show_map_name_ticks < 0) game.show_map_name_ticks = 0; { char *name = game.maps[game.curr_map_index].name; i32 name_len = gb_strlen(name); i32 w = name_len*font.char_width+6; i32 h = font.char_height+6; rect_draw(rect_make_size(1, 1, w, h), PC_WHITE); rect_draw(rect_make_size(1, 0, w, 1), PC_DARK_GREY); rect_draw(rect_make_size(1, h+1, w, 1), PC_DARK_GREY); rect_draw(rect_make_size(0, 1, 1, h), PC_DARK_GREY); rect_draw(rect_make_size(w+1, 1, 1, h), PC_DARK_GREY); text_draw(4, 4, name, PC_BLACK); } } if (render_doctor_text) { if (game.doctor_line_index < gb_count_of(doctor_lines)) { draw_message_box(doctor_lines[game.doctor_line_index], "", "[Z] to continue"); if (key_pressed(KEY_Z)) game.doctor_line_index++; } else if (!game.has_chosen_transmog) { draw_message_box("Line [X]", "Triangle [C]", "Square [V]"); if (key_pressed(KEY_X)) { game.player.my_transmog = make_transmog(TRANSMOG_TYPE_LINE); game.has_chosen_transmog = true; } else if (key_pressed(KEY_C)) { game.player.my_transmog = make_transmog(TRANSMOG_TYPE_TRIANGLE); game.has_chosen_transmog = true; } else if (key_pressed(KEY_V)) { game.player.my_transmog = make_transmog(TRANSMOG_TYPE_SQUARE); game.has_chosen_transmog = true; } } else { draw_message_box("Go now and find Transmogs!", "", ""); } } else if (draw_tooltip) { draw_message_box(tooltip_messages[0], tooltip_messages[1], tooltip_messages[2]); } } } break; case GAME_MODE_BATTLE: { i32 i, w = CANVAS_WIDTH, h = CANVAS_HEIGHT; Transmog *enemy_mog = &game.battle_mode.enemy_mog; Transmog *player_mog = game.battle_mode.player_mog; char name_buf[32] = ""; char *attack_names[3] = { "TRANSLATE", "ROTATE", "DIVIDE", }; i32 level_diff = enemy_mog->shift_level - player_mog->shift_level; CORE->translate_x = 0; CORE->translate_y = 0; canvas_clear(PC_WHITE); // Enemy Transmog gb_snprintf(name_buf, gb_size_of(name_buf), "%s LVL%d", transmog_name(enemy_mog->type), enemy_mog->shift_level); text_draw(8, 5, name_buf, PC_BLACK); draw_sprite_size(8, 12, 2, 4, 3, 1, 0); draw_sprite_size(24, 15, 3, 5, 2, 1, 0); { f32 hp_percent = cast(f32)enemy_mog->health/cast(f32)enemy_mog->max_health; rect_draw(rect_make_size(25, 16, hp_percent*30, 1), PC_LIGHT_GREY); draw_big_transmog(84, 4, enemy_mog->type); } // Player Transmog gb_snprintf(name_buf, gb_size_of(name_buf), "%s LVL%d", transmog_name(player_mog->type), player_mog->shift_level); text_draw(100, 80, name_buf, PC_BLACK); draw_sprite_size(104, 92, 5, 4, 3, 1, 0); draw_sprite_size(117, 95, 3, 5, 2, 1, 0); { f32 hp_percent = cast(f32)player_mog->health/cast(f32)player_mog->max_health; char buf[16] = {0}; gb_snprintf(buf, gb_size_of(buf), "%03d/%03d", player_mog->health, player_mog->max_health); rect_draw(rect_make_size(118, 96, hp_percent*30, 1), PC_LIGHT_GREY); text_draw(118, 99, buf, PC_BLACK); draw_big_transmog(4, 54, player_mog->type); } if (!game.battle_mode.is_enemies_turn) { switch (game.battle_mode.state) { case BATTLE_TURN_STATE_INTRO: { char buf[32] = {0}; gb_snprintf(buf, gb_size_of(buf), "A wild %s appears!", transmog_name(enemy_mog->type)); draw_message_box(buf, "", "[Z] to continue"); if (key_pressed(KEY_Z)) { game.battle_mode.state = BATTLE_TURN_STATE_WHAT_TO_DO; return; } } break; case BATTLE_TURN_STATE_WHAT_TO_DO: { u32 hot_item = 0; if (game.battle_mode.type == BATTLE_TYPE_GRASS) { game.battle_mode.state_index %= 3; hot_item = game.battle_mode.state_index; draw_message_box("What to do? Fight ", " Substitute", " Escape "); draw_sprite(70, 111, 0, 4, 0); draw_sprite(70, 127, 0, 5, 0); draw_sprite(90, 117 + hot_item*font.char_height, 5, 5, 0); if (key_pressed(KEY_DOWN)) { game.battle_mode.state_index++; } if (key_pressed(KEY_UP)) { if (game.battle_mode.state_index == 0) game.battle_mode.state_index = 2; else game.battle_mode.state_index--; } if (key_pressed(KEY_Z)) { switch (hot_item) { case 0: game.battle_mode.state = BATTLE_TURN_STATE_FIGHT; game.battle_mode.state_index = 0; game.battle_mode.fighting_state = FIGHTING_STATE_NONE; return; case 1: game.battle_mode.state = BATTLE_TURN_STATE_SUBSTITUTE; game.battle_mode.state_index = 0; case 2: game.battle_mode.state = BATTLE_TURN_STATE_ESPACE; game.battle_mode.state_index = 0; return; } } } else { game.battle_mode.state_index %= 2; hot_item = game.battle_mode.state_index; draw_message_box("What to do? Fight ", " Escape ", " "); draw_sprite(70, 111, 0, 4, 0); draw_sprite(70, 127, 0, 5, 0); draw_sprite(90, 117 + hot_item*font.char_height, 5, 5, 0); if (key_pressed(KEY_DOWN)) { game.battle_mode.state_index++; } if (key_pressed(KEY_UP)) { if (game.battle_mode.state_index == 0) game.battle_mode.state_index = 1; else game.battle_mode.state_index--; } if (key_pressed(KEY_Z)) { switch (hot_item) { case 0: game.battle_mode.state = BATTLE_TURN_STATE_FIGHT; game.battle_mode.state_index = 0; game.battle_mode.fighting_state = FIGHTING_STATE_NONE; return; case 1: game.battle_mode.state = BATTLE_TURN_STATE_ESPACE; game.battle_mode.state_index = 0; return; } } } } break; case BATTLE_TURN_STATE_FIGHT: { char buf[32] = ""; switch (game.battle_mode.fighting_state) { case FIGHTING_STATE_NONE: { u32 hot_item = game.battle_mode.state_index; draw_message_box("Which attack? Translate ", " Rotate ", " Divide "); draw_sprite(70, 111, 0, 4, 0); draw_sprite(70, 127, 0, 5, 0); draw_sprite(90, 117 + hot_item*font.char_height, 5, 5, 0); if (key_pressed(KEY_DOWN)) { game.battle_mode.state_index++; game.battle_mode.state_index %= 3; } if (key_pressed(KEY_UP)) { if (game.battle_mode.state_index == 0) game.battle_mode.state_index = 2; else game.battle_mode.state_index--; } if (key_pressed(KEY_X)) { game.battle_mode.state_index = 0; game.battle_mode.state = BATTLE_TURN_STATE_WHAT_TO_DO; return; } if (key_pressed(KEY_Z)) { game.battle_mode.attack_index = hot_item; game.battle_mode.state_index = 0; game.battle_mode.fighting_state = FIGHTING_STATE_USE; return; } } break; case FIGHTING_STATE_USE: { gb_snprintf(buf, gb_size_of(buf), "Your %s used %s", transmog_name(enemy_mog->type), attack_names[game.battle_mode.attack_index]); draw_message_box(buf, NULL, NULL); if (key_pressed(KEY_Z)) { if (-level_diff < 0) { // Higher level enemy game.battle_mode.fighting_state = FIGHTING_STATE_SUPER_EFFECTIVE; } else { if (gb_random_range_int(0, -level_diff+1) == 0) { game.battle_mode.fighting_state = FIGHTING_STATE_SUPER_EFFECTIVE; } else { if (gb_random_range_int(0, -level_diff/2 + 1) == 0) game.battle_mode.fighting_state = FIGHTING_STATE_EFFECTIVE; else game.battle_mode.fighting_state = FIGHTING_STATE_NO_EFFECT; } } return; } } break; case FIGHTING_STATE_EFFECTIVE: { gb_snprintf(buf, gb_size_of(buf), "%s was effective", attack_names[game.battle_mode.attack_index]); draw_message_box(buf, NULL, NULL); if (key_pressed(KEY_Z)) { i32 damage = gb_clamp(0, player_mog->attack_power/3 + level_diff/2, 1000); enemy_mog->health -= damage; game.battle_mode.fighting_state = FIGHTING_STATE_NEXT_TURN; } } break; case FIGHTING_STATE_SUPER_EFFECTIVE: { gb_snprintf(buf, gb_size_of(buf), "%s was super effective", attack_names[game.battle_mode.attack_index]); draw_message_box(buf, NULL, NULL); if (key_pressed(KEY_Z)) { i32 damage = gb_clamp(0, player_mog->attack_power/1 + level_diff/2, 1000); enemy_mog->health -= damage; game.battle_mode.fighting_state = FIGHTING_STATE_NEXT_TURN; game.battle_mode.state_index = 0; } } break; case FIGHTING_STATE_NO_EFFECT: { gb_snprintf(buf, gb_size_of(buf), "%s had no effect", attack_names[game.battle_mode.attack_index]); draw_message_box(buf, NULL, NULL); if (key_pressed(KEY_Z)) game.battle_mode.fighting_state = FIGHTING_STATE_NEXT_TURN; } break; case FIGHTING_STATE_NEXT_TURN: { char *end_message = ""; if (enemy_mog->health <= 0 && !(game.battle_mode.state_index & 1)) { enemy_mog->health = 0; player_mog->xp += clamp(1, 4 + level_diff, 1000); while (player_mog->xp >= 10) { player_mog->xp -= 10; player_mog->shift_level++; update_transmog_stats(player_mog); player_mog->health = player_mog->max_health; game.battle_mode.state_index |= 2; } game.battle_mode.state_index |= 1; } if (game.battle_mode.state_index & 1) { if (game.battle_mode.type == BATTLE_TYPE_GRASS) { if (game.battle_mode.state_index & 2) end_message = "Your Transmog has levelled up!"; draw_message_box("You win this battle!", end_message, "Exit the battle."); if (key_pressed(KEY_Z)) { game.mode = GAME_MODE_MAP; game.battle_mode.state_index = 0; return; } } else { canvas_clear(PC_WHITE); draw_sprite(72, 40, 2, 3, 0); draw_message_box("You defeated me?! HOW?!", "Well it's my first battle...", "Well done and thank you!"); if (key_pressed(KEY_Z)) { game.mode = GAME_MODE_END_SUCCESS; return; } } } else { draw_message_box("End turn.", NULL, NULL); if (key_pressed(KEY_Z)) { end_battle_mode_turn(); return; } } } break; } } break; case BATTLE_TURN_STATE_SUBSTITUTE: { i32 level_diff = enemy_mog->shift_level - player_mog->shift_level; if (level_diff <= 0) { draw_message_box("Failed to substitute", "", "End turn."); if (key_pressed(KEY_Z)) { end_battle_mode_turn(); return; } } else { if (game.battle_mode.state_index == 0) { if (gb_random_range_int(0, level_diff) != 0) game.battle_mode.state_index = 1; // Success else game.battle_mode.state_index = 2; // Failure } if (game.battle_mode.state_index == 1) { draw_message_box("Well done!", "You successfully substituted.", "Exit to world"); if (key_pressed(KEY_Z)) { // Change it! game.player.my_transmog = *enemy_mog; game.mode = GAME_MODE_MAP; return; } } else { draw_message_box("Substitution failed!", "", "End turn."); if (key_pressed(KEY_Z)) { end_battle_mode_turn(); return; } } } } break; case BATTLE_TURN_STATE_ESPACE: { if (game.battle_mode.state_index == 0) { if (gb_random_range_int(0, 100) < 90) { game.battle_mode.state_index = 1; } else { game.battle_mode.state_index = 2; } } if (game.battle_mode.state_index == 1) { draw_message_box("You escaped from this battle!", "", "[Z] to return to the world."); if (key_pressed(KEY_Z)) { game.mode = GAME_MODE_MAP; return; } } else { draw_message_box("You could not escape!", "You'll have to try again!", "[Z] to end turn."); end_battle_mode_turn(); return; } } break; } } else { // NOTE(bill): Enemies Turn char buf[32] = {0}; switch (game.battle_mode.fighting_state) { case FIGHTING_STATE_NONE: { game.battle_mode.attack_index = gb_random_range_int(0, 2); game.battle_mode.fighting_state = FIGHTING_STATE_USE; return; } break; case FIGHTING_STATE_USE: { gb_snprintf(buf, gb_size_of(buf), "Enemy %s used %s", transmog_name(enemy_mog->type), attack_names[game.battle_mode.attack_index]); draw_message_box(buf, NULL, NULL); if (key_pressed(KEY_Z)) { if (level_diff < 0) { // Higher level enemy game.battle_mode.fighting_state = FIGHTING_STATE_SUPER_EFFECTIVE; } else { if (gb_random_range_int(0, -level_diff+3) == 0) { game.battle_mode.fighting_state = FIGHTING_STATE_SUPER_EFFECTIVE; } else { if (gb_random_range_int(0, -level_diff/2 + 3) == 0) game.battle_mode.fighting_state = FIGHTING_STATE_EFFECTIVE; else game.battle_mode.fighting_state = FIGHTING_STATE_NO_EFFECT; } } return; } } break; case FIGHTING_STATE_EFFECTIVE: { gb_snprintf(buf, gb_size_of(buf), "%s was effective", attack_names[game.battle_mode.attack_index]); draw_message_box(buf, NULL, NULL); if (key_pressed(KEY_Z)) { i32 damage = gb_clamp(0, enemy_mog->attack_power/6.0f - level_diff/3.0f, 1000); player_mog->health -= damage; game.battle_mode.fighting_state = FIGHTING_STATE_NEXT_TURN; } } break; case FIGHTING_STATE_SUPER_EFFECTIVE: { gb_snprintf(buf, gb_size_of(buf), "%s was super effective", attack_names[game.battle_mode.attack_index]); draw_message_box(buf, NULL, NULL); if (key_pressed(KEY_Z)) { i32 damage = gb_clamp(0, enemy_mog->attack_power/3.0f - level_diff/2.0f, 1000); player_mog->health -= damage; game.battle_mode.fighting_state = FIGHTING_STATE_NEXT_TURN; } } break; case FIGHTING_STATE_NO_EFFECT: { gb_snprintf(buf, gb_size_of(buf), "%s had no effect", attack_names[game.battle_mode.attack_index]); draw_message_box(buf, NULL, NULL); if (key_pressed(KEY_Z)) game.battle_mode.fighting_state = FIGHTING_STATE_NEXT_TURN; } break; case FIGHTING_STATE_NEXT_TURN: { char *end_message = ""; if (player_mog->health <= 0 && !(game.battle_mode.state_index & 1)) { player_mog->health = 0; game.battle_mode.state_index |= 1; } if (game.battle_mode.state_index & 1) { draw_message_box("You lost this battle!", "And the game :(", "Exit the battle."); if (key_pressed(KEY_Z)) { game.mode = GAME_MODE_END_FAIL; return; } } else { draw_message_box("The enemy's turn has ended.", NULL, NULL); if (key_pressed(KEY_Z)) { end_battle_mode_turn(); return; } } } break; } } } break; case GAME_MODE_END_FAIL: { canvas_clear(PC_BLACK); draw_message_box("You have failed :(", "Would you like to try again?", "Try again [z] Exit [esc]"); if (key_pressed(KEY_Z)) { init_game(); return; } } break; case GAME_MODE_END_SUCCESS: { canvas_clear(PC_BLACK); text_draw(4, 4, "Thank you for playing Transmog!\n" "Ginger Bill (c) 2016\n" "\n" "Ludum Dare 35\n" "Theme: Shapeshift\n" "\n" "Please remember for vote!\n", PC_WHITE); draw_message_box("Exit [esc]", NULL, NULL); } break; } }
void step() { // printf("\nSTEP %d\n", PROGRAM->frame); draw_set(COLOR_BLACK); Entity *entity; memi i; player_step(GAME->player); // // Update entities // entity = GAME->entities; for (i = 0; i != GAME->entities_count; ++i, ++entity) { if (!equalf(entity->vx, 0, 0.1) || !equalf(entity->vy, 0, WORLD_COLLISION_EPSILON)) { world_test_move(&GAME->world, entity->collider, entity->vx, entity->vy, CollisionTestMask_All, &collision, on_collision); if (!equalf(collision.end_x, entity->collider->x, WORLD_COLLISION_EPSILON) || !equalf(collision.end_y, entity->collider->y, WORLD_COLLISION_EPSILON)) { world_move(&GAME->world, entity->collider, collision.end_x, collision.end_y); } } } player_camera_step(GAME->player, &GAME->camera); PROGRAM->tx = -GAME->camera.x + (SCREEN_WIDTH >> 1); PROGRAM->ty = -GAME->camera.y + (SCREEN_HEIGHT >> 1); // // Update animations // if ((PROGRAM->frame % 3) == 0) { Animation *ani = GAME->animations; for (i = 0; i != GAME->animations_count; ++i, ++ani) { ani->sprite++; if (ani->sprite == ani->end) { ani->sprite = ani->begin; } } } // // Draw // tilemap_draw(res_level_0_layer1, res_image_set_main, 0, 0); Collider *collider; entity = GAME->entities; for (i = 0; i != GAME->entities_count; ++i, ++entity) { collider = entity->collider; rect_draw(v4i_make_size(roundf(collider->x - collider->w), roundf(collider->y - collider->h), collider->w * 2, collider->h * 2), COLOR_SHADE_3); // image_set_draw(collider->x + entity->sprite_ox, collider->y + entity->sprite_oy, // entity->animation->set, // entity->animation->sprite, // entity->animation->sprite_mode, 0); } // // Debug // debug_world_colliders_count(&GAME->world); PROGRAM->tx = 0; PROGRAM->ty = 0; debug_buttons(4, SCREEN_HEIGHT - res_icons->ch - 4); debug_fps(SCREEN_WIDTH - (7 * res_font->cw) - 4, SCREEN_HEIGHT - res_font->ch - 4, PROGRAM->time_step); debug_world_bucket_stats(&GAME->world, 4, 4); debug_world_bucket_cells(&GAME->world, SCREEN_WIDTH - 4 - 16, 4); }