Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed MotionSensor
Skull.cpp
00001 #include "Skull.h" 00002 #include <complex> 00003 Skull::Skull(float pos_x, float pos_y) 00004 { 00005 _hp = 20; 00006 _attack = 1; 00007 00008 _dash = false; 00009 _dash_counter = 0; 00010 00011 _hitbox.width = 19; 00012 _hitbox.height = 9; 00013 00014 _sprite_size.width = 21; 00015 _sprite_size.height = 23; 00016 _sprite_size.offset_x = -1; 00017 _sprite_size.offset_y = -14; 00018 00019 _shadow.width = 19; 00020 _shadow.height = 5; 00021 _shadow.offset_x = 0; 00022 _shadow.offset_y = 5; 00023 00024 _position.x = pos_x; 00025 _position.y = pos_y; 00026 update_prev_pos(); 00027 00028 _frame.count = 0; 00029 _frame.number = 0; 00030 _frame.max = 2; 00031 _face = 2; 00032 00033 _velocity = 0.2; 00034 _hp_drop_chance = 0; 00035 } 00036 00037 void Skull::move(float player_x, float player_y, char * map, bool * doorways) 00038 { 00039 if (_dash_counter < DASH_DELAY) { // Approaching Movement 00040 approaching_movement(player_x, player_y); 00041 } else if (_dash_counter < DASH_DELAY + 28){ // Dashing movement; const 28 = 4(velocity_index_increment_delay) * 7 (length of the velocity_pattern) 00042 dash_movement(); 00043 } else { 00044 _dash_counter = 0; 00045 _velocity = 0.2; 00046 } 00047 undo_move_x(entity_to_map_collision_test(_position.x, _prev_pos.y, map, doorways)); 00048 undo_move_y(entity_to_map_collision_test(_prev_pos.x, _position.y, map, doorways)); 00049 00050 _dash_counter++; 00051 increment_frames(); 00052 } 00053 00054 void Skull::approaching_movement(float player_x, float player_y) 00055 { 00056 _dash = false; 00057 std::complex<double> pos_diff(player_x - _position.x, player_y - _position.y); // defining difference in position as a vector for simplicity, similar to Headless 00058 _position.x += _velocity * pos_diff.real() / std::abs(pos_diff); 00059 _position.y += _velocity * pos_diff.imag() / std::abs(pos_diff); 00060 // Setting Face 00061 if (abs(pos_diff.real()) > abs(pos_diff.imag())) { 00062 if (pos_diff.real() > 0) { 00063 _face = 1; 00064 } else { 00065 _face = 3; 00066 } 00067 } else { 00068 if (pos_diff.imag() > 0) { 00069 _face = 2; 00070 } else { 00071 _face = 0; 00072 } 00073 } 00074 } 00075 00076 void Skull::dash_movement() // Changes velocity over time using the velocity pattern 00077 { 00078 _dash = true; 00079 _velocity = skull_velocity_pattern[(int)((_dash_counter - DASH_DELAY)/4)]; 00080 if (_face == 0){ 00081 _position.y -= _velocity; 00082 } else if (_face == 1){ 00083 _position.x += _velocity; 00084 } else if (_face == 2){ 00085 _position.y += _velocity; 00086 } else if (_face == 3){ 00087 _position.x -= _velocity; 00088 } 00089 } 00090 00091 void Skull::draw(N5110 &lcd) 00092 { 00093 update_offsets(); 00094 lcd.drawSpriteTransparent(_position.x+_shadow.offset_x, 00095 _position.y+_shadow.offset_y, 00096 _shadow.height, 00097 _shadow.width, 00098 (char *)skull_shadow_sprite[_frame.number]); 00099 lcd.drawSpriteTransparent(_position.x+_sprite_size.offset_x, 00100 _position.y+_sprite_size.offset_y, 00101 _sprite_size.height, 00102 _sprite_size.width, 00103 (char *)skull_sprite[_face][_dash]); 00104 } 00105 00106 void Skull::take_damage(int damage) 00107 { 00108 _hp -= 1; 00109 } 00110 00111 // Methods 00112 00113 void Skull::increment_frames() 00114 { 00115 if (_frame.number < _frame.max) { 00116 _frame.count++; 00117 } else { 00118 _frame.count = 0; 00119 } 00120 _frame.number = (_frame.count/20) % _frame.max; 00121 } 00122 00123 void Skull::update_offsets() // Animates the shadows by offsetting the skull from the shadow periodically 00124 { 00125 if (_frame.number == 0) { 00126 _sprite_size.offset_y = -14; 00127 } else if (_frame.number == 1) { 00128 _sprite_size.offset_y = -15; 00129 } 00130 }
Generated on Tue Jul 19 2022 23:32:07 by
 1.7.2
 1.7.2