A rouge-like rpg, heavily inspired on the binding of isaac. Running on a FRDM-K64F Mbed board. C++.

Dependencies:   mbed MotionSensor

Committer:
el17sm
Date:
Thu May 09 07:39:49 2019 +0000
Revision:
51:4d0cd75e7ed3
Parent:
46:f09711580d4a
Child:
56:ef9521b7ed78
Removed moving boolean from entity, finished commenting room.cpp

Who changed what in which revision?

UserRevisionLine numberNew contents of line
el17sm 33:4f3948dcd2f7 1 #include "Skull.h"
el17sm 36:92d131695e7c 2 #include <complex>
el17sm 33:4f3948dcd2f7 3 Skull::Skull(float pos_x, float pos_y)
el17sm 33:4f3948dcd2f7 4 {
el17sm 46:f09711580d4a 5 hp = 20;
el17sm 33:4f3948dcd2f7 6 attack = 1;
el17sm 33:4f3948dcd2f7 7
el17sm 36:92d131695e7c 8 _dash = false;
el17sm 36:92d131695e7c 9 _dash_counter = 0;
el17sm 36:92d131695e7c 10
el17sm 36:92d131695e7c 11 hitbox.width = 19;
el17sm 36:92d131695e7c 12 hitbox.height = 9;
el17sm 33:4f3948dcd2f7 13
el17sm 36:92d131695e7c 14 sprite_size.width = 21;
el17sm 36:92d131695e7c 15 sprite_size.height = 23;
el17sm 36:92d131695e7c 16 sprite_size.offset_x = -1;
el17sm 36:92d131695e7c 17 sprite_size.offset_y = -14;
el17sm 33:4f3948dcd2f7 18
el17sm 36:92d131695e7c 19 _shadow.width = 19;
el17sm 36:92d131695e7c 20 _shadow.height = 5;
el17sm 36:92d131695e7c 21 _shadow.offset_x = 0;
el17sm 36:92d131695e7c 22 _shadow.offset_y = 5;
el17sm 36:92d131695e7c 23
el17sm 36:92d131695e7c 24 position.x = pos_x;
el17sm 36:92d131695e7c 25 position.y = pos_y;
el17sm 33:4f3948dcd2f7 26 update_prev_pos();
el17sm 33:4f3948dcd2f7 27
el17sm 33:4f3948dcd2f7 28 frame.count = 0;
el17sm 33:4f3948dcd2f7 29 frame.number = 0;
el17sm 36:92d131695e7c 30 frame.max = 2;
el17sm 37:a404860171a9 31 face = 2;
el17sm 33:4f3948dcd2f7 32
el17sm 36:92d131695e7c 33 velocity = 0.2;
el17sm 46:f09711580d4a 34 _hp_drop_chance = 0;
el17sm 33:4f3948dcd2f7 35 }
el17sm 33:4f3948dcd2f7 36
el17sm 34:1d5b4da3935e 37 void Skull::move(float player_x, float player_y, char * map, bool * doorways)
el17sm 33:4f3948dcd2f7 38 {
el17sm 36:92d131695e7c 39 // Approaching Movement
el17sm 36:92d131695e7c 40 if (_dash_counter < DASH_DELAY) {
el17sm 36:92d131695e7c 41 _dash = false;
el17sm 36:92d131695e7c 42 std::complex<double> pos_diff(player_x - position.x, player_y - position.y); // defining difference in position as a vector
el17sm 36:92d131695e7c 43 position.x += velocity * pos_diff.real() / std::abs(pos_diff);
el17sm 36:92d131695e7c 44 position.y += velocity * pos_diff.imag() / std::abs(pos_diff);
el17sm 36:92d131695e7c 45
el17sm 36:92d131695e7c 46 // Setting Face
el17sm 36:92d131695e7c 47 if (abs(pos_diff.real()) > abs(pos_diff.imag())) {
el17sm 36:92d131695e7c 48 if (pos_diff.real() > 0) {
el17sm 36:92d131695e7c 49 face = 1;
el17sm 36:92d131695e7c 50 } else {
el17sm 36:92d131695e7c 51 face = 3;
el17sm 36:92d131695e7c 52 }
el17sm 36:92d131695e7c 53 } else {
el17sm 36:92d131695e7c 54 if (pos_diff.imag() > 0) {
el17sm 36:92d131695e7c 55 face = 2;
el17sm 36:92d131695e7c 56 } else {
el17sm 36:92d131695e7c 57 face = 0;
el17sm 36:92d131695e7c 58 }
el17sm 36:92d131695e7c 59 }
el17sm 36:92d131695e7c 60 } else if (_dash_counter < DASH_DELAY + 28){
el17sm 36:92d131695e7c 61 _dash = true;
el17sm 36:92d131695e7c 62 velocity = skull_velocity_pattern[(int)((_dash_counter - DASH_DELAY)/4)];
el17sm 36:92d131695e7c 63 if (face == 0){
el17sm 36:92d131695e7c 64 position.y -= velocity;
el17sm 36:92d131695e7c 65 } else if (face == 1){
el17sm 36:92d131695e7c 66 position.x += velocity;
el17sm 36:92d131695e7c 67 } else if (face == 2){
el17sm 36:92d131695e7c 68 position.y += velocity;
el17sm 36:92d131695e7c 69 } else if (face == 3){
el17sm 36:92d131695e7c 70 position.x -= velocity;
el17sm 36:92d131695e7c 71 }
el17sm 36:92d131695e7c 72 } else {
el17sm 36:92d131695e7c 73 _dash_counter = 0;
el17sm 36:92d131695e7c 74 velocity = 0.2;
el17sm 36:92d131695e7c 75 }
el17sm 36:92d131695e7c 76 undo_move_x(entity_to_map_collision_test(position.x, prev_pos.y, map, doorways));
el17sm 36:92d131695e7c 77 undo_move_y(entity_to_map_collision_test(prev_pos.x, position.y, map, doorways));
el17sm 33:4f3948dcd2f7 78
el17sm 36:92d131695e7c 79 _dash_counter++;
el17sm 36:92d131695e7c 80 increment_frames();
el17sm 33:4f3948dcd2f7 81 }
el17sm 33:4f3948dcd2f7 82
el17sm 33:4f3948dcd2f7 83 void Skull::draw(N5110 &lcd)
el17sm 33:4f3948dcd2f7 84 {
el17sm 36:92d131695e7c 85 update_offsets();
el17sm 36:92d131695e7c 86 lcd.drawSpriteTransparent(position.x+_shadow.offset_x,
el17sm 36:92d131695e7c 87 position.y+_shadow.offset_y,
el17sm 36:92d131695e7c 88 _shadow.height,
el17sm 36:92d131695e7c 89 _shadow.width,
el17sm 36:92d131695e7c 90 (char *)skull_shadow_sprite[frame.number]);
el17sm 36:92d131695e7c 91 lcd.drawSpriteTransparent(position.x+sprite_size.offset_x,
el17sm 36:92d131695e7c 92 position.y+sprite_size.offset_y,
el17sm 36:92d131695e7c 93 sprite_size.height,
el17sm 36:92d131695e7c 94 sprite_size.width,
el17sm 36:92d131695e7c 95 (char *)skull_sprite[face][_dash]);
el17sm 33:4f3948dcd2f7 96 }
el17sm 33:4f3948dcd2f7 97
el17sm 33:4f3948dcd2f7 98 void Skull::take_damage(int damage)
el17sm 33:4f3948dcd2f7 99 {
el17sm 36:92d131695e7c 100 hp -= 1;
el17sm 36:92d131695e7c 101 }
el17sm 36:92d131695e7c 102
el17sm 36:92d131695e7c 103 // Private Functions
el17sm 36:92d131695e7c 104
el17sm 36:92d131695e7c 105 void Skull::increment_frames()
el17sm 36:92d131695e7c 106 {
el17sm 36:92d131695e7c 107 if (frame.number < frame.max) {
el17sm 36:92d131695e7c 108 frame.count++;
el17sm 36:92d131695e7c 109 } else {
el17sm 36:92d131695e7c 110 frame.count = 0;
el17sm 36:92d131695e7c 111 }
el17sm 36:92d131695e7c 112 frame.number = (frame.count/20) % frame.max;
el17sm 36:92d131695e7c 113 }
el17sm 36:92d131695e7c 114
el17sm 36:92d131695e7c 115 void Skull::update_offsets()
el17sm 36:92d131695e7c 116 {
el17sm 36:92d131695e7c 117 if (frame.number == 0) {
el17sm 36:92d131695e7c 118 sprite_size.offset_y = -14;
el17sm 36:92d131695e7c 119 } else if (frame.number == 1) {
el17sm 36:92d131695e7c 120 sprite_size.offset_y = -15;
el17sm 36:92d131695e7c 121 }
el17sm 33:4f3948dcd2f7 122 }