source: libcaca/trunk/src/aliens.c @ 121

Last change on this file since 121 was 121, checked in by Sam Hocevar, 20 years ago
  • libee/sprite.c: + More robust sprite loader. + Added ee_set_sprite_frame() and ee_get_sprite_frame(). + Free all structures in ee_free_sprite().
  • src/aliens.c src/bonus.c: + Use ee_draw_sprite() instead of our manual sprite rendering.
  • src/box.c: + Use ee_draw_line() instead of the manual equivalent.
  • data/: + Added foo_fighter, baz_fighter, item_gem and item_heart sprites.
  • Property svn:keywords set to Id
File size: 4.1 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 121 2003-11-10 09:26:40Z 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
29static void draw_alien_foo(game *, int, int, int);
30static void draw_alien_bar(game *, int, int, int);
31static void draw_alien_baz(game *, int, int, int);
32
33struct ee_sprite *foo_sprite;
34struct ee_sprite *bar_sprite;
35struct ee_sprite *baz_sprite;
36
37void init_aliens(game *g, aliens *al)
38{
39    int i;
40
41    for(i = 0; i < ALIENS; i++)
42    {
43        al->type[i] = ALIEN_NONE;
44    }
45
46    foo_sprite = ee_load_sprite("data/foo_fighter");
47    bar_sprite = ee_load_sprite("data/bar_fighter");
48    baz_sprite = ee_load_sprite("data/baz_fighter");
49}
50
51void draw_aliens(game *g, aliens *al)
52{
53    int i;
54
55    for(i = 0; i < ALIENS; i++)
56    {
57        switch(al->type[i])
58        {
59            case ALIEN_FOO:
60                draw_alien_foo(g, al->x[i], al->y[i], al->img[i] % 8);
61                break;
62            case ALIEN_BAR:
63                draw_alien_bar(g, al->x[i], al->y[i], al->img[i] % 2);
64                break;
65            case ALIEN_BAZ:
66                draw_alien_baz(g, al->x[i], al->y[i], al->img[i] % 6);
67                break;
68            case ALIEN_NONE:
69                break;
70        }
71    }
72}
73
74void update_aliens(game *g, aliens *al)
75{
76    int i;
77
78    for(i = 0; i < ALIENS; i++)
79    {
80        /* If alien died, make it explode */
81        if(al->type[i] != ALIEN_NONE && al->life[i] < 0)
82        {
83            add_explosion(g, g->ex, al->x[i], al->y[i], 0, 0, EXPLOSION_MEDIUM);
84            al->type[i] = ALIEN_NONE;
85            add_bonus(g, g->bo, al->x[i], al->y[i], ee_rand(0,4) ? BONUS_GREEN : BONUS_LIFE);
86        }
87
88        /* Update coordinates */
89        switch(al->type[i])
90        {
91            case ALIEN_FOO:
92            case ALIEN_BAR:
93            case ALIEN_BAZ:
94                al->x[i] = ((al->x[i] + 5) % (g->w + 3)) - 3;
95                al->y[i] = al->y[i] + (rand() % 8) / 7 - (rand() % 8) / 7;
96                al->img[i] = al->img[i] + 1;
97
98                /* Check bounds */
99                if(al->y[i] < 0 ) al->y[i] = 0;
100                if(al->y[i] > g->w - 1 ) al->y[i] = g->w - 1;
101                break;
102            case ALIEN_NONE:
103                break;
104        }
105    }
106}
107
108void add_alien(game *g, aliens *al, int x, int y, int type)
109{
110    int i;
111
112    for(i = 0; i < ALIENS; i++)
113    {
114        if(al->type[i] == ALIEN_NONE)
115        {
116            al->type[i] = type;
117            al->x[i] = x;
118            al->y[i] = y;
119            al->img[i] = 0;
120
121            switch(al->type[i])
122            {
123                case ALIEN_FOO:
124                    al->life[i] = 3;
125                    break;
126                case ALIEN_BAR:
127                    al->life[i] = 3;
128                    break;
129                case ALIEN_BAZ:
130                    al->life[i] = 3;
131                    break;
132                case ALIEN_NONE:
133                    break;
134            }
135
136            break;
137        }
138    }
139}
140
141static void draw_alien_bar(game *g, int x, int y, int frame)
142{
143    ee_set_sprite_frame(bar_sprite, frame);
144    ee_draw_sprite(x, y, bar_sprite);
145}
146
147static void draw_alien_baz(game *g, int x, int y, int frame)
148{
149    ee_set_sprite_frame(baz_sprite, frame);
150    ee_draw_sprite(x, y, baz_sprite);
151}
152
153static void draw_alien_foo(game *g, int x, int y, int frame)
154{
155    ee_set_sprite_frame(foo_sprite, frame);
156    ee_draw_sprite(x, y, foo_sprite);
157}
158
159
Note: See TracBrowser for help on using the repository browser.