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

Last change on this file since 544 was 544, checked in by Sam Hocevar, 15 years ago
  • Ported to new libcaca/libcucul API.
  • Property svn:keywords set to Id
File size: 3.6 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 544 2006-03-07 14:25:20Z 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
29struct cucul_sprite *foo_sprite;
30struct cucul_sprite *bar_sprite;
31struct cucul_sprite *baz_sprite;
32
33void init_aliens(game *g, aliens *al)
34{
35    int i;
36
37    for(i = 0; i < ALIENS; i++)
38    {
39        al->type[i] = ALIEN_NONE;
40    }
41
42    foo_sprite = cucul_load_sprite(g->qq, "data/foofight.txt");
43    bar_sprite = cucul_load_sprite(g->qq, "data/barfight.txt");
44    baz_sprite = cucul_load_sprite(g->qq, "data/bazfight.txt");
45}
46
47void draw_aliens(game *g, aliens *al)
48{
49    int i;
50
51    for(i = 0; i < ALIENS; i++)
52    {
53        switch(al->type[i])
54        {
55            case ALIEN_FOO:
56                cucul_draw_sprite(g->qq, al->x[i], al->y[i], foo_sprite, al->img[i] % 5);
57                break;
58            case ALIEN_BAR:
59                cucul_draw_sprite(g->qq, al->x[i], al->y[i], bar_sprite, al->img[i] % 2);
60                break;
61            case ALIEN_BAZ:
62                cucul_draw_sprite(g->qq, al->x[i], al->y[i], baz_sprite, al->img[i] % 4);
63                break;
64            case ALIEN_NONE:
65                break;
66        }
67    }
68}
69
70void update_aliens(game *g, aliens *al)
71{
72    int i;
73
74    for(i = 0; i < ALIENS; i++)
75    {
76        /* If alien died, make it explode */
77        if(al->type[i] != ALIEN_NONE && al->life[i] < 0)
78        {
79            add_explosion(g, g->ex, al->x[i], al->y[i], 0, 0, EXPLOSION_MEDIUM);
80            al->type[i] = ALIEN_NONE;
81            add_bonus(g, g->bo, al->x[i], al->y[i], cucul_rand(0,4) ? BONUS_GREEN : BONUS_LIFE);
82        }
83
84        /* Update coordinates */
85        switch(al->type[i])
86        {
87            case ALIEN_FOO:
88            case ALIEN_BAR:
89            case ALIEN_BAZ:
90                al->x[i] = ((al->x[i] + 5) % (g->w + 3)) - 3;
91                al->y[i] = al->y[i] + (rand() % 8) / 7 - (rand() % 8) / 7;
92                al->img[i] = al->img[i] + 1;
93
94                /* Check bounds */
95                if(al->y[i] < 0 ) al->y[i] = 0;
96                if(al->y[i] > g->w - 1 ) al->y[i] = g->w - 1;
97                break;
98            case ALIEN_NONE:
99                break;
100        }
101    }
102}
103
104void add_alien(game *g, aliens *al, int x, int y, int type)
105{
106    int i;
107
108    for(i = 0; i < ALIENS; i++)
109    {
110        if(al->type[i] == ALIEN_NONE)
111        {
112            al->type[i] = type;
113            al->x[i] = x;
114            al->y[i] = y;
115            al->img[i] = 0;
116
117            switch(al->type[i])
118            {
119                case ALIEN_FOO:
120                    al->life[i] = 3;
121                    break;
122                case ALIEN_BAR:
123                    al->life[i] = 3;
124                    break;
125                case ALIEN_BAZ:
126                    al->life[i] = 3;
127                    break;
128                case ALIEN_NONE:
129                    break;
130            }
131
132            break;
133        }
134    }
135}
136
Note: See TracBrowser for help on using the repository browser.