Skip to content
This repository
Browse code

added special item sound effect when a heart is acquired

  • Loading branch information...
commit 00d7d03369caf02573cfda5265b470e9e2084dcd 1 parent 6f839db
davewx7 authored
5 item.cpp
@@ -4,6 +4,7 @@
4 4 #include "formatter.hpp"
5 5 #include "item.hpp"
6 6 #include "level.hpp"
  7 +#include "sound.hpp"
7 8 #include "wml_node.hpp"
8 9 #include "wml_utils.hpp"
9 10
@@ -44,6 +45,10 @@ void item::process(level& lvl)
44 45 time_in_frame_ = 0;
45 46 touched_ = true;
46 47
  48 + if(type_->on_touch_music().empty() == false) {
  49 + sound::play_music_interrupt(type_->on_touch_music());
  50 + }
  51 +
47 52 for(int n = 0; n != type_->num_on_touch_particles(); ++n) {
48 53 const_custom_object_type_ptr particle_type = custom_object_type::get(type_->on_touch_particles());
49 54 if(!particle_type) {
1  item_type.cpp
@@ -31,6 +31,7 @@ item_type::item_type(wml::const_node_ptr node)
31 31 frame_(node->get_child("animation")),
32 32 destroy_on_touch_(wml::get_bool(node, "destroy_on_touch", false)),
33 33 automatic_touch_(wml::get_bool(node, "automatic_touch", true)),
  34 + on_touch_music_(node->attr("on_touch_music")),
34 35 touch_condition_(game_logic::formula::create_optional_formula(node->attr("touch_condition"))),
35 36 on_touch_particles_(node->attr("on_touch_particles")),
36 37 num_on_touch_particles_(wml::get_int(node, "num_particles"))
2  item_type.hpp
@@ -33,6 +33,7 @@ class item_type
33 33
34 34 const std::string& on_touch_particles() const { return on_touch_particles_; }
35 35 int num_on_touch_particles() const { return num_on_touch_particles_; }
  36 + const std::string& on_touch_music() const { return on_touch_music_; }
36 37
37 38 private:
38 39 std::string id_;
@@ -40,6 +41,7 @@ class item_type
40 41 boost::shared_ptr<frame> touched_frame_;
41 42 bool destroy_on_touch_;
42 43 bool automatic_touch_;
  44 + std::string on_touch_music_;
43 45 std::map<std::string, game_logic::const_formula_ptr> on_touch_;
44 46 game_logic::const_formula_ptr touch_condition_;
45 47 std::string on_touch_particles_;
1  items.cfg
@@ -94,6 +94,7 @@
94 94 destroy_on_touch=yes
95 95 on_touch_particles=heart_sparkle
96 96 num_particles=12
  97 + on_touch_music=specialitem.ogg
97 98 [ontouch]
98 99 hitpoints="hitpoints + 1"
99 100 max_hitpoints="max_hitpoints + 1"
0  sounds/specialitem.ogg → music/specialitem.ogg
File renamed without changes
26 sound.cpp
@@ -25,6 +25,7 @@ typedef std::map<std::string, Mix_Chunk*> cache_map;
25 25 cache_map cache;
26 26
27 27 Mix_Music* current_music = NULL;
  28 +std::string current_music_name;
28 29 std::string next_music;
29 30
30 31 //function which gets called when music finishes playing. It starts playing
@@ -34,7 +35,9 @@ void on_music_finished()
34 35 std::cerr << "music finished...\n";
35 36 Mix_FreeMusic(current_music);
36 37 current_music = NULL;
37   - play_music(next_music);
  38 + if(next_music.empty() == false) {
  39 + play_music(next_music);
  40 + }
38 41 next_music.clear();
39 42 }
40 43
@@ -102,6 +105,7 @@ void play_music(const std::string& file)
102 105 return;
103 106 }
104 107
  108 + current_music_name = file;
105 109 current_music = Mix_LoadMUS(("music/" + file).c_str());
106 110 if(!current_music) {
107 111 std::cerr << "Mix_LaadMUS ERROR loading " << file << ": " << Mix_GetError() << "\n";
@@ -113,4 +117,24 @@ void play_music(const std::string& file)
113 117 Mix_FadeInMusic(current_music, -1, 1000);
114 118 }
115 119
  120 +void play_music_interrupt(const std::string& file)
  121 +{
  122 + if(next_music.empty() == false) {
  123 + current_music_name = next_music;
  124 + next_music.clear();
  125 + }
  126 +
  127 + Mix_HaltMusic();
  128 +
  129 + next_music = current_music_name;
  130 +
  131 + current_music = Mix_LoadMUS(("music/" + file).c_str());
  132 + if(!current_music) {
  133 + std::cerr << "Mix_LaadMUS ERROR loading " << file << ": " << Mix_GetError() << "\n";
  134 + return;
  135 + }
  136 +
  137 + Mix_PlayMusic(current_music, 1);
  138 +}
  139 +
116 140 }
1  sound.hpp
@@ -14,6 +14,7 @@ bool ok();
14 14
15 15 void play(const std::string& file);
16 16 void play_music(const std::string& file);
  17 +void play_music_interrupt(const std::string& file);
17 18
18 19 }
19 20

0 comments on commit 00d7d03

Please sign in to comment.
Something went wrong with that request. Please try again.