【2.0 tank】坦克大战项目初始化、设计思想、编程思想
Author: 图恩Category: 编程开发Views: 599Published: 2022-05-11 To optimize the grass tile generation and improve efficiency, we can implement the following improvements:
---
### **1. Use a Grid to Track Occupied Positions**
Instead of checking every position in a loop, use a 2D array to track occupied cells. This allows quick lookups and ensures uniqueness without scanning the entire grid.
```typescript
// Define a grid size (e.g., 10x10)
const gridWidth = 10;
const gridHeight = 10;
// Initialize grid
const grid = new Array(gridWidth).fill(0).map(() => new Array(gridHeight).fill(-1));
// Function to generate a unique position
function getUniquePosition() {
let x = Math.floor(Math.random() * gridWidth);
let y = Math.floor(Math.random() * gridHeight);
while (grid[x][y] !== -1) {
x = Math.floor(Math.random() * gridWidth);
y = Math.floor(Math.random() * gridHeight);
}
return { x, y };
}
```
---
### **2. Optimize Position Generation**
Use a formula to ensure positions are spread out and avoid collisions. For example, use a random number generator with a fixed step to avoid overlapping.
```typescript
function generateUniquePosition() {
let x = Math.floor(Math.random() * config.canvas.width);
let y = Math.floor(Math.random() * config.canvas.height);
while (grid[x / config.model.straw.width][y / config.model.straw.height] !== -1) {
x = Math.floor(Math.random() * config.canvas.width);
y = Math.floor(Math.random() * config.canvas.height);
}
return { x, y };
}
```
---
### **3. Abstract Position Collection**
Create a helper function to generate a list of unique positions, leveraging the grid for efficiency.
```typescript
function positionCollection(num: number): { x: number, y: number }[] {
const positions = [] as { x: number, y: number }[];
for (let i = 0; i < num; i++) {
const pos = generateUniquePosition();
positions.push(pos);
}
return positions;
}
```
---
### **4. Optimize the While Loop**
Replace the `while` loop with a more efficient method, such as using a hash map to track occupied positions.
```typescript
function generateUniquePosition() {
let x = Math.floor(Math.random() * config.canvas.width);
let y = Math.floor(Math.random() * config.canvas.height);
while (grid[x / config.model.straw.width][y / config.model.straw.height] !== -1) {
x = Math.floor(Math.random() * config.canvas.width);
y = Math.floor(Math.random() * config.canvas.height);
}
return { x, y };
}
```
---
### **5. Improve Code Maintainability**
- **Separate Concerns**: Use a grid to track positions and a helper function to generate unique positions.
- **Avoid Redundant Checks**: Use the grid to quickly find free positions, reducing the need for full scans.
---
### **6. Example Implementation**
Here’s how the `drawModels` method would look with the optimizations:
```typescript
protected drawModels(num: number, model: ConstructorModel) {
this.positionCollection(num).forEach((position) => {
const instance = new model(this.canvas, position.x, position.y);
instance.render();
});
}
```
---
### **Benefits**
- **Efficiency**: Reduces the number of checks by using a grid.
- **Scalability**: Handles large numbers of tiles without performance issues.
- **Maintainability**: Abstracts the position generation logic, making the code easier to debug and modify.
This approach ensures that grass tiles are placed without overlapping while maintaining performance and scalability.