From a6b36fcb5d7a1bd424752ef5776c9f327de1f749 Mon Sep 17 00:00:00 2001 From: name Date: Wed, 3 Mar 2021 13:24:23 +0300 Subject: [PATCH] idle for enemies --- Enemy.cpp | 11 ++++++++++- LiveObjSprite.h | 7 +++---- SpriteManager.h | 9 ++++++++- resources/enemies/0/idle.png | Bin 0 -> 2241 bytes resources/enemies/1/idle.png | Bin 0 -> 1143 bytes 5 files changed, 21 insertions(+), 6 deletions(-) create mode 100644 resources/enemies/0/idle.png create mode 100644 resources/enemies/1/idle.png diff --git a/Enemy.cpp b/Enemy.cpp index cef6aee..75c06d8 100644 --- a/Enemy.cpp +++ b/Enemy.cpp @@ -23,6 +23,10 @@ void Enemy::Draw(Image &canvas) if (hp <= 0)alive = false; } + if (cur_state == E_LiveObjState::Walk) { + if (!mov.Moved())cur_state = E_LiveObjState::Idle; + } + int hp_x = p.x - (max_hp / 20) * 12; for (int i = 0; i * 10 < max_hp; i ++) { SpriteManager::Get().DrawHpBlock(canvas, {.x = hp_x + i * 12, .y = p.y - sz.h / 2 - 5}, hp - i * 10); @@ -57,7 +61,8 @@ void Enemy::Move(Point player_pos) if (!alive)return; int r = get_r(type); auto pos = mov.CenterPos(); - if ((player_pos.x + r * TILE_SZ < pos.x) || + if ((cur_state == E_LiveObjState::TakeHit || cur_state == E_LiveObjState::Attack) || + (player_pos.x + r * TILE_SZ < pos.x) || (player_pos.x - r * TILE_SZ > pos.x) || (player_pos.y + r * TILE_SZ < pos.y) || (player_pos.y - r * TILE_SZ > pos.y)) { @@ -67,8 +72,12 @@ void Enemy::Move(Point player_pos) bool x_eq = std::abs(player_pos.x - pos.x) < 3; bool pl_x_less = player_pos.x < pos.x; + bool y_eq = std::abs(player_pos.y - pos.y) < 3; bool pl_y_less = player_pos.y < pos.y; + if (!x_eq)cur_dir = pl_x_less ? E_Dir::LEFT : E_Dir::RIGHT; + mov.Move(x_eq ? 0 : pl_x_less ? -1 : 1, y_eq ? 0 : pl_y_less ? -1 : 1, speed); + if (mov.Moved())cur_state = E_LiveObjState::Walk; } \ No newline at end of file diff --git a/LiveObjSprite.h b/LiveObjSprite.h index 52eb81c..b390f62 100644 --- a/LiveObjSprite.h +++ b/LiveObjSprite.h @@ -21,15 +21,16 @@ enum class E_LiveObjType struct LiveObjSprite { - LiveObjSprite(E_LiveObjType must_enemy, int type, int move_frames, int attack_frames, int take_hit_frames, int ms_speed) : spr_states() + LiveObjSprite(E_LiveObjType must_enemy, int type, int move_frames, int attack_frames, int take_hit_frames, int idle_frames, int ms_speed) : spr_states() { if (must_enemy != E_LiveObjType::Enemy)error("must enemy. for character use another constructor"); lo_type = must_enemy; cur_dir = std::rand() % 2 ? E_Dir::LEFT : E_Dir::RIGHT; std::string path_0 = "../resources/enemies/" + std::to_string(type) + "/"; - spr_states.insert({E_LiveObjState::Idle, std::map{}}).first->second.insert({E_Dir::RIGHT, Sprite(path_0 + "move.png", move_frames, ms_speed)}); + spr_states.insert({E_LiveObjState::Walk, std::map{}}).first->second.insert({E_Dir::RIGHT, Sprite(path_0 + "move.png", move_frames, ms_speed)}); spr_states.insert({E_LiveObjState::Attack, std::map{}}).first->second.insert({E_Dir::RIGHT, Sprite(path_0 + "attack.png", attack_frames, ms_speed)}); spr_states.insert({E_LiveObjState::TakeHit, std::map{}}).first->second.insert({E_Dir::RIGHT, Sprite(path_0 + "take_hit.png", take_hit_frames)}); + spr_states.insert({E_LiveObjState::Idle, std::map{}}).first->second.insert({E_Dir::RIGHT, Sprite(path_0 + "idle.png", idle_frames)}); //SetCurSprite(); } @@ -75,8 +76,6 @@ struct LiveObjSprite bool SetState(E_LiveObjState new_state) { - if (lo_type == E_LiveObjType::Enemy && new_state == E_LiveObjState::Walk)new_state = E_LiveObjState::Idle; - if (cur_state != new_state) { cur_state = new_state; SpritePrepare(); diff --git a/SpriteManager.h b/SpriteManager.h index 406179c..4cd5f62 100644 --- a/SpriteManager.h +++ b/SpriteManager.h @@ -34,6 +34,12 @@ struct SpriteManager private: std::vector doors; + static constexpr int e_idle_frames(int type) + { + if (type == 0)return 8; + if (type == 1)return 4; + return 1; + } static constexpr int e_get_move_frames(int type) { if (type == 0)return 8; @@ -63,7 +69,8 @@ struct SpriteManager for (int _type = 0; _type < 2; _type++) enemy_spr.emplace_back(E_LiveObjType::Enemy, _type, e_get_move_frames(_type), e_get_attack_frames(_type), - e_get_take_hit_frames(_type), e_get_spr_speed(_type)); + e_get_take_hit_frames(_type), e_idle_frames(_type), + e_get_spr_speed(_type)); int sz = HP_SZ; Image hp_img {sz,sz,4}; diff --git a/resources/enemies/0/idle.png b/resources/enemies/0/idle.png new file mode 100644 index 0000000000000000000000000000000000000000..5547b4373c4b8e274268296b46128eb10e9d02f4 GIT binary patch literal 2241 zcmV;y2tN0TP)Px#1ZP1_K>z@;j|==^1poj532;bRa{vGi!vFvd!vV){sAK>D2v$i%K~#8N?UqZj zf`%<#}Q@I)sPM0lZD$kRgg0J4* z-flMV-M3%Ai5$NYui$udb7jiGXY=bXKRg=ys?CjGj$e)Ya=f|OM>+UR{`&Lx$K~_K zZ$JI}`hNb|my`SHIp>{cx~lew8 zQP(q2P1Rmt$f zbJOSXk2kZRp}ncOcv_9VhpMSHo{jC#vprjm!o8|7TzQA1FLU-{jpNZ5^v%te#N(fA zW}x8&jm$XlTkv&7*7Vq_u|?*p4Ph(t;ysz~XIl!1 zcP}^N&ji0#&GgiBFa8<$-d)jeFPuR|#y*S64Kj8OpS@@?KkN}wg7=ClUyuu}53ILF z59;T7PL=0m{8AGvx#s+mU$V5m$Q+@|QQlR&qxIK1t%3DhHLh}rkDPPmxW-c+$YM#2Ij5c-)T(lO?6`n)wwQP08A=h)N zJSXFORQb+`&lYl@1#_c&H+li_H&kS7qL@~_qb_h?gJ+RFlXX(H=+m3Ezx-cR#(S!zg=Q@zGj5S9lJ!*N|(ppkC$+ z<|@dq;(UFf{mKthWSqlVnd5jpp2f&nlLtOGa=z5^yo#(N+z&GEZ176IcV0jKx1YJj z5}doQ--7*2nDbcodw7{+l@D^!m^&VR`S_SiimjU|KY!0Uqg0N<9k=)ad&&D=PxAfU z9raGFwPej6UY~s4b3XluS%P(a{VL93{mg}G4flhQJe_NPhZ&RLw}j`sb6e;cU#D_h zgRkXI&fgzCH2plkBI^l_S9SK#bFAVW^|1RbUOql2d~S)JXs+AC9rXtYufrVpoWnu+ znEO4vHaaGi=O~q@aL+xCd%d*As9(k;t%j~*D{yMzvFX-oZqI@A6Rp@iti6CTUt{Sh zdY5N!y+nTQxaRUn2R&DDd)ebCb?0+;Ua!YmKaUe$Q!eSlH+VF?v##QyauikUUV1%K#V6W3x#6ra z&ddJ}#_}91GBzDY-GtUqeH^QpEv#{*KJSk2k@$4jf~NMdC9=S%Xs$ELUcV&$j=CU6 zJu4^n&dEJI%Ckk*Ut*76?p3~5^|sJE>nh$Ik|Qti(ViJWd&ec|Yl@Uj3`@1}p!#I5Qk{{+M$T>@*TiN2+bJII+`9bD7XX1Vp>3H@9=~O<+ z48N-G5~F7gS+hm!JM^_$wBp%YM*}o{+q`$j?6KxJcoIZ zm+=3~t>T2nm&lwgj!s_Q;=o^Cl%v2+R3GGQdVYGxkH!`n&vnk^Dt_+a zwC3CuPoX*1oJsm=ulDqeUpx+Lj8f&PKH!S?dlBJ%`uxV6Z3iHsFKPmJ=IKa*+?aY&e( zv4=f+7bML59xKkce2!*m54TVb?%@?S@)aCMnb|mb`Q*IA$NG$>*7>eE<7Zwzc#gV0 zi>vp{S$^f<9@SI#mygc$>W!Sdg6q|L=1jkGa4T2+IOXM=ctyvX|0l2T zEnc;+;Y`1Ba4T26eDJ!W+s(}@aOK{4nG5pf=H_NL_XT-#b8|DDx3B&I1&Pv4rhMAX P00000NkvXXu0mjf$#tiv literal 0 HcmV?d00001 diff --git a/resources/enemies/1/idle.png b/resources/enemies/1/idle.png new file mode 100644 index 0000000000000000000000000000000000000000..88b1109f6135d769bcf8121ece9dd4053e437ab5 GIT binary patch literal 1143 zcmV--1c>{IP)Px#1ZP1_K>z@;j|==^1poj532;bRa{vGi!vFvd!vV){sAK>D1Pn<;K~!i%?U=od z6hRb(F9D=8FmUmZu#iXy5e7M6kp(dj1Yr;Z#-KW=RZUIx|Lk&zH9pPs+xP0*?mjr_ z9=2_3^5ONHtp}gGb3U;M(uIuRc_q^9D4zKoN#8r+bIzfVLT2%7CH1f34kyiw;+Zwl z{0N>0c8fH$isqBlcNXpfsc#i`D5-xI&t8(ee|&i$_0h57>g8{`KT)5bYD$b3LF6$bhKO~<+j%q(XdVlQXxD;6D! zz|SAw;}iJYEX=FnGqYUs77no&x@!206N|*d-oAKzx}fxAk7tacKL4TM6ZqUL%&Xxu zGict*zG4wDK*7c6B`NIjoLST|nnS zuZwtI4WAv|m*^w&T6NZsGl& z@8~QT@r`(Z&Vn;tyM+ehi9gTuibzXHR$V#|Jm2x`f7Nw);or|V0QmPb4Q$a8f?K+P z_xZYo_j|tLy}s_D=DNJ#u=F7HsbM|v;4#a5fLSe8T!dLQc#!(U_e^;;^g`T0uI<{%6dmS8Xw6}d=k$>BT8#Q| z00i?m2+wT69A=hSap}I2YqwBJL7-Ikj9xU4nOAhOV{V>xo6s z3N7I|J=|S;JV%K|Fe+lN=k#!Q{ieqii-7-}HPCy2idwP~$yMTicf}%p*+GUU!tm}r zguhtX>z}uH4lcYq*K#FBT(L+IpZ>Wr(wn%u#(HD%&U0mZ_yy^<5%^j@Oc?+G002ov JPDHLkV1iT;Jud(N literal 0 HcmV?d00001