From d4f9be5bffacfde80802fcbb2c1bacde22cd0472 Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Fri, 4 Oct 2024 01:21:52 +0200 Subject: [PATCH] add sprites for most tiles --- assets/tiles/add_off.png | Bin 0 -> 176 bytes assets/tiles/add_on.png | Bin 0 -> 176 bytes assets/tiles/bag_off.png | Bin 0 -> 200 bytes assets/tiles/bag_on.png | Bin 0 -> 190 bytes assets/tiles/div_off.png | Bin 0 -> 194 bytes assets/tiles/div_on.png | Bin 0 -> 195 bytes assets/tiles/down.png | Bin 0 -> 198 bytes assets/tiles/eq_off.png | Bin 0 -> 158 bytes assets/tiles/eq_on.png | Bin 0 -> 158 bytes assets/tiles/flipper_off.png | Bin 0 -> 209 bytes assets/tiles/flipper_on.png | Bin 0 -> 200 bytes assets/tiles/gt_off.png | Bin 0 -> 198 bytes assets/tiles/gt_on.png | Bin 0 -> 198 bytes assets/tiles/input_off.png | Bin 0 -> 196 bytes assets/tiles/input_on.png | Bin 0 -> 196 bytes assets/tiles/left.png | Bin 0 -> 211 bytes assets/tiles/lt_off.png | Bin 0 -> 194 bytes assets/tiles/lt_on.png | Bin 0 -> 194 bytes assets/tiles/mul_off.png | Bin 0 -> 209 bytes assets/tiles/mul_on.png | Bin 0 -> 210 bytes assets/tiles/neq_off.png | Bin 0 -> 206 bytes assets/tiles/neq_on.png | Bin 0 -> 207 bytes assets/tiles/output_off.png | Bin 0 -> 188 bytes assets/tiles/output_on.png | Bin 0 -> 188 bytes assets/tiles/rem_off.png | Bin 0 -> 215 bytes assets/tiles/rem_on.png | Bin 0 -> 215 bytes assets/tiles/right.png | Bin 0 -> 220 bytes assets/tiles/sub_off.png | Bin 0 -> 177 bytes assets/tiles/sub_on.png | Bin 0 -> 178 bytes assets/tiles/trigger_off.png | Bin 0 -> 147 bytes assets/tiles/trigger_on.png | Bin 0 -> 148 bytes assets/tiles/up.png | Bin 0 -> 205 bytes assets/tiles/wire_cross_off.png | Bin 0 -> 189 bytes assets/tiles/wire_cross_on.png | Bin 0 -> 188 bytes assets/tiles/wire_horizontal_off.png | Bin 0 -> 126 bytes assets/tiles/wire_horizontal_on.png | Bin 0 -> 125 bytes assets/tiles/wire_vertical_off.png | Bin 0 -> 136 bytes assets/tiles/wire_vertical_on.png | Bin 0 -> 127 bytes src/main.rs | 26 +++++- src/marble_engine.rs | 27 ++++-- src/marble_engine/tile.rs | 118 +++++++++++++++------------ 41 files changed, 108 insertions(+), 63 deletions(-) create mode 100644 assets/tiles/add_off.png create mode 100644 assets/tiles/add_on.png create mode 100644 assets/tiles/bag_off.png create mode 100644 assets/tiles/bag_on.png create mode 100644 assets/tiles/div_off.png create mode 100644 assets/tiles/div_on.png create mode 100644 assets/tiles/down.png create mode 100644 assets/tiles/eq_off.png create mode 100644 assets/tiles/eq_on.png create mode 100644 assets/tiles/flipper_off.png create mode 100644 assets/tiles/flipper_on.png create mode 100644 assets/tiles/gt_off.png create mode 100644 assets/tiles/gt_on.png create mode 100644 assets/tiles/input_off.png create mode 100644 assets/tiles/input_on.png create mode 100644 assets/tiles/left.png create mode 100644 assets/tiles/lt_off.png create mode 100644 assets/tiles/lt_on.png create mode 100644 assets/tiles/mul_off.png create mode 100644 assets/tiles/mul_on.png create mode 100644 assets/tiles/neq_off.png create mode 100644 assets/tiles/neq_on.png create mode 100644 assets/tiles/output_off.png create mode 100644 assets/tiles/output_on.png create mode 100644 assets/tiles/rem_off.png create mode 100644 assets/tiles/rem_on.png create mode 100644 assets/tiles/right.png create mode 100644 assets/tiles/sub_off.png create mode 100644 assets/tiles/sub_on.png create mode 100644 assets/tiles/trigger_off.png create mode 100644 assets/tiles/trigger_on.png create mode 100644 assets/tiles/up.png create mode 100644 assets/tiles/wire_cross_off.png create mode 100644 assets/tiles/wire_cross_on.png create mode 100644 assets/tiles/wire_horizontal_off.png create mode 100644 assets/tiles/wire_horizontal_on.png create mode 100644 assets/tiles/wire_vertical_off.png create mode 100644 assets/tiles/wire_vertical_on.png diff --git a/assets/tiles/add_off.png b/assets/tiles/add_off.png new file mode 100644 index 0000000000000000000000000000000000000000..286a7e92ff7cb15087caef780cb79f66785744b5 GIT binary patch literal 176 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61SBU+%rFB|<(@8%AsQ2tb0W)o8buBqI&h%i zo8ab+ckX*@t33!@E*{7bcP*+-18g06CYvv^C?*KB2?;YZH^;d!8hR<7l8C-0tE~CaaMG+j ZjOHqaK0hp9T?0CS!PC{xWt~$(69C@QJyiez literal 0 HcmV?d00001 diff --git a/assets/tiles/bag_off.png b/assets/tiles/bag_off.png new file mode 100644 index 0000000000000000000000000000000000000000..c270af5fd5324039b11f9c60c6945b364be4d3ea GIT binary patch literal 200 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61SBU+%rFB|{hlt4AsQ2t|7;BJX%snd=)i%3 zAAiK7o#*YC$yaej*UO$E@EFHNM&Ht;L{Vnu!+-vnnb`91@c8TqQ8rd!Sk%+0!k=)R zp`3HpWQ7LSv#J8FC%i8HIoxvp?uFVdQ&MBb@0JUgLbN~PV literal 0 HcmV?d00001 diff --git a/assets/tiles/bag_on.png b/assets/tiles/bag_on.png new file mode 100644 index 0000000000000000000000000000000000000000..9630dabb9893ef0d21fda59dd46c8b1e6e5f6dfb GIT binary patch literal 190 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61SBU+%rFB|t)4E9AsQ2t|7;BJX%snd=)i%3 zAAiK7o#*YC$yaej*UO$E@EFHNM&GZETU>c~c=$Fyy70vS2p$IYo@rp{FjAXj(-6yi zM>WH*i9yU{x`T+Kw(Dnp;kxqVuF0?2W8NJTNk2H{@MHNmeutjQ&yo7Tti~2G=W(Gq mi^CMde>b$<{2nRGF))aK^Zd>rBys`hAO=rYKbLh*2~7Z1B15GB literal 0 HcmV?d00001 diff --git a/assets/tiles/div_off.png b/assets/tiles/div_off.png new file mode 100644 index 0000000000000000000000000000000000000000..272ecb429b219cbc3e26f2b4af4569493490c1cb GIT binary patch literal 194 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61SBU+%rFB|ot`d^AsQ2tb0W)o8buBqI&h%i zo8ab+ckX*@t33!@E*{7bc~->psq!XXLnZkC|>8UxYNM4;dX;0{|}P~UF<XB!?G?(JX#>JySzUU^cQ;c&P6T*S94mYf46dpcr0MVb--wyRp8lx>K#UFIVLm2 zWip%%cmdKI;Vst0EsC{`Tzg` literal 0 HcmV?d00001 diff --git a/assets/tiles/eq_off.png b/assets/tiles/eq_off.png new file mode 100644 index 0000000000000000000000000000000000000000..39f6cb1f5f6f3794636d2000e16253e069a74732 GIT binary patch literal 158 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61SBU+%rFB|sh%#5AsQ2tW9rtlHi{fLbl|{- z@8=TJMQ?u<)_WKz9m literal 0 HcmV?d00001 diff --git a/assets/tiles/eq_on.png b/assets/tiles/eq_on.png new file mode 100644 index 0000000000000000000000000000000000000000..178d693f808a959964caffbd95dffc3f2ac14d20 GIT binary patch literal 158 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61SBU+%rFB|sh%#5AsQ2tW9rtlHi{fLbl|{- z@8=TJMQ?u<)_WKz9`VLG`s)+ literal 0 HcmV?d00001 diff --git a/assets/tiles/flipper_off.png b/assets/tiles/flipper_off.png new file mode 100644 index 0000000000000000000000000000000000000000..71c7983379eb5546ddd30f0b0f87c665057229d6 GIT binary patch literal 209 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61SBU+%rFB|Gdx`!Lo_BP|JfMc(gV5F0b3p(9-o4%1)OVEJv=o-L1xFz zjH(AQJDAjV$SjldxvC*`;Y&hj?>fd{DPxHXrUEQ4qzWpfcC)3FKjt!5TeIq-#EB}2 z`ZuD^ej+pWxrFW2_{$*7taEPTDmSqf4X`uj9C5c{U|4@ET(m7@rvlK844$rjF6*2U FngG#YO{xF@ literal 0 HcmV?d00001 diff --git a/assets/tiles/flipper_on.png b/assets/tiles/flipper_on.png new file mode 100644 index 0000000000000000000000000000000000000000..7da4ad913cb853e7199c1af856fd2fabd47ac7ff GIT binary patch literal 200 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61SBU+%rFB|{hlt4AsQ2t|7;BJX%snd=)i%3 zAAiK7o#*YC$yaej*UO$E@EFHN#znszZYixHmktOp@vl8BlDT}fdR){R0T%g&? xFljQIFf;Svd)DoWGJjTmzh~LAp|OI2p>Vp(gVR&z+5jEK;OXk;vd$@?2>?x7N+|#U literal 0 HcmV?d00001 diff --git a/assets/tiles/gt_on.png b/assets/tiles/gt_on.png new file mode 100644 index 0000000000000000000000000000000000000000..ae01917df51ebc074b03a3ef4afd8818861392ee GIT binary patch literal 198 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61SBU+%rFB|y`Csj=iSX_)4;k~W0vuRT<7Zz?wY&Un!F0FIHw13 wC|G717#bM7*;_1e=-~RQ_j3<(NO!O^7;w18@Dwi906L7p)78&qol`;+0HD7`#Q*>R literal 0 HcmV?d00001 diff --git a/assets/tiles/input_off.png b/assets/tiles/input_off.png new file mode 100644 index 0000000000000000000000000000000000000000..c2606d76186b03372ec920731f1b3ae7c6372621 GIT binary patch literal 196 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61SBU+%rFB|-JULvAsQ2t|7;BJX%snd=)i%3 zAAiK7o#*YC$yaej*UO$E@EFHN#{F44~uoc+nT;l%vcl u=Q<7LRz3XiQ=jFlgOEJ)5q=d*PKE>)t5l5-5+OimF?hQAxvX up?<7lMcd)$`vtBrd)Dv>*iU$(!XW<5=AyV?*GizX7(8A5T-G@yGywn&?m+1P literal 0 HcmV?d00001 diff --git a/assets/tiles/left.png b/assets/tiles/left.png new file mode 100644 index 0000000000000000000000000000000000000000..5bb4a87418bbf829a4a40a108547f040242bf0ac GIT binary patch literal 211 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61SBU+%rFB|vpiiKLo_B%on*_^>>$u;pBl-r zvp3huQ_!hzb&_J)wm7E`Y>p8+K|N`{1^?r=f3UrGZqLbORcfwW29H;9U*(zFUVSgF z>1o6KeerS*#v!``9L%n6?iLHVqPzCV3Wcmw+uyCXH{Nj}IMP`_^r`0-(<9l&Cxk8u z?ONKq@o&9mhV}f18P?MpOn86zKaT!9E#X+G+&YtQk2iCE{Ka75;;Jj~#HteLN(N6? KKbLh*2~7Y{A5!uF literal 0 HcmV?d00001 diff --git a/assets/tiles/lt_off.png b/assets/tiles/lt_off.png new file mode 100644 index 0000000000000000000000000000000000000000..6b8bb6b6303d1a5b322574139f78541bd2b8547e GIT binary patch literal 194 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61SBU+%rFB|ot`d^AsQ2tW9rtlHi{fLbl|{- z@8=TJMQ?u<)_WKz958_07?jW`$L+W@;!i{T z%EN0c=16_ecD?A(6U32lNY&Ay87Pzz%)dnU(9=v2>5hwSM?HURefWiWmYIcuWu}3l rfx(--#S(`OuCIDO_b`Wa2RnoQKbJF~?G`lx9mU}3>gTe~DWM4f8GlJv literal 0 HcmV?d00001 diff --git a/assets/tiles/mul_off.png b/assets/tiles/mul_off.png new file mode 100644 index 0000000000000000000000000000000000000000..8fb3dc71b6f1445af78bf48553f5da2202740fc9 GIT binary patch literal 209 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61SBU+%rFB|Gdx`!Lo_BP=R}tGG>RNJbl^b2 zH^I#t@7(v+R(lY*Ts)8=@Ys<92OI)SnVAnS6&3Dz`p?L~z`)Uhhli)LH57<0MWkXDRNJbl^b2 zH^I#t@7(v+R(lY*Ts)8=@Ys<92OI)SnVAnS6&3Dz`p?L~z`)Uhhlhv#l9;SVbjwN+ zJ-)zWEtkw4p%5Q8t@Zk{^CE`c9ClJMdnD!{LTQPu7aI z15CC%tZY^(o|+*cA@S{=WzQP+>96*d^BEf$7)+YAhjD?HGk4=Z`*NTI89ZJ6T-G@y GGywq7yibY% literal 0 HcmV?d00001 diff --git a/assets/tiles/neq_on.png b/assets/tiles/neq_on.png new file mode 100644 index 0000000000000000000000000000000000000000..af8cba09c712c828c58d4d1f9aea3efdc923248b GIT binary patch literal 207 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61SBU+%rFB|(>z@qLo_BP$JDK9Z4^0h=)i#u z-_Iqci{Ab!toJZbJYoTR;IShI4m8AnZQSC@!^87hJ*FsZV`@S|LQ}zq)orgEvkt7E z)!2A2F-)dQe!|laFIqAVr!$8gXv);oJMO?BCjDVW+JX1%CXQJ@822YDFeEY?u39L? z#Qo*Ls}C#MG?J4N5^hvKH`pW>v+Daj%QFWK9Pr5c#K!Q$+L`-c?bUvu3mH6J{an^L HB{Ts5Iu=yF literal 0 HcmV?d00001 diff --git a/assets/tiles/output_off.png b/assets/tiles/output_off.png new file mode 100644 index 0000000000000000000000000000000000000000..43fb910af708a9b614c18d3e2f21491ee897cde7 GIT binary patch literal 188 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61SBU+%rFB|&7LlfAsQ2t|7;BJX%snd=)i%3 zAAiK7o#*YC$yaej*UO$E@EFHN#{F44K5?<(O+bp7R07D<6A7sa2@zB38tm#Cj#wJLYs|8!`N!>O jJK@N2hL0wmeoPFUYs~&GoK-vr=o|)5S3j3^P6y$s~&#% msn7D&K}eqY$Ym8vPKGjWtHjNvQ=S2x!{F)a=d#Wzp$P!pLP7Na literal 0 HcmV?d00001 diff --git a/assets/tiles/rem_off.png b/assets/tiles/rem_off.png new file mode 100644 index 0000000000000000000000000000000000000000..23093cd24aefa77b45a317a07dd29f867d93f0c5 GIT binary patch literal 215 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61SBU+%rFB|^E_P~Lo_BP=R}tGG>RNJbl^b2 zH^I#t@7(v+R(lY*Ts)8=@Ys<92OI)SnVAnS6&3Dz`p+r1VTGU0nx+$H-{(7raS6`9 zf4?mAD9hdsCbbkxlcgt?9bgoe%Un~o>4A~lbnd_$g%wh}*=$yA%s8-q7UNC+h|uIN zgNK2=Q@h`(GzA{y@T_5(%*M>z9OuG#rEPQn3`LngtIpdcx!rKEVPFX5_CBK(r?dp< OQU*^~KbLh*2~7a?L{K9D literal 0 HcmV?d00001 diff --git a/assets/tiles/rem_on.png b/assets/tiles/rem_on.png new file mode 100644 index 0000000000000000000000000000000000000000..dde5d9658500ba59fb88591b5849585f2235481c GIT binary patch literal 215 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61SBU+%rFB|^E_P~Lo_BP=R}tGG>RNJbl^b2 zH^I#t@7(v+R(lY*Ts)8=@Ys<92OI)SnVAnS6&3Dz`p+r1VTIjG2!WjDN3`M*nKLNY`k@{>2yVmp3BC@1zB&TKCC#lh;e@wTZ(-`Xs_m> z7I)Jyy^N(PMQD! literal 0 HcmV?d00001 diff --git a/assets/tiles/right.png b/assets/tiles/right.png new file mode 100644 index 0000000000000000000000000000000000000000..f3dc6b918c29ddd2ad80811ee5dd40f970784c07 GIT binary patch literal 220 zcmV<203-j2P);ODJkNk#%13=J@ z!l|Z8E2;64+$J^rwxvi7`=l$KH(VN>MI|ANG8KQm^w-hqH W5>q9@RD?wU0000st literal 0 HcmV?d00001 diff --git a/assets/tiles/sub_off.png b/assets/tiles/sub_off.png new file mode 100644 index 0000000000000000000000000000000000000000..af3816710da65c84769756857e65c5aec20ea6c0 GIT binary patch literal 177 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61SBU+%rFB|6`n4RAsQ2tb0W)o8buBqI&h%i zo8ab+ckX*@t33!@E*{7bc~Ii%I(_5AzxzyxL&bHfCIHX&hV=H@sTMnf;fQ!^waB+l6-sjXn2^aIG0@btaM ZpxdN;QapU|TA&*kJYD@<);T3K0RUP3I{g3u literal 0 HcmV?d00001 diff --git a/assets/tiles/sub_on.png b/assets/tiles/sub_on.png new file mode 100644 index 0000000000000000000000000000000000000000..a2181e6bb73906ff60a9845e9f196302fc3a784e GIT binary patch literal 178 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61SBU+%rFB|m7Xq+AsQ2tb0W)o8buBqI&h%i zo8ab+ckX*@t33!@E*{7bcZQ{_TNpfD{an^LB{Ts5x?C|n literal 0 HcmV?d00001 diff --git a/assets/tiles/trigger_on.png b/assets/tiles/trigger_on.png new file mode 100644 index 0000000000000000000000000000000000000000..f7308008edea2b3c9182f1b4b6bfa518fc308c45 GIT binary patch literal 148 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`(Vi}jAr`&K2@;oTG8i5j^p(nZ=U6hC$=fw_SwXEgh8xk u(vCU47g%r1={>D|rPs}=Ey3I4FwpFbZHitsw+o$tOa@O^KbLh*2~7YqyfX9v literal 0 HcmV?d00001 diff --git a/assets/tiles/up.png b/assets/tiles/up.png new file mode 100644 index 0000000000000000000000000000000000000000..956e2704ca83e6212b46039525576bb4d084f0dc GIT binary patch literal 205 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61SBU+%rFB|Q#@T9Lo_BP*X;Q!$*kGX+SvH; z`cC^CH=Wykc?TDtJAI3NQBP-MBV+JZ?&a;=%h@-b*tR(OMo)x`Qvpn5DqEAiH7qscrqzmxa1=9gO)`tBTxccp0;1+JtQ8u*Vytn67Sk{+rKc*`~LT z+q4r}RP6q6ax(2%Q@gt^Ys&zAdp$e!s&cMFU%%9U?igR_v|sW#=}9$_i_4OJcT&Z8+_=reTtU3~LAj Y1ADM!PA!j!JJ3J|Pgg&ebxsLQ05Ulzs{jB1 literal 0 HcmV?d00001 diff --git a/assets/tiles/wire_horizontal_on.png b/assets/tiles/wire_horizontal_on.png new file mode 100644 index 0000000000000000000000000000000000000000..4c5f82da63abf356bf07e518da86d523e25b2439 GIT binary patch literal 125 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`E}kxqAr`&K2@FVdQ&MBb@0JX9)0RR91 literal 0 HcmV?d00001 diff --git a/assets/tiles/wire_vertical_on.png b/assets/tiles/wire_vertical_on.png new file mode 100644 index 0000000000000000000000000000000000000000..37d508cf8002d25b66faef4fb4be896ec06094b9 GIT binary patch literal 127 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`Zk{fVAr`&KDF+yjeEajG|4jYU zLkBy2Wy20`*s$UMjU(Y}qgofrt!7-Zqk(feAi}EzN(`dOioxL = HashMap::new(); + for d in read_dir("assets/tiles").unwrap().flatten() { + let name = d.file_name(); + if d.path().is_file() { + let name = name.to_string_lossy(); + let texture = rl + .load_texture(&thread, &format!("assets/tiles/{name}")) + .unwrap(); + textures.insert(name.to_string(), texture); + } + } + while !rl.window_should_close() { if rl.is_key_pressed(KeyboardKey::KEY_SPACE) { machine.step(); @@ -27,7 +45,7 @@ fn main() { let mut d = rl.begin_drawing(&thread); d.clear_background(Color::new(64, 64, 64, 255)); - machine.draw(&mut d, pos_offset); + machine.draw(&mut d, &textures, pos_offset); d.draw_fps(2, 2); } diff --git a/src/marble_engine.rs b/src/marble_engine.rs index c8b043d..519477d 100644 --- a/src/marble_engine.rs +++ b/src/marble_engine.rs @@ -1,4 +1,6 @@ -use raylib::{drawing::RaylibDrawHandle, math::Vector2}; +use std::collections::HashMap; + +use raylib::{drawing::RaylibDrawHandle, math::Vector2, texture::Texture2D}; mod board; mod tile; @@ -48,14 +50,19 @@ impl Machine { } } - pub fn draw(&self, d: &mut RaylibDrawHandle, offset: Vector2) { + pub fn draw( + &self, + d: &mut RaylibDrawHandle, + textures: &HashMap, + offset: Vector2, + ) { let tile_size = 32; for x in 0..self.board.width() { for y in 0..self.board.height() { if let Some(tile) = self.board.get((x, y).into()) { let px = x as i32 * tile_size + offset.x as i32 + tile_size / 2; let py = y as i32 * tile_size + offset.y as i32 + tile_size / 2; - tile.draw(d, px, py, tile_size); + tile.draw(d, textures, px, py, tile_size); } } } @@ -66,10 +73,8 @@ impl Machine { // reset wires for y in 0..self.board.height() { for x in 0..self.board.width() { - match self.board.get_mut((x, y).into()) { - Tile::Powerable(_, state) => *state = false, - Tile::Trigger(state) => *state = false, - _ => (), + if let Tile::Powerable(_, state) = self.board.get_mut((x, y).into()) { + *state = false; } } } @@ -120,7 +125,7 @@ impl Machine { *other_dir = dir; } } - Tile::Trigger(state) => { + Tile::Powerable(PTile::Trigger, state) => { *state = true; triggers.push(next_pos); let far_pos = dir.step(next_pos); @@ -191,6 +196,9 @@ impl Machine { let tile = self.board.get_mut(pos); let front_pos = dir.step(pos); if let Tile::Powerable(tile, state) = tile { + if let PTile::Trigger = tile { + return; + } if *state { return; } @@ -285,6 +293,7 @@ impl Machine { self.propagate_power(dir, dir.step(pos)); } } + PTile::Trigger => (), } } } @@ -316,7 +325,7 @@ pub fn parse(source: &str) -> Board { value: 0, dir: Direction::Down, }, - '*' => Tile::Trigger(false), + '*' => Tile::Powerable(PTile::Trigger, false), '-' => Tile::Powerable(PTile::Wire(WireType::Horizontal), false), '|' => Tile::Powerable(PTile::Wire(WireType::Vertical), false), '+' => Tile::Powerable(PTile::Wire(WireType::Cross), false), diff --git a/src/marble_engine/tile.rs b/src/marble_engine/tile.rs index 7f24a40..439ece1 100644 --- a/src/marble_engine/tile.rs +++ b/src/marble_engine/tile.rs @@ -1,8 +1,11 @@ +use std::collections::HashMap; + use raylib::{ color::Color, drawing::{RaylibDraw, RaylibDrawHandle}, ffi::Rectangle, math::Vector2, + texture::Texture2D, }; use super::board::Pos; @@ -19,7 +22,6 @@ pub enum Tile { value: MarbleValue, dir: Direction, }, - Trigger(bool), Digit(u8), Mirror(MirrorType), Arrow(Direction), @@ -28,6 +30,7 @@ pub enum Tile { #[derive(Debug, Clone, Copy)] pub enum PTile { + Trigger, Wire(WireType), Gate(GateType), Math(MathOp), @@ -88,16 +91,73 @@ impl Tile { } } - pub fn draw(&self, d: &mut RaylibDrawHandle, x: i32, y: i32, size: i32) { - let up = y - size / 2 + 1; - let down = y + size / 2 - 1; - let left = x - size / 2 + 1; - let right = x + size / 2 - 1; + pub fn draw( + &self, + d: &mut RaylibDrawHandle, + textures: &HashMap, + x: i32, + y: i32, + size: i32, + ) { + let tex_name = match self { + Tile::Blank => "", + Tile::Block => "", + Tile::Comment(_) => "", + Tile::Marble { value, dir } => "todo!()", + Tile::Digit(_) => "", + Tile::Mirror(_) => "", + Tile::Arrow(dir) => match dir { + Direction::Up => "up", + Direction::Down => "down", + Direction::Left => "left", + Direction::Right => "right", + }, + Tile::Powerable(tile, state) => { + let t = match tile { + PTile::Trigger => "trigger", + PTile::Wire(wire) => match wire { + WireType::Vertical => "wire_vertical", + WireType::Horizontal => "wire_horizontal", + WireType::Cross => "wire_cross", + }, + PTile::Gate(gate) => match gate { + GateType::LessThan => "lt", + GateType::GreaterThan => "gt", + GateType::Equal => "eq", + GateType::NotEqual => "neq", + }, + PTile::Math(math_op) => match math_op { + MathOp::Add => "add", + MathOp::Sub => "sub", + MathOp::Mul => "mul", + MathOp::Div => "div", + MathOp::Rem => "rem", + }, + PTile::Bag => "bag", + PTile::Flipper => "flipper", + PTile::Input => "input", + PTile::Output => "output", + }; + &format!("{t}_{}", if *state { "on" } else { "off" }) + } + }; + let tex_name = format!("{tex_name}.png"); + if let Some(texture) = textures.get(&tex_name) { + d.draw_texture_ex( + texture, + Vector2::new((x - size / 2) as f32, (y - size / 2) as f32), + 0.0, + 2.0, + Color::WHITE, + ); + return; + } + match self { Tile::Blank => (), Tile::Block => d.draw_rectangle(x - size / 2, y - size / 2, size, size, Color::DIMGRAY), Tile::Comment(c) => { - d.draw_rectangle(x-size/2, y-size/2, size, size, Color::DIMGRAY); + d.draw_rectangle(x - size / 2, y - size / 2, size, size, Color::DIMGRAY); d.draw_text(&format!("{}", *c as char), x - 10, y - 10, 20, Color::WHITE); } Tile::Marble { value, dir } => { @@ -110,10 +170,6 @@ impl Tile { Color::MAGENTA, ); } - Tile::Trigger(state) => { - let color = if *state { Color::RED } else { Color::LIGHTGRAY }; - d.draw_rectangle(x - size / 4, y - size / 4, size / 2, size / 2, color) - } Tile::Digit(n) => { d.draw_text(&String::from(*n as char), x - 10, y - 10, 20, Color::ORANGE) } @@ -132,45 +188,7 @@ impl Tile { }; d.draw_rectangle_pro(rec, Vector2::new(width, height) * 0.5, rot, Color::CYAN); } - Tile::Arrow(dir) => { - let up = Vector2::from((x as f32, up as f32)); - let down = Vector2::from((x as f32, down as f32)); - let left = Vector2::from((left as f32, y as f32)); - let right = Vector2::from((right as f32, y as f32)); - let (v1, v2, v3) = match dir { - Direction::Up => (up, left, right), - Direction::Down => (down, right, left), - Direction::Left => (left, down, up), - Direction::Right => (right, up, down), - }; - d.draw_triangle(v1, v2, v3, Color::CYAN); - } - Tile::Powerable(tile, state) => { - let color = if *state { Color::RED } else { Color::LIGHTGRAY }; - match tile { - PTile::Bag => { - d.draw_circle(x, y, size as f32 * 0.4, color); - d.draw_circle(x, y, size as f32 * 0.2, Color::GRAY); - } - PTile::Wire(wire) => { - let vertical = !matches!(wire, WireType::Horizontal); - let horizontal = !matches!(wire, WireType::Vertical); - if vertical { - d.draw_rectangle(x - size / 8, y - size / 2, size / 4, size, color) - } - if horizontal { - d.draw_rectangle(x - size / 2, y - size / 8, size, size / 4, color) - } - } - // PTile::Gate(_) => todo!(), - // PTile::Math(_) => todo!(), - // PTile::Print => todo!(), - // PTile::Input => todo!(), - // PTile::Flip => todo!(), - _ => d.draw_rectangle(x - size / 2, y - size / 2, size, size, color), - } - } - + _ => d.draw_rectangle(x - size / 2, y - size / 2, size, size, Color::YELLOW), } } }