| 1 | use 5.38.0; | ||
| 2 | use experimental 'class'; | ||
| 3 | package Game::Snake::Head 0.11; | ||
| 4 | class Game::Snake::Head { | ||
| 5 | our $VERSION = 0.11; | ||
| 6 | use Raylib::FFI; | ||
| 7 | use Game::Snake::Sprite; | ||
| 8 | |||
| 9 | field $x : param; | ||
| 10 | field $y : param; | ||
| 11 | field $height : param; | ||
| 12 | field $width : param; | ||
| 13 | field $direction : param; | ||
| 14 | field $last : param = 0; | ||
| 15 | |||
| 16 | field $sprite = Game::Snake::Sprite->new( | ||
| 17 | image => 'resources/snake-graphics.png', | ||
| 18 | x => 0, | ||
| 19 | y => 0, | ||
| 20 | width => 64, | ||
| 21 | height => 64 | ||
| 22 | ); | ||
| 23 | |||
| 24 | field $bend = 0; | ||
| 25 | |||
| 26 | method bend (@b) { | ||
| 27 | $bend = $b[0] if @b; | ||
| 28 | $bend; | ||
| 29 | } | ||
| 30 | |||
| 31 | method last (@l) { | ||
| 32 | $last = $l[0] if @l; | ||
| 33 | $last; | ||
| 34 | } | ||
| 35 | |||
| 36 | |||
| 37 | method x (@xx) { | ||
| 38 | $x = $xx[0] if @xx; | ||
| 39 | $x; | ||
| 40 | } | ||
| 41 | |||
| 42 | method y (@yy) { | ||
| 43 | $y = $yy[0] if @yy; | ||
| 44 | $y; | ||
| 45 | } | ||
| 46 | |||
| 47 | method width (@w) { | ||
| 48 | $width = $w[0] if @w; | ||
| 49 | $width; | ||
| 50 | } | ||
| 51 | |||
| 52 | method height (@h) { | ||
| 53 | $height = $h[0] if @h; | ||
| 54 | $height; | ||
| 55 | } | ||
| 56 | |||
| 57 | method direction (@dir) { | ||
| 58 | $direction = $dir[0] if @dir; | ||
| 59 | $direction; | ||
| 60 | } | ||
| 61 | |||
| 62 | method sprite { | ||
| 63 | $sprite; | ||
| 64 | } | ||
| 65 | |||
| 66 | method draw () { | ||
| 67 | if ($direction eq 'left') { | ||
| 68 | $sprite->y(64); | ||
| 69 | $sprite->x(192); | ||
| 70 | } elsif ($direction eq 'right') { | ||
| 71 | $sprite->y(0); | ||
| 72 | $sprite->x(256); | ||
| 73 | } elsif ($direction eq 'up') { | ||
| 74 | $sprite->y(0); | ||
| 75 | $sprite->x(192); | ||
| 76 | } else { | ||
| 77 | $sprite->y(64); | ||
| 78 | $sprite->x(256); | ||
| 79 | } | ||
| 80 | $sprite->draw($x, $y, $width, $height); | ||
| 81 | } | ||
| 82 | } | ||
| 83 | |||
| 84 | 1; | ||
| 85 | |||
| 86 | =pod | ||
| 87 | |||
| 88 | =cut |