-
Notifications
You must be signed in to change notification settings - Fork 0
/
level.c
135 lines (120 loc) · 3.48 KB
/
level.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
* The Virus, a ZX Spectrum game.
* Copyright (C) 2022 Derek Fountain
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <stdint.h>
#include <stdlib.h>
#include <arch/zx.h>
#include "level.h"
#include "levels.h"
#include "main.h"
#include "int.h"
#include "swarm.h"
#include "print_str.h"
#include "timer.h"
#include "sound.h"
uint8_t current_frame;
uint16_t frames_before_change;
void init_level( LEVEL *level )
{
/*
* Reset timer. Start at 1, not zero, so we don't get
* an immediate update on the first loop
*/
interrupt_service_required_100ms = 1;
zx_border( level->border_colour );
SET_SWARM_SIZE( level->starting_num_virions );
current_frame = 0;
frames_before_change = 0;
(level->level_handler)( level, PHASE_INIT );
draw_timer( 1 );
if( level->caption )
{
roll_str(11,level->caption);
}
return;
}
void finalise_level( LEVEL *level )
{
/*
* The design is to call this:
* (level->level_handler)( level, PHASE_FINALISE );
* which allows the level code to tidy up after itself. But in
* practise the levels only ever (maybe) allocate some memory
* so I can get away with this:
*/
if( level->level_data != NULL )
free( level->level_data );
}
void apply_virion_logic( LEVEL *level, VIRION *v )
{
if( ! v->active )
return;
if( v->x < 0 || v->x > 255
||
v->y < 0 || v->y > 183 )
return;
uint8_t attribute = *(zx_pxy2aaddr(v->x, v->y));
if( (attribute & BRIGHT) == 0 )
return;
if( attribute == (PAPER_RED|BRIGHT) )
{
/* Deactivate this one */
deactivate_virion( v );
kill_virion_sound();
}
else if( attribute == (PAPER_GREEN|BRIGHT) )
{
if( GET_ACTIVE_SWARM_SIZE < level->max_virions )
{
/* Active any currently inactive virion in the swarm */
VIRION *reactivated_virion = activate_virion_in_swarm();
if( reactivated_virion != INVALID_VIRION_PTR )
{
random_reappear_virion( reactivated_virion );
reactivate_virion_sound();
}
}
}
else if( attribute == (PAPER_BLUE|BRIGHT) )
{
random_reappear_virion( v );
relocate_virion_sound();
}
else if( attribute == (PAPER_BLACK|BRIGHT) )
{
/*
* I'd prefer a better "bounce" dynamic, but it's expensive to work out
* where the virion has come from, how it's hit the black block (i.e.
* which side of the cell) and hence which direction it should bounce
* off in. Putting it back where it came from pointing the other way
* is best I can do.
*/
v->velocity_x = -(v->velocity_x);
v->velocity_y = -(v->velocity_y);
v->x = v->previous_x;
v->y = v->previous_y;
}
}
void update_level( LEVEL *level )
{
if( interrupt_service_required_100ms )
{
interrupt_service_required_100ms = 0;
(level->level_handler)( level, PHASE_UPDATE );
}
}