Project

General

Profile

Statistics
| Revision:

root / proj / src / zombies.c @ 345

History | View | Annotate | Download (4.81 KB)

1 345 up20180642
#include <lcom/lcf.h>
2
3
#include "zombies.h"
4
5
#include "proj.h"
6
7
#include "proj_macros.h"
8
#include "proj_func.h"
9
#include "ent.h"
10
#include "interrupts_func.h"
11
#include "graph.h"
12
#include "hltp.h"
13
#include "errors.h"
14
#include <math.h>
15
16
#define ZOMBIES_NUM             5
17
#define ZOMBIE_HEALTH_FACTOR    1.1
18
int (zombies)(void){
19
20
    int r;
21
22
    ent_set_scale(DEFAULT_SCALE);
23
    text_timer_t *in_game_timer = text_timer_ctor(font_get_default());
24
25
    list_t *shooter_list = list_ctor();
26
27
    gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol, GUNNER_PLAYER, 1); if(shooter1 == NULL) printf("Failed to get shooter1\n");
28
    gunner_set_spawn(shooter1, 980, 790);
29
    gunner_set_pos(shooter1, 980, 790);
30
31
    list_insert(shooter_list, list_end(shooter_list), shooter1);
32
33
    list_t *bullet_list  = list_ctor();
34
35
    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
36
    gunner_get_y(shooter1)-ent_get_YLength()/2.0);
37
38
    //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
39
    uint8_t last_lb = 0;
40
    struct packet pp;
41
    keys_t *keys = get_key_presses();
42
43
    /// loop stuff
44
    uint64_t int_vector = 0;
45
    int good = true;
46
    int dead = false;
47
48
    int health = 50;
49
50
    map_make_dijkstra(map1, gunner_get_x(shooter1), gunner_get_y(shooter1));
51
52
    while (good && !dead) {
53
        /* Get a request message. */
54
        if((r = get_interrupts_vector(&int_vector))) return r;
55
        uint32_t n = 1;
56
        for (uint8_t i = 0; i < 32; i++, n <<= 1) {
57
            if(!good || dead) break;
58
            if (int_vector & n) {
59
                interrupt_handler(i);
60
                switch (i) {
61
                    case TIMER0_IRQ:
62
                    if (timer_get_no_interrupts() % 60 == 0) text_timer_update(in_game_timer);
63
                    if (timer_get_no_interrupts() %  6 == 0){
64
                        map_make_dijkstra(map1, gunner_get_x(shooter1), gunner_get_y(shooter1));
65
                    }
66
67
                    update_movement(map1, shooter1, keys, shooter_list);
68
69
                    update_game_state(map1, shooter_list, bullet_list);
70
71
                    if(list_find(shooter_list, shooter1) == list_end(shooter_list)){
72
                        good = false;
73
                        dead = true;
74
                        break;
75
                    }
76
77
                    double angle = get_mouse_angle(shooter1);
78
                    gunner_set_angle(shooter1, angle - M_PI_2);
79
80
                    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
81
                                   gunner_get_y(shooter1)-ent_get_YLength()/2.0);
82
83
                    while(list_size(shooter_list) < ZOMBIES_NUM+1){
84
                        gunner_t *zombie = gunner_ctor(bsp_zombie, bsp_nothing, GUNNER_MELEE | GUNNER_FOLLOW, 3);
85
                        gunner_set_health(zombie, health);
86
                        gunner_set_curr_health(zombie, health);
87
                        health *= ZOMBIE_HEALTH_FACTOR;
88
                        get_random_spawn(map1, zombie, shooter_list);
89
                        list_push_back(shooter_list, zombie);
90
                    }
91
92
                    graph_clear_screen();
93
                    map_draw   (map1);
94
                    bullet_draw_list(bullet_list);
95
                    gunner_draw_list(shooter_list);
96
97
                    text_draw(in_game_timer->text);
98
99
                    sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
100
                    sprite_draw(sp_crosshair);
101
                    graph_draw();
102
103
                    break;
104
                    case KBC_IRQ:
105
                    if (keyboard_get_scancode()[0] == ESC_BREAK_CODE) {
106
                        good = false;
107
                    }
108
                    break;
109
                    case MOUSE_IRQ:
110
                    if (mouse_get_counter_mouse_ih() >= 3) {
111
                        mouse_parse_packet(mouse_get_packet_mouse_ih(), &pp);
112
                        update_mouse(&pp);
113
                        if (last_lb ^ keys->lb_pressed && keys->lb_pressed)
114
                        shoot_bullet(shooter1, bullet_list, bsp_bullet);
115
                        last_lb = keys->lb_pressed;
116
                        mouse_set_counter_mouse_ih(0);
117
118
                    }
119
                    break;
120
                    case COM1_IRQ: nctp_ih(); break;
121
                }
122
            }
123
        }
124
    }
125
126
    while(list_size(shooter_list) > 0){
127
        gunner_t *p = list_erase(shooter_list, list_begin(shooter_list));
128
        gunner_dtor(p);
129
    }
130
    if(list_dtor(shooter_list)) printf("COULD NOT DESTRUCT SHOOTER LIST\n");
131
132
    while(list_size(bullet_list) > 0){
133
        bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
134
        bullet_dtor(p);
135
    }
136
    if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n");
137
138
    if(dead){
139
        printf("YOU DIED\n");
140
    }
141
142
    text_timer_dtor(in_game_timer); in_game_timer = NULL;
143
144
    return SUCCESS;
145
}