source: ttyvaders/trunk/src/aliens.c @ 1057

Last change on this file since 1057 was 1057, checked in by Sam Hocevar, 14 years ago
  • Bring the tree back in sync with the new libcaca API.
  • Property svn:keywords set to Id
File size: 3.9 KB
Line 
1/*
2 *   ttyvaders     Textmode shoot'em up
3 *   Copyright (c) 2002 Sam Hocevar <sam@zoy.org>
4 *                 All Rights Reserved
5 *
6 *   $Id: aliens.c 1057 2006-09-18 16:54:08Z sam $
7 *
8 *   This program is free software; you can redistribute it and/or modify
9 *   it under the terms of the GNU General Public License as published by
10 *   the Free Software Foundation; either version 2 of the License, or
11 *   (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23#include "config.h"
24
25#include <stdlib.h>
26
27#include "common.h"
28
29cucul_canvas_t *foo_sprite;
30cucul_canvas_t *bar_sprite;
31cucul_canvas_t *baz_sprite;
32
33void init_aliens(game *g, aliens *al)
34{
35    cucul_buffer_t *b;
36    int i;
37
38    for(i = 0; i < ALIENS; i++)
39    {
40        al->type[i] = ALIEN_NONE;
41    }
42
43    b = cucul_load_file("data/foofight.caca");
44    foo_sprite = cucul_import_canvas(b, "");
45    cucul_free_buffer(b);
46
47    b = cucul_load_file("data/barfight.caca");
48    bar_sprite = cucul_import_canvas(b, "");
49    cucul_free_buffer(b);
50
51    b = cucul_load_file("data/bazfight.caca");
52    baz_sprite = cucul_import_canvas(b, "");
53    cucul_free_buffer(b);
54}
55
56void draw_aliens(game *g, aliens *al)
57{
58    int i;
59
60    for(i = 0; i < ALIENS; i++)
61    {
62        switch(al->type[i])
63        {
64            case ALIEN_FOO:
65                cucul_set_canvas_frame(foo_sprite, al->img[i] % 5);
66                cucul_blit(g->cv, al->x[i], al->y[i], foo_sprite, NULL);
67                break;
68            case ALIEN_BAR:
69                cucul_set_canvas_frame(bar_sprite, al->img[i] % 2);
70                cucul_blit(g->cv, al->x[i], al->y[i], bar_sprite, NULL);
71                break;
72            case ALIEN_BAZ:
73                cucul_set_canvas_frame(baz_sprite, al->img[i] % 4);
74                cucul_blit(g->cv, al->x[i], al->y[i], baz_sprite, NULL);
75                break;
76            case ALIEN_NONE:
77                break;
78        }
79    }
80}
81
82void update_aliens(game *g, aliens *al)
83{
84    int i;
85
86    for(i = 0; i < ALIENS; i++)
87    {
88        /* If alien died, make it explode */
89        if(al->type[i] != ALIEN_NONE && al->life[i] < 0)
90        {
91            add_explosion(g, g->ex, al->x[i], al->y[i], 0, 0, EXPLOSION_MEDIUM);
92            al->type[i] = ALIEN_NONE;
93            add_bonus(g, g->bo, al->x[i], al->y[i], cucul_rand(0,4) ? BONUS_GREEN : BONUS_LIFE);
94        }
95
96        /* Update coordinates */
97        switch(al->type[i])
98        {
99            case ALIEN_FOO:
100            case ALIEN_BAR:
101            case ALIEN_BAZ:
102                al->x[i] = ((al->x[i] + 5) % (g->w + 3)) - 3;
103                al->y[i] = al->y[i] + (rand() % 8) / 7 - (rand() % 8) / 7;
104                al->img[i] = al->img[i] + 1;
105
106                /* Check bounds */
107                if(al->y[i] < 0 ) al->y[i] = 0;
108                if(al->y[i] > g->w - 1 ) al->y[i] = g->w - 1;
109                break;
110            case ALIEN_NONE:
111                break;
112        }
113    }
114}
115
116void add_alien(game *g, aliens *al, int x, int y, int type)
117{
118    int i;
119
120    for(i = 0; i < ALIENS; i++)
121    {
122        if(al->type[i] == ALIEN_NONE)
123        {
124            al->type[i] = type;
125            al->x[i] = x;
126            al->y[i] = y;
127            al->img[i] = 0;
128
129            switch(al->type[i])
130            {
131                case ALIEN_FOO:
132                    al->life[i] = 3;
133                    break;
134                case ALIEN_BAR:
135                    al->life[i] = 3;
136                    break;
137                case ALIEN_BAZ:
138                    al->life[i] = 3;
139                    break;
140                case ALIEN_NONE:
141                    break;
142            }
143
144            break;
145        }
146    }
147}
148
Note: See TracBrowser for help on using the repository browser.