0458-Bomb-道具和效果

环境

  • Time 2024-03-22
  • Zig 0.12.0-dev.3152+90c1a2c41
  • WSL-Ubuntu 22.04.3 LTS
  • raylib 5.0

前言

说明

参考资料:

  1. 《游戏开发:世嘉新人培训教材》
  2. 图片素材:https://www.ituring.com.cn/book/1742

目标

实现道具的获取和增加道具功能。

player.zig

增加了道具的数量和长度的变量。

const std = @import("std");
const engine = @import("../engine.zig");
const core = @import("core.zig");

const speedUnit = 1000;

pub const Player = struct {
    x: usize,
    y: usize,
    bombNumber: usize = 0,
    maxBombNumber: usize = 1,
    maxBombLength: usize = 1,
    type: core.MapType,

    pub fn genEnemy(x: usize, y: usize) Player {
        return init(x, y, .enemy);
    }

    pub fn genPlayer(x: usize, y: usize) Player {
        return init(x, y, .player1);
    }

    fn init(x: usize, y: usize, t: core.MapType) Player {
        return Player{
            .x = x * core.getMapUnit() * speedUnit,
            .y = y * core.getMapUnit() * speedUnit,
            .type = t,
        };
    }

    pub fn getCell(self: Player) engine.Vector {
        const unit = core.getMapUnit();
        return .{
            .x = (self.x / speedUnit + (unit / 2)) / unit,
            .y = (self.y / speedUnit + (unit / 2)) / unit,
        };
    }

    pub fn draw(self: Player) void {
        const x = self.x / speedUnit;
        core.drawXY(self.type, x, self.y / speedUnit);
    }

    pub fn toCollisionRec(self: Player) engine.Rectangle {
        return engine.Rectangle{
            .x = self.x / speedUnit + 5,
            .y = self.y / speedUnit + 5,
            .width = core.getMapUnit() - 10,
            .height = core.getMapUnit() - 7,
        };
    }
};

map.zig

增加了获取道具的功能。

    pub fn update(self: *Map) void {
        const pos = self.player1().getCell();
        const mapUnit = self.world.indexRef(pos.x, pos.y);

        if (mapUnit.contains(.item)) {
            mapUnit.remove(.item);
            self.player1().maxBombNumber += 1;
        }

        if (mapUnit.contains(.power)) {
            self.player1().maxBombLength += 1;
            mapUnit.remove(.power);
        }

        self.world.update();
    }

world.zig

重新实现了道具的效果,具有加长的功能。

    fn explosion(self: *World, mapUnit: *core.MapUnit, idx: usize) void {
        const time = engine.time();
        mapUnit.remove(.bomb);
        mapUnit.insertTimedType(.explosion, time);
        // 左
        self.explosionLeft(time, idx);
        // 右
        self.explosionRight(time, idx);
        // 上
        self.explosionUp(time, idx);
        // 下
        self.explosionDown(time, idx);
    }

    fn explosionLeft(self: *World, time: usize, idx: usize) void {
        for (1..self.player1().maxBombLength + 1) |i| {
            const mapUnit = &self.data[idx -| i];
            if (mapUnit.contains(.wall)) return;
            if (mapUnit.contains(.brick)) mapUnit.remove(.brick);
            mapUnit.insertTimedType(.fireX, time);
        }
    }

    fn explosionRight(self: *World, time: usize, idx: usize) void {
        for (1..self.player1().maxBombLength + 1) |i| {
            const mapUnit = &self.data[idx + i];
            if (mapUnit.contains(.wall)) return;
            if (mapUnit.contains(.brick)) mapUnit.remove(.brick);
            mapUnit.insertTimedType(.fireX, time);
        }
    }

    fn explosionUp(self: *World, time: usize, idx: usize) void {
        for (1..self.player1().maxBombLength + 1) |i| {
            const mapUnit = &self.data[idx -| (self.width * i)];
            if (mapUnit.contains(.wall)) return;
            if (mapUnit.contains(.brick)) mapUnit.remove(.brick);
            mapUnit.insertTimedType(.fireY, time);
        }
    }

    fn explosionDown(self: *World, time: usize, idx: usize) void {
        for (1..self.player1().maxBombLength + 1) |i| {
            const mapUnit = &self.data[idx + (self.width * i)];
            if (mapUnit.contains(.wall)) return;
            if (mapUnit.contains(.brick)) mapUnit.remove(.brick);
            mapUnit.insertTimedType(.fireY, time);
        }
    }

效果

bomb

总结

实现了道具的获取,并且增加玩家获取道具的功能。

附录

posted @ 2026-08-16 08:56  jiangbo4444  阅读(10)  评论(0)    收藏  举报