void move_enemies(GameState* state) { register int i; for (i = 0; i < state->enemies_length; i++) { Enemy* en = &state->enemies[i].enemy; if (en->alive) { int to_x = convert_tile2world_x(en->path.nodes[en->path.current_node_index].tile_x); int to_y = convert_tile2world_y(en->path.nodes[en->path.current_node_index].tile_y); //@euclidean_distance: laat een kleine speling toe if (euclidean_distance(en->world_x, en->world_y, to_x, to_y) < 2 && en->path.current_node_index > 0) { //enemy heeft tile bereikt; verzet zijn doel naar de volgende node en->path.current_node_index--; } calc_direction(en->world_x, en->world_y, to_x, to_y, &en->direction_x, &en->direction_y); en->angle = find_render_angle(0, en->direction_x, en->direction_y); en->world_x += en->speed * en->direction_x; en->world_y += en->speed * en->direction_y; if (en->world_x == state->world.castle->castle.world_x && en->world_y == state->world.castle->castle.world_y) { //enemy heeft kasteel bereikt, trek levenspunten af en despawn enemy state->world.castle->castle.health -= en->damage; en->alive = 0; if (state->world.castle->castle.health <= 0) { //in geval dat health onder 0 zou staan state->world.castle->castle.health = 0; state->game_over = 1; } } } } }
void move_projectiles(GameState* state) { register int i, j; for (i = 0; i < state->towers_length; i++) { for (j = 0; j < state->towers[i]->tower.ammo; j++) { Projectile* proj = &state->towers[i]->tower.projectiles[j]; int from_x = proj->world_x; int from_y = proj->world_y; if (proj->target != NULL && proj->live) { int to_x = proj->target->world_x; int to_y = proj->target->world_y; float alpha, a, b, c; calc_direction(from_x, from_y, to_x, to_y, &proj->direction_x, &proj->direction_y); a = fabs(proj->target->world_x - proj->world_x); b = fabs(proj->target->world_y - proj->world_y); c = euclidean_distance(proj->target->world_x, proj->target->world_y, proj->world_x, proj->world_y); alpha = find_alpha(a, b, c); proj->world_x += proj->speed * proj->direction_x; proj->world_y += proj->speed * proj->direction_y; proj->angle = find_render_angle(alpha, proj->direction_x, proj->direction_y); } else{ //verwijder stilstaande projectielen proj->live = 0; } } } }
static void VS_CC proc_edge_detect(float *buff, int bstride, const float *srcp, float *dstp, uint8_t *direction,int width, int height, int stride) { float *p0 = buff; float *p1 = p0 + bstride; float *p2 = p1 + bstride; float *orig = p0, *end = p2; line_copyf(p0, srcp, width, 1); srcp += stride; line_copyf(p1, srcp, width, 1); memset(dstp, 0, stride * sizeof(float)); for (int y = 1; y < height - 1; y++) { srcp += stride; line_copyf(p2, srcp, width, 1); for (int x = 0; x < width; x++) { float gx = p1[x + 1] - p1[x - 1]; float gy = p0[x] - p2[x]; direction[x] = calc_direction(gx, gy); dstp[x] = sqrtf(gx * gx + gy * gy); } dstp += stride; direction += stride; p0 = p1; p1 = p2; p2 = (p2 == end) ? orig : p2 + bstride; } memset(dstp, 0, stride * sizeof(float)); }
//implemetation of functions int start_finding(int start_x, int start_y) { int inter = 0; int token = 0; int cur_x = start_x, cur_y = start_y; int dir = SOUTH; int ppath = -1; int npop = 0; //how many points should be poped struct POINT *cur_p = NULL; struct POINT *tmp_p = NULL; int ret = 0; #ifdef DEBUG inter = Robot_GetIntersections(); #else inter = get_intersection(); #endif cur_p = mark_point(cur_x, cur_y, inter); dir = get_direction(cur_p); #ifdef DEBUG printf("start point: "); print_point(cur_p); printf("\n"); #endif while(token < TOKEN_COUNT) { #ifdef DEBUG //inter = Robot_GetIntersections(); //print_intersection(inter); #endif if(points[cur_x][cur_y].detected == 0) cur_p = mark_point(cur_x, cur_y, inter); else cur_p = &points[cur_x][cur_y]; push(cur_p); //print_stack(); if(dir = get_direction(cur_p)) { //update current point switch(dir) { case EAST: cur_x += 1; break; case SOUTH: cur_y -= 1; break; case WEST: cur_x -= 1; break; case NORTH: cur_y += 1; break; } #ifdef DEBUG print_direction(cur_p, dir); ret = aud_move(cur_p, dir); #else //move one step display_clear(0); display_goto_xy(0, 0); display_int(cur_p->x, 2); display_goto_xy(3, 0); display_int(cur_p->y, 2); display_goto_xy(7, 0); display_int(cur_x, 2); display_goto_xy(10, 0); display_int(cur_y, 2); display_goto_xy(0, 1); display_int(g_dir, 3); display_goto_xy(5, 1); display_int(dir, 3); display_goto_xy(0, 2); display_int(cur_p->inter&0xF0, 3); display_update(); ret = move(cur_x, cur_y); #endif #ifdef DEBUG inter = Robot_GetIntersections(); #else inter = get_intersection(); #endif cur_p = mark_point(cur_x, cur_y, inter); #ifdef DEBUG print_point(cur_p); #endif if(ret == ROBOT_SUCCESS) { #ifndef DEBUG #endif } else if(ret == ROBOT_TOKENFOUND) { tmp_p = &points[cur_x][cur_y]; if(tmp_p->has_token == 0) { tmp_p->has_token = 1; token++; #ifdef DEBUG printf("[%d. token]\n", token); #endif } else { #ifdef DEBUG printf("[not a new token]\n"); #endif } if(token == TOKEN_COUNT) { //all token were found, go back to start point #ifdef DEBUG printf("going back to start point......\n"); #endif push(cur_p); ppath = find_shortest_path(cur_p->x, cur_p->y, START_X, START_Y); if(ppath) { //going back to last open point ppath--; while(ppath >= 0) { tmp_p = shortest_path[ppath]; dir = calc_direction(cur_p->x, cur_p->y, tmp_p->x, tmp_p->y); #ifdef DEBUG print_point(tmp_p); printf("\n"); ROBOT_MOVE(tmp_p->x, tmp_p->y); #else display_clear(0); display_goto_xy(0, 0); display_int(cur_p->x, 2); display_goto_xy(3, 0); display_int(cur_p->y, 2); display_goto_xy(7, 0); display_int(tmp_p->x, 2); display_goto_xy(10, 0); display_int(tmp_p->y, 2); display_goto_xy(0, 1); display_int(g_dir, 3); display_goto_xy(5, 1); display_int(dir, 3); display_goto_xy(0, 2); display_int(cur_p->inter&0xF0, 3); display_update(); move(tmp_p->x, tmp_p->y); #endif cur_p = tmp_p; ppath--; } //delete the path in stack pop(npop + 1); cur_p = tmp_p; cur_x = cur_p->x; cur_y = cur_p->y; } #ifdef DEBUG printf("task finished!\n"); #else beep(); #endif break; } } else { #ifdef DEBUG printf("move failed!\n"); #endif } } else { //there is no ways forward, go back to nearest open point tmp_p = get_last_open_point(); npop = stack_pointer - get_stack_index(tmp_p->x, tmp_p->y); #ifdef DEBUG printf("going back to (%d, %d)\n", tmp_p->x, tmp_p->y); #endif if(tmp_p) { if((tmp_p->x == START_X) && (tmp_p->y == START_Y) && !IS_OPEN_POINT(points[tmp_p->x][tmp_p->y])) { #ifdef DEBUG return 0; #else stop_robot(); beep(); return 0; #endif } ppath = find_shortest_path(cur_p->x, cur_p->y, tmp_p->x, tmp_p->y); if(ppath) { //going back to last open point ppath--; while(ppath >= 0) { tmp_p = shortest_path[ppath]; dir = calc_direction(cur_p->x, cur_p->y, tmp_p->x, tmp_p->y); #ifdef DEBUG ROBOT_MOVE(tmp_p->x, tmp_p->y); #else display_clear(0); display_goto_xy(0, 0); display_int(cur_p->x, 2); display_goto_xy(3, 0); display_int(cur_p->y, 2); display_goto_xy(7, 0); display_int(tmp_p->x, 2); display_goto_xy(10, 0); display_int(tmp_p->y, 2); display_goto_xy(0, 1); display_int(g_dir, 3); display_goto_xy(5, 1); display_int(dir, 3); display_goto_xy(0, 2); display_int(cur_p->inter&0xF0, 3); display_update(); move(tmp_p->x, tmp_p->y); #endif cur_p = tmp_p; ppath--; } //delete the path in stack pop(npop + 1); cur_p = tmp_p; cur_x = cur_p->x; cur_y = cur_p->y; } else { //was already at every point and back to start point //task should be ended //that means, not enough token can be found #ifdef DEBUG printf("task ended without enough token were found.\n"); #endif break; } } } #ifdef DEBUG printf("\n"); #endif } return 0; }