1 /*
2  * ring.c - rings
3  * Copyright (C) 2010  Alexandre Martins <alemartf(at)gmail(dot)com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #include <math.h>
21 #include "ring.h"
22 #include "../../scenes/level.h"
23 #include "../../core/util.h"
24 #include "../../core/timer.h"
25 #include "../../core/audio.h"
26 #include "../../core/soundfactory.h"
27 
28 /* ring class */
29 typedef struct ring_t ring_t;
30 struct ring_t {
31     item_t item; /* base class */
32     int is_disappearing; /* is this ring disappearing? */
33     int is_moving; /* is this ring moving (bouncing) around? */
34     float life_time; /* life time (used to destroy the moving ring after some time) */
35 };
36 
37 static void ring_init(item_t *item);
38 static void ring_release(item_t* item);
39 static void ring_update(item_t* item, player_t** team, int team_size, brick_list_t* brick_list, item_list_t* item_list, enemy_list_t* enemy_list);
40 static void ring_render(item_t* item, v2d_t camera_position);
41 
42 
43 
44 /* public methods */
ring_create()45 item_t* ring_create()
46 {
47     item_t *item = mallocx(sizeof(ring_t));
48 
49     item->init = ring_init;
50     item->release = ring_release;
51     item->update = ring_update;
52     item->render = ring_render;
53 
54     return item;
55 }
56 
ring_start_bouncing(item_t * ring)57 void ring_start_bouncing(item_t *ring)
58 {
59     ring_t *me = (ring_t*)ring;
60 
61     me->is_moving = TRUE;
62     ring->actor->speed.x = ring->actor->maxspeed * (random(100)-50)/100;
63     ring->actor->speed.y = -ring->actor->jump_strength + random(ring->actor->jump_strength);
64 }
65 
66 
67 
68 /* private methods */
ring_init(item_t * item)69 void ring_init(item_t *item)
70 {
71     ring_t *me = (ring_t*)item;
72 
73     item->obstacle = FALSE;
74     item->bring_to_back = FALSE;
75     item->preserve = TRUE;
76     item->actor = actor_create();
77     item->actor->maxspeed = 220 + random(140);
78     item->actor->jump_strength = (350 + random(50)) * 1.2;
79     item->actor->input = input_create_computer();
80 
81     me->is_disappearing = FALSE;
82     me->is_moving = FALSE;
83     me->life_time = 0.0f;
84 
85     actor_change_animation(item->actor, sprite_get_animation("SD_RING", 0));
86 }
87 
88 
89 
ring_release(item_t * item)90 void ring_release(item_t* item)
91 {
92     actor_destroy(item->actor);
93 }
94 
95 
96 
ring_update(item_t * item,player_t ** team,int team_size,brick_list_t * brick_list,item_list_t * item_list,enemy_list_t * enemy_list)97 void ring_update(item_t* item, player_t** team, int team_size, brick_list_t* brick_list, item_list_t* item_list, enemy_list_t* enemy_list)
98 {
99     int i;
100     float dt = timer_get_delta();
101     ring_t *me = (ring_t*)item;
102     actor_t *act = item->actor;
103 
104     /* a player has just got this ring */
105     for(i=0; i<team_size; i++) {
106         player_t *player = team[i];
107         if(
108             (!me->is_moving || (me->is_moving && !player->getting_hit && me->life_time >= 0.5f)) &&
109             !me->is_disappearing &&
110             !player->dying &&
111             actor_collision(act, player->actor)
112         ) {
113             player_set_rings(player_get_rings() + 1);
114             me->is_disappearing = TRUE;
115             sound_play( soundfactory_get("ring") );
116             break;
117         }
118     }
119 
120     /* disappearing animation... */
121     if(me->is_disappearing) {
122         actor_change_animation(act, sprite_get_animation("SD_RING", 1));
123         if(actor_animation_finished(act))
124             item->state = IS_DEAD;
125     }
126 
127     /* this ring is bouncing around... */
128     else if(me->is_moving) {
129         float sqrsize = 2, diff = -2;
130         brick_t *left = NULL, *right = NULL, *down = NULL;
131         actor_corners(act, sqrsize, diff, brick_list, NULL, NULL, &right, NULL, &down, NULL, &left, NULL);
132         actor_handle_clouds(act, diff, NULL, NULL, &right, NULL, &down, NULL, &left, NULL);
133         input_simulate_button_down(act->input, IB_FIRE1);
134         item->preserve = FALSE;
135 
136         /* who wants to live forever? */
137         me->life_time += dt;
138         if(me->life_time > 5.0f) {
139             int val = 240 + random(20);
140             act->visible = ((int)(timer_get_ticks() % val) < val/2);
141             if(me->life_time > 8.0f)
142                 item->state = IS_DEAD;
143         }
144 
145         /* keep moving... */
146         if(right && act->speed.x > 0.0f)
147             act->speed.x = -fabs(act->speed.x);
148 
149         if(left && act->speed.x < 0.0f)
150             act->speed.x = fabs(act->speed.x);
151 
152         if(down && act->speed.y > 0.0f)
153             act->jump_strength *= 0.95f;
154 
155         actor_move(act, actor_platform_movement(act, brick_list, level_gravity()));
156     }
157 
158     /* this ring is being attracted by the thunder shield */
159     else if(level_player()->shield_type == SH_THUNDERSHIELD) {
160         float threshold = 120.0f;
161         v2d_t diff = v2d_subtract(level_player()->actor->position, act->position);
162         if(v2d_magnitude(diff) < threshold) {
163             float speed = 320.0f;
164             v2d_t d = v2d_multiply(v2d_normalize(diff), speed);
165             act->position.x += d.x * dt;
166             act->position.y += d.y * dt;
167         }
168     }
169 }
170 
171 
ring_render(item_t * item,v2d_t camera_position)172 void ring_render(item_t* item, v2d_t camera_position)
173 {
174     actor_render(item->actor, camera_position);
175 }
176 
177