Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // new module header
- //header================================================
- #define FAR_4_SPRITE 20
- #define CLEAR_SCREEN 25
- #define P_X playerChar.x
- enum PlayerFaceDirection
- {
- eFaceUp = 0,
- eFaceRight,
- eFaceDown,
- eFaceLeft,
- };
- enum MazeType{
- eMazeWall,
- eMazeFree,
- eMazeChest,
- };
- String map[10];
- struct PlayerController
- {
- int x;
- int y;
- PlayerFaceDirection faceDir;
- int rotation_x;
- int rotation_y;
- import void UpdateRotation();
- import void Rotate(int direction);
- import void Move(int distance);
- };
- import function renderVisionCone();
- //==================================end header
- DynamicSprite* sprite;
- int dungeonCounter = 25;
- PlayerController playerChar;
- function clearScreen() //starts a fresh canvas for drawing the walls on
- {
- DynamicSprite.Create(620, 400);
- DrawingSurface *surface = Room.GetDrawingSurfaceForBackground();
- surface.DrawImage(0, 0, CLEAR_SCREEN);
- surface.Release();
- }
- int GetCell(int x, int y) { //returns whether a cell is a wall or an open path
- map[0]="###### ###";
- map[1]="###### ###";
- map[2]="###### ###";
- map[3]=" ";
- map[4]="###### ###";
- map[5]="###### ###";
- map[6]="###### ###";
- map[7]="###### ###";
- map[8]="###### ###";
- map[9]="###### ###";
- // 0123456789
- // everything outside the maze is wall
- if (x < 0 || x >= map[0].Length) return eMazeWall;
- if (y < 0 || y >= 9) return eMazeWall;
- char c = map[y].Chars[x];
- if (c == '#') return eMazeWall;
- if (c == ' ') return eMazeFree;
- if (c == 'X') return eMazeChest;
- return eMazeWall;
- }
- int wallSprites[15];
- int[] rOffset(int x, int y, Direction dir) {
- int r[] = new int[2];
- if (dir == eFaceUp) { r[0] = x; r[1] = y; }
- if (dir == eFaceDown) { r[0] = -x; r[1] = -y; }
- if (dir == eFaceLeft) { r[0] = y; r[1] = -x; }
- if (dir == eFaceRight) { r[0] = -y; r[1] = x; }
- return r;
- }
- function renderVisionCone() //draws the walls
- {
- clearScreen();
- DrawingSurface * dungeonWalls = Room.GetDrawingSurfaceForBackground();
- // iterate over cone
- for (int y = -2; y <= 0; y++) {
- for (int x = -2; x <= 2; x++) {
- // rotate offset to find correct cell
- int ro[] = rOffset(x, y, playerChar.faceDir);
- MazeType mt = GetCell(playerChar.x + ro[0], playerChar.y + ro[1]);
- if (mt == eMazeWall) {
- int sprite2 = wallSprites[(y + 2) * 5 + (x + 2)];
- if (sprite2 > 0){ dungeonWalls.DrawImage(0, 0, sprite2);}
- }
- }
- }
- }
- void PlayerController::UpdateRotation()
- {
- playerChar.rotation_x = 0;
- playerChar.rotation_y = 0;
- if(this.faceDir == eFaceDown) playerChar.rotation_y = 1;
- if(this.faceDir == eFaceUp) playerChar.rotation_y = -1;
- if(this.faceDir == eFaceLeft) playerChar.rotation_x = -1;
- if(this.faceDir == eFaceRight) playerChar.rotation_x = 1;
- }
- void PlayerController::Rotate(int direction)
- {
- this.faceDir += direction;
- if(this.faceDir > 3) this.faceDir = eFaceUp;
- else if(this.faceDir < 0) this.faceDir = eFaceLeft;
- this.UpdateRotation();
- renderVisionCone();
- }
- void PlayerController::Move(int distance)
- {
- map[0]="###### ###";
- map[1]="###### ###";
- map[2]="###### ###";
- map[3]=" ";
- map[4]="###### ###";
- map[5]="###### ###";
- map[6]="###### ###";
- map[7]="###### ###";
- map[8]="###### ###";
- map[9]="###### ###";
- // 0123456789
- int x = this.x + distance * playerChar.rotation_x;
- int y = this.y + distance * playerChar.rotation_y;
- if((y < 0 || y > 9) || x < 0 || x > 10) return; //can't move off the map as defined above
- char tile = map[y].Chars[x];
- if(tile != ' ') return;
- //Move the player
- this.x += distance * playerChar.rotation_x;
- this.y += distance * playerChar.rotation_y;
- renderVisionCone();
- }
- function SetPlayerStart(int x, int y, PlayerFaceDirection faceDir)
- {
- playerChar.x = x;
- playerChar.y = y;
- playerChar.faceDir = faceDir;
- playerChar.UpdateRotation();
- //renderVisionCone(); no room loaded yet lol
- }
- int mapSize;
- function repeatedly_execute()
- {
- if (playerChar.faceDir==eFaceUp){ labelDirection.Text="Up";}
- if (playerChar.faceDir==eFaceDown){ labelDirection.Text="Down";}
- if (playerChar.faceDir==eFaceRight){ labelDirection.Text="Right";}
- if (playerChar.faceDir==eFaceLeft){ labelDirection.Text="Left";}
- labelXY.Text=String.Format("x:%d y:%d", playerChar.x, playerChar.y);
- }
- function on_key_press(eKeyCode keycode, int mod)
- {
- if(keycode==eKeyA){
- playerChar.Rotate(-1);
- }
- else if (keycode==eKeyD){
- playerChar.Rotate(1);
- }
- else if (keycode==eKeyW){
- playerChar.Move(1);
- }
- else if (keycode==eKeyS){
- playerChar.Move(-1);
- }
- else if (keycode==eKeyP){
- MazeType dumbtest = GetCell(2, 2);
- if (dumbtest==eMazeFree){
- Display("Not wall!");
- }
- else if (dumbtest==eMazeWall){
- Display("Wall!");
- }
- }
- }
- function game_start() {
- /* wall sprites, 3 rows and 5 columns (eFaceUp)
- 14 10 9 3 13
- 0 8 2 7 0
- 0 6 X 5 0
- */
- wallSprites[0] = 14; wallSprites[1] = 10; wallSprites[2] = 9; wallSprites[3] = 3; wallSprites[4] = 13;
- wallSprites[6] = 8; wallSprites[7] = 2; wallSprites[8] = 7; wallSprites[11] = 6; wallSprites[13] = 5;
- SetPlayerStart(3, 3, eFaceRight);
- }
Advertisement
Add Comment
Please, Sign In to add comment