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 2990 2008-10-18 21:42:24Z sam $ |
---|
7 | * |
---|
8 | * This program is free software. It comes without any warranty, to |
---|
9 | * the extent permitted by applicable law. You can redistribute it |
---|
10 | * and/or modify it under the terms of the Do What The Fuck You Want |
---|
11 | * To Public License, Version 2, as published by Sam Hocevar. See |
---|
12 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
13 | */ |
---|
14 | |
---|
15 | #include "config.h" |
---|
16 | |
---|
17 | #include <stdlib.h> |
---|
18 | |
---|
19 | #include "common.h" |
---|
20 | |
---|
21 | caca_canvas_t *ship_sprite; |
---|
22 | |
---|
23 | /* Init tunnel */ |
---|
24 | player * create_player(game *g) |
---|
25 | { |
---|
26 | caca_buffer_t *b; |
---|
27 | |
---|
28 | player *p = malloc(sizeof(player)); |
---|
29 | if(p == NULL) |
---|
30 | exit(1); |
---|
31 | |
---|
32 | p->x = g->w / 2; |
---|
33 | p->y = g->h - 3; |
---|
34 | p->vx = 0; |
---|
35 | p->vy = 0; |
---|
36 | p->weapon = 0; |
---|
37 | p->special = MAX_SPECIAL; |
---|
38 | p->life = MAX_LIFE; |
---|
39 | p->dead = 0; |
---|
40 | |
---|
41 | b = caca_load_file("data/ship.caca"); |
---|
42 | ship_sprite = caca_import_canvas(b, ""); |
---|
43 | caca_free_buffer(b); |
---|
44 | |
---|
45 | return p; |
---|
46 | } |
---|
47 | |
---|
48 | void free_player(player *p) |
---|
49 | { |
---|
50 | free(p); |
---|
51 | } |
---|
52 | |
---|
53 | void draw_player(game *g, player *p) |
---|
54 | { |
---|
55 | if(p->dead) |
---|
56 | return; |
---|
57 | |
---|
58 | caca_set_canvas_frame(ship_sprite, 0); |
---|
59 | caca_blit(g->cv, p->x, p->y, ship_sprite, NULL); |
---|
60 | } |
---|
61 | |
---|
62 | void update_player(game *g, player *p) |
---|
63 | { |
---|
64 | if(p->dead) |
---|
65 | return; |
---|
66 | |
---|
67 | if(p->life <= 0) |
---|
68 | { |
---|
69 | add_explosion(g, g->ex, p->x, p->y, 0, 0, EXPLOSION_SMALL); |
---|
70 | p->dead = 1; |
---|
71 | return; |
---|
72 | } |
---|
73 | |
---|
74 | /* Update weapon stats */ |
---|
75 | if(p->weapon) |
---|
76 | p->weapon--; |
---|
77 | |
---|
78 | if(p->special < MAX_SPECIAL) |
---|
79 | p->special++; |
---|
80 | |
---|
81 | /* Update life */ |
---|
82 | if(p->life < MAX_LIFE) |
---|
83 | p->life++; |
---|
84 | |
---|
85 | /* Update coords */ |
---|
86 | p->x += p->vx; |
---|
87 | |
---|
88 | if(p->vx < 0) |
---|
89 | p->vx++; |
---|
90 | else if(p->vx > 0) |
---|
91 | p->vx--; |
---|
92 | |
---|
93 | if(p->x < 1) |
---|
94 | p->x = 1; |
---|
95 | else if(p->x > g->w - 7) |
---|
96 | p->x = g->w - 7; |
---|
97 | } |
---|
98 | |
---|