source: ttyvaders/trunk/src/player.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: 2.2 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: player.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 *ship_sprite;
30
31/* Init tunnel */
32player * create_player(game *g)
33{
34    cucul_buffer_t *b;
35
36    player *p = malloc(sizeof(player));
37    if(p == NULL)
38        exit(1);
39
40    p->x = g->w / 2;
41    p->y = g->h - 3;
42    p->vx = 0;
43    p->vy = 0;
44    p->weapon = 0;
45    p->special = MAX_SPECIAL;
46    p->life = MAX_LIFE;
47    p->dead = 0;
48
49    b = cucul_load_file("data/ship.caca");
50    ship_sprite = cucul_import_canvas(b, "");
51    cucul_free_buffer(b);
52
53    return p;
54}
55
56void free_player(player *p)
57{
58    free(p);
59}
60
61void draw_player(game *g, player *p)
62{
63    if(p->dead)
64        return;
65
66    cucul_set_canvas_frame(ship_sprite, 0);
67    cucul_blit(g->cv, p->x, p->y, ship_sprite, NULL);
68}
69
70void update_player(game *g, player *p)
71{
72    if(p->dead)
73        return;
74
75    if(p->life <= 0)
76    {
77        add_explosion(g, g->ex, p->x, p->y, 0, 0, EXPLOSION_SMALL);
78        p->dead = 1;
79        return;
80    }
81
82    /* Update weapon stats */
83    if(p->weapon)
84        p->weapon--;
85
86    if(p->special < MAX_SPECIAL)
87        p->special++;
88
89    /* Update life */
90    if(p->life < MAX_LIFE)
91        p->life++;
92
93    /* Update coords */
94    p->x += p->vx;
95
96    if(p->vx < 0)
97        p->vx++;
98    else if(p->vx > 0)
99        p->vx--;
100
101    if(p->x < 1)
102        p->x = 1;
103    else if(p->x > g->w - 7)
104        p->x = g->w - 7;
105}
106
Note: See TracBrowser for help on using the repository browser.