Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.IO;
- using GameCore;
- namespace Game1
- {
- public partial class Form1 : Form
- {
- int[,] _map = new int[50, 50];
- Image _groundBlock = Resource1.Ground1;
- Image _sandBlock = Resource1.Ground2;
- Image _player = Resource1.ForestGump;
- const int blockWidth = 80, blockHeight = 80;
- Camera mainCamera = new Camera(80);
- float playerX = 0;
- float playerY = 0;
- int currentPlacedBlock = 0;
- public Form1()
- {
- InitializeComponent();
- this.SetStyle(ControlStyles.UserPaint |
- ControlStyles.AllPaintingInWmPaint |
- ControlStyles.DoubleBuffer, true);
- KeyPreview = true;
- GenerateWorld();
- }
- private void exitButton_Click(object sender, EventArgs e)
- {
- Application.Exit();
- }
- private void MainLoopTimer_Tick(object sender, EventArgs e)
- {
- Point cursor = PointToClient(Cursor.Position);
- MoveCamera(cursor);
- log.Text = $"{cursor.ToString()}\n{mainCamera.ToString()}";
- if (!IsGrounded())
- {
- float prevY = playerY;
- playerY = playerY + (1f / 10f);
- if((int)playerY < Math.Round(prevY, MidpointRounding.AwayFromZero))
- {
- playerY = (int)Math.Round(prevY, MidpointRounding.AwayFromZero);
- }
- }
- Invalidate();
- Refresh();
- }
- private void MoveCamera(Point point)
- {
- mainCamera.X += (int)(10 * CalcDeep(800, 50, point.X));
- mainCamera.Y += (int)(10 * CalcDeep(600, 50, point.Y));
- mainCamera.Y = Clamp(mainCamera.Y, 0, _map.GetLength(1) * mainCamera.Scale - 600);
- mainCamera.X = Clamp(mainCamera.X, 0, _map.GetLength(0) * mainCamera.Scale - 800);
- }
- private float CalcDeep(float length, float border, float current)
- {
- if(current < border)
- {
- return -(1 - Clamp(current, 0, border) / border);
- }
- if(current > (length - border))
- {
- return Clamp(1f - (length - current) / border, 0 , 1);
- }
- return 0;
- }
- private float Clamp(float value, float min, float max)
- {
- if (value > max)
- {
- return max;
- }
- else if (value < min)
- {
- return min;
- }
- return value;
- }
- private int Clamp(int value, int min, int max)
- {
- if(value > max)
- {
- return max;
- }
- else if(value < min)
- {
- return min;
- }
- return value;
- }
- private void Form1_Paint(object sender, PaintEventArgs e)
- {
- Graphics g = e.Graphics;
- for(int x = 0; x < _map.GetLength(0); x++)
- {
- for(int y = 0; y < _map.GetLength(1); y++)
- {
- int blockType = _map[x, y];
- Rectangle rect = mainCamera.MatrixToSceenProjection(x, y);
- DrawBlock(rect, blockType, g);
- }
- }
- g.DrawImage(_player, mainCamera.MatrixToSceenProjection(playerX, playerY));
- }
- private void Form1_Click(object sender, EventArgs e)
- {
- Point cursor = PointToClient(Cursor.Position);
- if(cursor.X < 0 || cursor.Y < 0 || cursor.X > 800 || cursor.Y > 600)
- {
- return;
- }
- int x, y;
- x = (cursor.X + (int)mainCamera.X) / blockWidth;
- y = (cursor.Y + (int)mainCamera.Y) / blockHeight;
- _map[x, y] = currentPlacedBlock;
- if (currentPlacedBlock == 0)
- {
- DestroyNotChainedBlocks();
- }
- }
- private void DrawBlock(Rectangle rect, int blockType, Graphics g)
- {
- Image currentImage = null;
- if (blockType == 1)
- {
- currentImage = _groundBlock;
- }
- else if (blockType == 2)
- {
- currentImage = _sandBlock;
- }
- if (currentImage == null)
- return;
- g.DrawImage(currentImage, rect);
- }
- private void ClearWorld()
- {
- for(int x = 0; x < _map.GetLength(0); x++)
- {
- for(int y = 0; y < _map.GetLength(1); y++)
- {
- _map[x, y] = 0;
- }
- }
- }
- private void GenerateWorld()
- {
- ClearWorld();
- Random r = new Random();
- int direction = 1;
- int y = 40;
- for (int x = 0; x < _map.GetLength(0); x++)
- {
- y += direction;
- direction = r.Next(-1, 2);
- for (int _y = y; _y < _map.GetLength(1); _y++)
- {
- _map[x, _y] = 1;
- }
- }
- for(int i = 0; i < _map.GetLength(1); i++)
- {
- if(_map[3, i] != 0)
- {
- playerY = i - 1;
- playerX = 3;
- break;
- }
- }
- }
- private void SaveWorld()
- {
- File.Delete("first.world");
- for (int x = 0; x < _map.GetLength(0); x++)
- {
- for (int y = 0; y < _map.GetLength(1); y++)
- {
- if(_map[x, y] != 0)
- {
- File.AppendAllText("first.world", $"Block:X={x};Y={y};Type={_map[x,y]}\n");
- }
- }
- }
- File.AppendAllText("first.world", $"Camera:X={mainCamera.X};Y={mainCamera.X}\n");
- }
- private void DestroyNotChainedBlocks()
- {
- List<Point> whiteList = new List<Point>();
- Queue<Point> queue = new Queue<Point>();
- for(int x = 0; x < 50; x++)
- {
- queue.Enqueue(new Point(x, 49));
- while(queue.Count > 0)
- {
- Point current = queue.Dequeue();
- whiteList.Add(current);
- Point[] points =
- {
- new Point(current.X + 1, current.Y - 1),
- new Point(current.X + 1, current.Y + 1),
- new Point(current.X - 1, current.Y - 1),
- new Point(current.X - 1, current.Y + 1),
- new Point(current.X, current.Y - 1),
- new Point(current.X, current.Y + 1),
- new Point(current.X + 1, current.Y),
- new Point(current.X - 1, current.Y),
- };
- for(int i = 0; i < points.Length; i++)
- {
- if (points[i].X > 49 || points[i].X < 0) continue;
- if (points[i].Y > 49 || points[i].Y < 0) continue;
- if (!whiteList.Contains(points[i]) &&
- _map[points[i].X, points[i].Y] != 0)
- {
- whiteList.Add(points[i]);
- queue.Enqueue(points[i]);
- }
- }
- }
- }
- for(int x = 0; x < _map.GetLength(0); x++)
- {
- for(int y = 0; y < _map.GetLength(1); y++)
- {
- if(!whiteList.Contains(new Point(x, y)))
- {
- _map[x, y] = 0;
- }
- }
- }
- }
- private bool IsGrounded()
- {
- if (playerY == 49) return true;
- if(_map[(int)playerX, (int)playerY + 1] != 0)
- {
- return true;
- }
- if(_map[(int)Math.Round(playerX, MidpointRounding.AwayFromZero), (int)playerY + 1] != 0)
- {
- return true;
- }
- return false;
- }
- //UI Handles
- private void button1_Click(object sender, EventArgs e)
- {
- currentPlacedBlock = 1;
- }
- private void button2_Click(object sender, EventArgs e)
- {
- currentPlacedBlock = 2;
- }
- private void generateButton_Click(object sender, EventArgs e)
- {
- GenerateWorld();
- }
- private void loadButton_Click(object sender, EventArgs e)
- {
- ClearWorld();
- _map = Core.LoadWorld();
- }
- private void saveButton_Click(object sender, EventArgs e)
- {
- SaveWorld();
- }
- private void Form1_KeyDown(object sender, KeyEventArgs e)
- {
- if(e.KeyCode == Keys.D)
- {
- playerX += 0.2f;
- }
- if(e.KeyCode == Keys.E)
- {
- mainCamera.X = (int)playerX * mainCamera.Scale;
- mainCamera.Y = (int)playerY * mainCamera.Scale;
- }
- }
- private void Form1_KeyPress(object sender, KeyPressEventArgs e)
- {
- }
- private void button3_Click(object sender, EventArgs e)
- {
- currentPlacedBlock = 0;
- }
- }
- }
Add Comment
Please, Sign In to add comment