Skip to content

Commit

Permalink
initial snake game plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Whitacre committed Feb 25, 2024
0 parents commit 6d6fa63
Show file tree
Hide file tree
Showing 17 changed files with 501 additions and 0 deletions.
97 changes: 97 additions & 0 deletions .bin/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#!/usr/bin/env bash

SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
source "$SCRIPT_DIR/log.sh"
source "$SCRIPT_DIR/watch_fs.sh"

log "lgrey" "Loaded scripts from the directory $SCRIPT_DIR"

GIT_DIR=$(git rev-parse --show-toplevel)
SRC_DIR="$GIT_DIR/src"
INFO_TOML="$GIT_DIR/info.toml"
PLUGINS_DIR=${PLUGINS_DIR:-$HOME/OpenplanetNext/Plugins}
ZIP=${ZIP:-"$PROGRAMFILES/7-Zip/7z.exe"}

PLUGIN_PRETTY_NAME="$(cat $INFO_TOML | dos2unix | grep '^name' | cut -f 2 -d '=' | tr -d '\"\r' | sed 's/^[ ]*//')"
PLUGIN_VERSION="$(cat $INFO_TOML | dos2unix | grep '^version' | cut -f 2 -d '=' | tr -d '\"\r' | sed 's/^[ ]*//')"
PLUGIN_NAME=$(echo "$PLUGIN_PRETTY_NAME" | tr -d '(),:;'\''"' | tr 'A-Z ' 'a-z-')
PLUGIN_BUILD_DIR="$GIT_DIR/dist"
PLUGIN_BUILD_NAME="$PLUGIN_BUILD_DIR/$PLUGIN_NAME-$PLUGIN_VERSION.op"
PLUGIN_DIRTY_FLAG="$PLUGIN_BUILD_DIR/dirty"

WATCH=false
CI=false
POSITIONAL_ARGS=()

while [[ $# -gt 0 ]]; do
case $1 in
-w|--watch)
WATCH=true
shift
;;
--ci)
CI=true
shift
;;
-*|--*)
log "red" "Unknown option $1. Exiting."
exit 1
;;
*)
POSITIONAL_ARGS+=("$1")
shift
;;
esac
done

set -- "${POSITIONAL_ARGS[@]}"

function publish() {
if ! $CI; then
local busy=true
log "lgrey" "Waiting to publish when not busy.. Stop/Unload the plugin."
while $busy; do
cp $PLUGIN_BUILD_NAME $PLUGINS_DIR 2> /dev/null && busy=false
sleep 1
done
log "green" "Copied build to plugins directory: $PLUGINS_DIR"
fi
}

function publish_repeat() {
while [[ true ]]; do
publish
watch_fs $PLUGIN_DIRTY_FLAG
done
}

function build() {
log "white" "Running build script for plugin: $PLUGIN_PRETTY_NAME v$PLUGIN_VERSION"

if ! test -d $PLUGIN_BUILD_DIR; then
mkdir $PLUGIN_BUILD_DIR
fi

if test -f $PLUGIN_BUILD_NAME; then
rm -f $PLUGIN_BUILD_NAME
fi

"$ZIP" a -mx1 -tzip $PLUGIN_BUILD_NAME $INFO_TOML $SRC_DIR
log "lgreen" "Build complete: $PLUGIN_BUILD_NAME"
echo "$(date)" > $PLUGIN_DIRTY_FLAG
}

build
if $WATCH; then
publish_repeat &
while [[ true ]]; do
watch_fs $SRC_DIR
build
done
else
publish &
fi

if ps -p $! > /dev/null; then
wait $!
fi
68 changes: 68 additions & 0 deletions .bin/log.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/usr/bin/env bash

function log() {
if [[ "$#" -eq 0 || "$#" -gt 2 ]]; then return
fi

local color=$1
local text=$2

local esc="\e["
local reset="${esc}39m"

local black="${esc}30m"
local red="${esc}31m"
local green="${esc}32m"
local yellow="${esc}33m"
local blue="${esc}34m"
local magenta="${esc}35m"
local cyan="${esc}36m"
local lightgrey="${esc}37m"
local darkgrey="${esc}90m"
local lightred="${esc}91m"
local lightgreen="${esc}92m"
local lightyellow="${esc}93m"
local lightblue="${esc}94m"
local lightmagenta="${esc}95m"
local lightcyan="${esc}96m"
local white="${esc}97m"

case ${color} in
"black") color=${black}
;;
"red") color=${red}
;;
"green") color=${green}
;;
"yellow") color=${yellow}
;;
"blue") color=${blue}
;;
"magenta") color=${magenta}
;;
"cyan") color=${cyan}
;;
"lgrey") color=${lightgrey}
;;
"dgrey") color=${darkgrey}
;;
"lred") color=${lightred}
;;
"lgreen") color=${lightgreen}
;;
"lyellow") color=${lightyellow}
;;
"lblue") color=${lightblue}
;;
"lmagenta") color=${lightmagenta}
;;
"lcyan") color=${lightcyan}
;;
"white") color=${white}
;;
*) echo -ne "${red}Unknown text color:${reset}"
;;
esac

echo -e "${color}${text}${reset}"
}
22 changes: 22 additions & 0 deletions .bin/watch_fs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env bash

function watch_fs() {
if [[ "$#" -eq 0 || "$#" -gt 1 ]]; then return
fi

local dir=$1
local chsum1=""

log "white" "Watching $dir for changes..."

while [[ true ]]; do
chsum2="$(find "$dir" -type f -exec md5sum {} \;)"
if [[ $chsum1 != $chsum2 ]] ; then
if [ -n "$chsum1" ]; then
return
fi
chsum1=$chsum2
fi
sleep 1
done
}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.zip
*.op
dist/
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Dan Whitacre

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
6 changes: 6 additions & 0 deletions info.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[meta]
name = "Snake"
author = "DanOnTheMoon"
category = "Game"
version = "1.0.0"
siteid = 510
11 changes: 11 additions & 0 deletions src/Apple.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Apple {
vec2 position;

Apple() {
this.Respawn();
}

void Respawn() {
position = vec2(Math::Rand(0, (Draw::GetWidth() - 1) / g_gridSize), Math::Rand(0, (Draw::GetHeight() - 1)) / g_gridSize);
}
}
72 changes: 72 additions & 0 deletions src/GameController.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
class GameController {
bool running;
Snake@ snake;
Apple@ apple;

GameController() {
running = false;
@snake = @Snake();
@apple = @Apple();
}

void StartGame() {
if (running) return;
LogTrace("Game started");
running = true;
snake = Snake();
apple.Respawn();
}

void StopGame() {
if (!running) return;
LogTrace("Game stopped");
running = false;
if (S_Snake_LastScore > S_Snake_HighScore) {
S_Snake_HighScore = S_Snake_LastScore;
NotifySuccess("New high score! " + Text::Format("%d", S_Snake_HighScore));
}
}

void Update() {
if (!running) return;

LogTrace("Snake position: " + Text::Format("%f", snake.segments[0].position.x) + "," + Text::Format("%f", snake.segments[0].position.y));
LogTrace("Apple position: " + Text::Format("%f", apple.position.x) + "," + Text::Format("%f", apple.position.y));

snake.Move();

if (snake.segments[0].position == apple.position) {
snake.Grow();
apple.Respawn();
LogTrace("Got apple! Score: " + Text::Format("%d", S_Snake_LastScore));
}

if (!snake.alive) {
NotifyInfo("You died! Score: " + Text::Format("%d", S_Snake_LastScore));
StopGame();
}
}

void Draw() {
if (S_Display_HideWithIFace && !UI::IsGameUIVisible()) return;
if (S_Display_HideWithOverlay && !UI::IsOverlayShown()) return;
if (!S_Display_Visible) return;

if (!running) return;

DrawApple(apple);
DrawSnake(snake);
}

void HandleInput(VirtualKey key) {
if (!running) {
if (key == S_Snake_StartGameKey) StartGame();
return;
}

if (key == S_Snake_LeftKey) snake.ChangeDirection(vec2(-1, 0));
else if (key == S_Snake_RightKey) snake.ChangeDirection(vec2(1, 0));
else if (key == S_Snake_UpKey) snake.ChangeDirection(vec2(0, -1));
else if (key == S_Snake_DownKey) snake.ChangeDirection(vec2(0, 1));
}
}
18 changes: 18 additions & 0 deletions src/Interface.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
void DrawSnake(Snake@ snake) {
nvg::FillColor(vec4(0, 255, 0, 255));

for (uint i = 0; i < snake.segments.Length; i++) {
auto segment = snake.segments[i];
nvg::BeginPath();
nvg::Rect(segment.position.x * g_gridSize, segment.position.y * g_gridSize, g_gridSize, g_gridSize);
nvg::Fill();
}
}

void DrawApple(Apple@ apple) {
nvg::FillColor(vec4(255, 0, 0, 255));

nvg::BeginPath();
nvg::Rect(apple.position.x * g_gridSize, apple.position.y * g_gridSize, g_gridSize, g_gridSize);
nvg::Fill();
}
17 changes: 17 additions & 0 deletions src/Log.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
void LogTrace(const string &in msg) {
if (S_Advanced_DevLog) {
trace(msg);
}
}

void LogInfo(const string &in msg) {
trace(msg);
}

void LogWarning(const string &in msg) {
warn(msg);
}

void LogError(const string &in msg) {
error(msg);
}
Loading

0 comments on commit 6d6fa63

Please sign in to comment.